Commits

Amaury Forgeot d'Arc committed b31b645

Try to use it in the posix module

Comments (0)

Files changed (2)

pypy/interpreter/error.py

 else:
     _WINDOWS = True
 
-    def wrap_windowserror(space, e, filename=None):
+    def wrap_windowserror(space, e, w_filename=None):
         from pypy.rlib import rwin32
 
         winerror = e.winerror
         except ValueError:
             msg = 'Windows Error %d' % winerror
         exc = space.w_WindowsError
-        if filename is not None:
+        if w_filename is not None:
             w_error = space.call_function(exc, space.wrap(winerror),
-                                          space.wrap(msg), space.wrap(filename))
+                                          space.wrap(msg), w_filename)
         else:
             w_error = space.call_function(exc, space.wrap(winerror),
                                           space.wrap(msg))
         return OperationError(exc, w_error)
 
-def wrap_oserror(space, e, filename=None, exception_name='w_OSError'): 
+def wrap_oserror2(space, e, w_filename=None, exception_name='w_OSError'): 
     assert isinstance(e, OSError)
 
     if _WINDOWS and isinstance(e, WindowsError):
-        return wrap_windowserror(space, e, filename)
+        return wrap_windowserror(space, e, w_filename)
 
     errno = e.errno
     try:
     except ValueError:
         msg = 'error %d' % errno
     exc = getattr(space, exception_name)
+    if w_filename is not None:
+        w_error = space.call_function(exc, space.wrap(errno),
+                                      space.wrap(msg), w_filename)
+    else:
+        w_error = space.call_function(exc, space.wrap(errno),
+                                      space.wrap(msg))
+    return OperationError(exc, w_error)
+wrap_oserror2._annspecialcase_ = 'specialize:arg(3)'
+
+def wrap_oserror(space, e, filename=None, exception_name='w_OSError'):
     if filename is not None:
-        w_error = space.call_function(exc, space.wrap(errno),
-                                      space.wrap(msg), space.wrap(filename))
+        return wrap_oserror2(space, e, space.wrap(filename),
+                             exception_name=exception_name)
     else:
-        w_error = space.call_function(exc, space.wrap(errno), space.wrap(msg))
-    return OperationError(exc, w_error)
+        return wrap_oserror2(space, e, None,
+                             exception_name=exception_name)
 wrap_oserror._annspecialcase_ = 'specialize:arg(3)'
+

pypy/module/posix/interp_posix.py

 from pypy.rlib import rposix
 from pypy.rlib.rarithmetic import r_longlong
 from pypy.rlib.unroll import unrolling_iterable
-from pypy.interpreter.error import OperationError, wrap_oserror
+from pypy.interpreter.error import OperationError, wrap_oserror, wrap_oserror2
 from pypy.rpython.module.ll_os import RegisterOs
 from pypy.rpython.module import ll_os_stat
 from pypy.rpython.lltypesystem import rffi, lltype
 import os, sys
 _WIN = sys.platform == 'win32'
 
-def open(space, fname, flag, mode=0777):
+class FileEncoder:
+    def __init__(self, space, w_obj):
+        self.space = space
+        self.w_obj = w_obj
+
+    def encode(self):
+        return self.space.path_w(self.w_obj)
+
+def open(space, w_fname, flag, mode=0777):
     """Open a file (for low level IO).
 Return a file descriptor (a small integer)."""
-    try: 
-        fd = os.open(fname, flag, mode)
+    try:
+        if space.isinstance_w(w_fname, space.w_unicode):
+            fname = FileEncoder(space, w_fname)
+            fd = rposix.open(fname, flag, mode)
+        else:
+            fname = space.str_w(w_fname)
+            fd = rposix.open(fname, flag, mode)
     except OSError, e: 
-        raise wrap_oserror(space, e, fname)
+        raise wrap_oserror2(space, e, w_fname)
     return space.wrap(fd)
-open.unwrap_spec = [ObjSpace, 'path', "c_int", "c_int"]
+open.unwrap_spec = [ObjSpace, W_Root, "c_int", "c_int"]
 
 def lseek(space, fd, pos, how):
     """Set the current position of a file descriptor.  Return the new position.