[pypy-svn] r79941 - in pypy/trunk/pypy: module/posix module/posix/test rpython/module translator/c/test

arigo at codespeak.net arigo at codespeak.net
Thu Dec 9 19:03:51 CET 2010


Author: arigo
Date: Thu Dec  9 19:03:49 2010
New Revision: 79941

Modified:
   pypy/trunk/pypy/module/posix/__init__.py
   pypy/trunk/pypy/module/posix/interp_posix.py
   pypy/trunk/pypy/module/posix/test/test_posix2.py
   pypy/trunk/pypy/rpython/module/ll_os.py
   pypy/trunk/pypy/translator/c/test/test_extfunc.py
Log:
os.fchdir().


Modified: pypy/trunk/pypy/module/posix/__init__.py
==============================================================================
--- pypy/trunk/pypy/module/posix/__init__.py	(original)
+++ pypy/trunk/pypy/module/posix/__init__.py	Thu Dec  9 19:03:49 2010
@@ -77,6 +77,8 @@
         interpleveldefs['fsync'] = 'interp_posix.fsync'
     if hasattr(os, 'fdatasync'):
         interpleveldefs['fdatasync'] = 'interp_posix.fdatasync'
+    if hasattr(os, 'fchdir'):
+        interpleveldefs['fchdir'] = 'interp_posix.fchdir'
     if hasattr(os, 'putenv'):
         interpleveldefs['putenv'] = 'interp_posix.putenv'
     if hasattr(posix, 'unsetenv'): # note: emulated in os

Modified: pypy/trunk/pypy/module/posix/interp_posix.py
==============================================================================
--- pypy/trunk/pypy/module/posix/interp_posix.py	(original)
+++ pypy/trunk/pypy/module/posix/interp_posix.py	Thu Dec  9 19:03:49 2010
@@ -168,6 +168,14 @@
         raise wrap_oserror(space, e)
 fdatasync.unwrap_spec = [ObjSpace, W_Root]
 
+def fchdir(space, w_fd):
+    fd = space.c_filedescriptor_w(w_fd)
+    try:
+        os.fchdir(fd)
+    except OSError, e:
+        raise wrap_oserror(space, e)
+fchdir.unwrap_spec = [ObjSpace, W_Root]
+
 # ____________________________________________________________
 
 # For LL backends, expose all fields.

Modified: pypy/trunk/pypy/module/posix/test/test_posix2.py
==============================================================================
--- pypy/trunk/pypy/module/posix/test/test_posix2.py	(original)
+++ pypy/trunk/pypy/module/posix/test/test_posix2.py	Thu Dec  9 19:03:49 2010
@@ -546,7 +546,7 @@
     if hasattr(os, 'fdatasync'):
         def test_fdatasync(self):
             os = self.posix
-            f = open(self.path2)
+            f = open(self.path2, "w")
             try:
                 fd = f.fileno()
                 os.fdatasync(fd)
@@ -555,6 +555,24 @@
             raises(OSError, os.fdatasync, fd)
             raises(ValueError, os.fdatasync, -1)
 
+    if hasattr(os, 'fchdir'):
+        def test_fchdir(self):
+            os = self.posix
+            localdir = os.getcwd()
+            try:
+                os.mkdir(self.path2 + 'dir')
+                fd = os.open(self.path2 + 'dir', os.O_RDONLY)
+                try:
+                    os.fchdir(fd)
+                    mypath = os.getcwd()
+                finally:
+                    os.close(fd)
+                assert mypath.endswith('test_posix2-dir')
+                raises(OSError, os.fchdir, fd)
+                raises(ValueError, os.fchdir, -1)
+            finally:
+                os.chdir(localdir)
+
     def test_largefile(self):
         os = self.posix
         fd = os.open(self.path2 + 'test_largefile', os.O_RDWR | os.O_CREAT, 0666)

Modified: pypy/trunk/pypy/rpython/module/ll_os.py
==============================================================================
--- pypy/trunk/pypy/rpython/module/ll_os.py	(original)
+++ pypy/trunk/pypy/rpython/module/ll_os.py	Thu Dec  9 19:03:49 2010
@@ -903,6 +903,18 @@
                       llimpl=fdatasync_llimpl,
                       export_name="ll_os.ll_os_fdatasync")
 
+    @registering_if(os, 'fchdir')
+    def register_os_fchdir(self):
+        os_fchdir = self.llexternal('fchdir', [rffi.INT], rffi.INT)
+
+        def fchdir_llimpl(fd):
+            res = rffi.cast(rffi.LONG, os_fchdir(rffi.cast(rffi.INT, fd)))
+            if res < 0:
+                raise OSError(rposix.get_errno(), "fchdir failed")
+        return extdef([int], s_None,
+                      llimpl=fchdir_llimpl,
+                      export_name="ll_os.ll_os_fchdir")
+
     @registering_str_unicode(os.access)
     def register_os_access(self, traits):
         os_access = self.llexternal(traits.posix_function_name('access'),

Modified: pypy/trunk/pypy/translator/c/test/test_extfunc.py
==============================================================================
--- pypy/trunk/pypy/translator/c/test/test_extfunc.py	(original)
+++ pypy/trunk/pypy/translator/c/test/test_extfunc.py	Thu Dec  9 19:03:49 2010
@@ -765,3 +765,21 @@
         f = compile(does_stuff, [])
         res = f()
         assert type(res) is float and res >= 0.0
+
+if hasattr(os, 'fchdir'):
+    def test_os_fchdir():
+        def does_stuff():
+            fd = os.open('/', os.O_RDONLY, 0400)
+            try:
+                os.fchdir(fd)
+                s = os.getcwd()
+            finally:
+                os.close(fd)
+            return s == '/'
+        f = compile(does_stuff, [])
+        localdir = os.getcwd()
+        try:
+            res = f()
+        finally:
+            os.chdir(localdir)
+        assert res == True



More information about the Pypy-commit mailing list