[pypy-svn] r79939 - in pypy/trunk/pypy/module/posix: . test

arigo at codespeak.net arigo at codespeak.net
Thu Dec 9 18:43:48 CET 2010


Author: arigo
Date: Thu Dec  9 18:43:46 2010
New Revision: 79939

Modified:
   pypy/trunk/pypy/module/posix/interp_posix.py
   pypy/trunk/pypy/module/posix/test/test_posix2.py
Log:
Support os.fsync(f) and os.fdatasync(f), where f is any object
with a fileno() method.


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 18:43:46 2010
@@ -4,6 +4,7 @@
 from pypy.rlib.rarithmetic import r_longlong
 from pypy.rlib.unroll import unrolling_iterable
 from pypy.interpreter.error import OperationError, wrap_oserror, wrap_oserror2
+from pypy.interpreter.error import operationerrfmt
 from pypy.rpython.module.ll_os import RegisterOs
 from pypy.rpython.module import ll_os_stat
 from pypy.rpython.lltypesystem import rffi, lltype
@@ -151,19 +152,34 @@
         raise wrap_oserror(space, e) 
 ftruncate.unwrap_spec = [ObjSpace, "c_int", r_longlong]
 
-def fsync(space, fd):
+def _as_filedescriptor(space, w_fd):
+    try:
+        fd = space.c_int_w(w_fd)
+    except OperationError, e:
+        if not e.match(space, space.w_TypeError):
+            raise
+        w_fd = space.call_method(w_fd, 'fileno')
+        fd = space.c_int_w(w_fd)
+    if fd < 0:
+        raise operationerrfmt(space.w_ValueError,
+            "file descriptor cannot be a negative integer (%d)", fd)
+    return fd
+
+def fsync(space, w_fd):
+    fd = _as_filedescriptor(space, w_fd)
     try:
         os.fsync(fd)
     except OSError, e:
         raise wrap_oserror(space, e)
-fsync.unwrap_spec = [ObjSpace, "c_int"]
+fsync.unwrap_spec = [ObjSpace, W_Root]
 
-def fdatasync(space, fd):
+def fdatasync(space, w_fd):
+    fd = _as_filedescriptor(space, w_fd)
     try:
         os.fdatasync(fd)
     except OSError, e:
         raise wrap_oserror(space, e)
-fdatasync.unwrap_spec = [ObjSpace, "c_int"]
+fdatasync.unwrap_spec = [ObjSpace, W_Root]
 
 # ____________________________________________________________
 

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 18:43:46 2010
@@ -536,14 +536,11 @@
             try:
                 fd = f.fileno()
                 os.fsync(fd)
-            finally:
+                os.fsync(f)     # <- should also work with a file, or anything
+            finally:            #    with a fileno() method
                 f.close()
-            try:
-                os.fsync(fd)
-            except OSError:
-                pass
-            else:
-                raise AssertionError("os.fsync didn't raise")
+            raises(OSError, os.fsync, fd)
+            raises(ValueError, os.fsync, -1)
 
     if hasattr(os, 'fdatasync'):
         def test_fdatasync(self):
@@ -554,12 +551,8 @@
                 os.fdatasync(fd)
             finally:
                 f.close()
-            try:
-                os.fdatasync(fd)
-            except OSError:
-                pass
-            else:
-                raise AssertionError("os.fdatasync didn't raise")
+            raises(OSError, os.fdatasync, fd)
+            raises(ValueError, os.fdatasync, -1)
 
     def test_largefile(self):
         os = self.posix



More information about the Pypy-commit mailing list