[pypy-svn] r78001 - in pypy/branch/fast-forward/pypy/module/_io: . test

afa at codespeak.net afa at codespeak.net
Fri Oct 15 18:00:15 CEST 2010


Author: afa
Date: Fri Oct 15 18:00:10 2010
New Revision: 78001

Modified:
   pypy/branch/fast-forward/pypy/module/_io/interp_fileio.py
   pypy/branch/fast-forward/pypy/module/_io/test/test_io.py
Log:
_io.open() now accepts a file descriptor


Modified: pypy/branch/fast-forward/pypy/module/_io/interp_fileio.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_io/interp_fileio.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_io/interp_fileio.py	Fri Oct 15 18:00:10 2010
@@ -88,6 +88,8 @@
         if space.isinstance_w(w_name, space.w_float):
             raise OperationError(space.w_TypeError, space.wrap(
                 "integer argument expected, got float"))
+
+        fd = -1
         try:
             fd = space.int_w(w_name)
         except OperationError, e:
@@ -99,12 +101,16 @@
 
         self.readable, self.writable, flags = decode_mode(space, mode)
 
-        from pypy.module.posix.interp_posix import dispatch_filename, rposix
-        try:
-            self.fd = dispatch_filename(rposix.open)(
-                space, w_name, flags, 0666)
-        except OSError, e:
-            raise wrap_oserror2(space, e, w_name)
+        if fd >= 0:
+            self.fd = fd
+        else:
+            from pypy.module.posix.interp_posix import (
+                dispatch_filename, rposix)
+            try:
+                self.fd = dispatch_filename(rposix.open)(
+                    space, w_name, flags, 0666)
+            except OSError, e:
+                raise wrap_oserror2(space, e, w_name)
         self.closefd = bool(closefd)
 
     def _check_closed(self, space):

Modified: pypy/branch/fast-forward/pypy/module/_io/test/test_io.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_io/test/test_io.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_io/test/test_io.py	Fri Oct 15 18:00:10 2010
@@ -1,4 +1,5 @@
 from pypy.conftest import gettestobjspace
+import os
 
 class AppTestIoModule:
     def setup_class(cls):
@@ -42,8 +43,18 @@
         from pypy.tool.udir import udir
         tmpfile = udir.join('tmpfile').ensure()
         cls.w_tmpfile = cls.space.wrap(str(tmpfile))
+        cls.w_posix = cls.space.appexec([], """():
+            import %s as m;
+            return m""" % os.name)
 
     def test_open(self):
         import io
         f = io.open(self.tmpfile, "rb")
         f.close()
+
+    def test_open_fd(self):
+        import io
+        os = self.posix
+        fd = os.open(self.tmpfile, os.O_RDONLY, 0666)
+        f = io.open(fd, "rb")
+        f.close()



More information about the Pypy-commit mailing list