[pypy-svn] pypy default: Properly convert WindowsErrors coming from the streamio module.

amauryfa commits-noreply at bitbucket.org
Thu Feb 17 20:07:55 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r42119:ff019dd9c604
Date: 2011-02-17 18:45 +0100
http://bitbucket.org/pypy/pypy/changeset/ff019dd9c604/

Log:	Properly convert WindowsErrors coming from the streamio module.
	Otherwise all you get is a IOError: [Errno 0] No error: 'foo'

diff --git a/pypy/module/_file/test/test_file_extra.py b/pypy/module/_file/test/test_file_extra.py
--- a/pypy/module/_file/test/test_file_extra.py
+++ b/pypy/module/_file/test/test_file_extra.py
@@ -520,6 +520,14 @@
         assert data == 'hel'
         f.close()
 
+        import errno, sys
+        f = open(fn)
+        exc = raises(OSError, f.truncate, 3)
+        assert exc.value.errno == errno.EACCES
+        if sys.platform == 'win32':
+            assert exc.value.winerror == 5 # ERROR_ACCESS_DENIED
+        f.close()
+
     def test_readinto(self):
         from array import array
         a = array('c')

diff --git a/pypy/module/_file/interp_stream.py b/pypy/module/_file/interp_stream.py
--- a/pypy/module/_file/interp_stream.py
+++ b/pypy/module/_file/interp_stream.py
@@ -2,7 +2,7 @@
 from pypy.rlib import streamio
 from pypy.rlib.streamio import StreamErrors
 
-from pypy.interpreter.error import OperationError
+from pypy.interpreter.error import OperationError, wrap_oserror2
 from pypy.interpreter.gateway import ObjSpace
 from pypy.interpreter.baseobjspace import Wrappable
 from pypy.interpreter.typedef import TypeDef
@@ -17,23 +17,13 @@
     elif isinstance(e, OSError):
         return wrap_oserror_as_ioerror(space, e, w_filename)
     else:
+        # should not happen: wrap_streamerror() is only called when
+        # StreamErrors = (OSError, StreamError) are raised
         return OperationError(space.w_IOError, space.w_None)
 
 def wrap_oserror_as_ioerror(space, e, w_filename=None):
-    assert isinstance(e, OSError)
-    errno = e.errno
-    try:
-        msg = os.strerror(errno)
-    except ValueError:
-        msg = 'error %d' % errno
-    if w_filename is None:
-        w_filename = space.w_None
-    w_error = space.call_function(space.w_IOError,
-                                  space.wrap(errno),
-                                  space.wrap(msg),
-                                  w_filename)
-    return OperationError(space.w_IOError, w_error)
-
+    return wrap_oserror2(space, e, w_filename,
+                         w_exception_class=space.w_IOError)
 
 class W_AbstractStream(Wrappable):
     """Base class for interp-level objects that expose streams to app-level"""


More information about the Pypy-commit mailing list