[pypy-svn] pypy fast-forward: os.fdopen(1234) did not set errno in the raised exception

amauryfa commits-noreply at bitbucket.org
Fri Jan 7 09:27:08 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: fast-forward
Changeset: r40433:3ce1eff8d52c
Date: 2011-01-07 09:28 +0100
http://bitbucket.org/pypy/pypy/changeset/3ce1eff8d52c/

Log:	os.fdopen(1234) did not set errno in the raised exception

diff --git a/pypy/module/posix/test/test_posix2.py b/pypy/module/posix/test/test_posix2.py
--- a/pypy/module/posix/test/test_posix2.py
+++ b/pypy/module/posix/test/test_posix2.py
@@ -13,7 +13,7 @@
 
 def setup_module(mod):
     if os.name != 'nt':
-        mod.space = gettestobjspace(usemodules=['posix'])
+        mod.space = gettestobjspace(usemodules=['posix', 'fcntl'])
     else:
         # On windows, os.popen uses the subprocess module
         mod.space = gettestobjspace(usemodules=['posix', '_rawffi', 'thread'])
@@ -247,12 +247,18 @@
         ex(self.posix.dup, UNUSEDFD)
 
     def test_fdopen(self):
+        import errno
         path = self.path
         posix = self.posix
         fd = posix.open(path, posix.O_RDONLY, 0777)
         f = posix.fdopen(fd, "r")
         f.close()
-        raises(OSError, posix.fdopen, fd)
+
+        # Ensure that fcntl is not faked
+        import fcntl
+        assert fcntl.__file__.endswith('pypy/module/fcntl')
+        exc = raises(OSError, posix.fdopen, fd)
+        assert exc.value.errno == errno.EBADF
 
     def test_fdopen_hackedbuiltins(self):
         "Same test, with __builtins__.file removed"

diff --git a/pypy/module/fcntl/interp_fcntl.py b/pypy/module/fcntl/interp_fcntl.py
--- a/pypy/module/fcntl/interp_fcntl.py
+++ b/pypy/module/fcntl/interp_fcntl.py
@@ -1,6 +1,6 @@
 from pypy.rpython.tool import rffi_platform as platform
 from pypy.rpython.lltypesystem import rffi, lltype
-from pypy.interpreter.error import OperationError
+from pypy.interpreter.error import OperationError, wrap_oserror
 from pypy.interpreter.baseobjspace import W_Root, ObjSpace
 from pypy.rlib import rposix
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
@@ -59,7 +59,6 @@
     return rffi.llexternal(name, args, result, compilation_info=CConfig._compilation_info_)
 
 _flock = lltype.Ptr(cConfig.flock)
-strerror = external('strerror', [rffi.INT], rffi.CCHARP)
 fcntl_int = external('fcntl', [rffi.INT, rffi.INT, rffi.INT], rffi.INT)
 fcntl_str = external('fcntl', [rffi.INT, rffi.INT, rffi.CCHARP], rffi.INT)
 fcntl_flock = external('fcntl', [rffi.INT, rffi.INT, _flock], rffi.INT)
@@ -70,9 +69,10 @@
 if has_flock:
     c_flock = external('flock', [rffi.INT, rffi.INT], rffi.INT)
 
-def _get_error_msg():
+def _get_error(space, funcname):
     errno = rposix.get_errno()
-    return rffi.charp2str(strerror(errno))
+    return wrap_oserror(space, OSError(errno, funcname),
+                        exception_name = 'w_IOError')
 
 def _get_module_object(space, obj_name):
     w_module = space.getbuiltinmodule('fcntl')
@@ -125,8 +125,7 @@
         intarg = rffi.cast(rffi.INT, intarg)   # C long => C int
         rv = fcntl_int(fd, op, intarg)
         if rv < 0:
-            raise OperationError(space.w_IOError,
-                space.wrap(_get_error_msg()))
+            raise _get_error(space, "fcntl")
         return space.wrap(rv)
 
     try:
@@ -140,8 +139,7 @@
         arg = rffi.charpsize2str(ll_arg, len(arg))
         lltype.free(ll_arg, flavor='raw')
         if rv < 0:
-            raise OperationError(space.w_IOError,
-                space.wrap(_get_error_msg()))
+            raise _get_error(space, "fcntl")
         return space.wrap(arg)
 
     raise OperationError(space.w_TypeError,


More information about the Pypy-commit mailing list