Commits

Armin Rigo committed 56918b5

Don't raise uncaught RPython-level OSError

Comments (0)

Files changed (4)

pypy/module/_ffi/interp_funcptr.py

 #
 from pypy.rlib import jit
 from pypy.rlib import libffi
-from pypy.rlib.clibffi import get_libc_name, StackCheckError
+from pypy.rlib.clibffi import get_libc_name, StackCheckError, LibFFIError
 from pypy.rlib.rdynload import DLOpenError
 from pypy.rlib.rarithmetic import intmask, r_uint
 from pypy.rlib.objectmodel import we_are_translated
 from pypy.module._ffi.type_converter import FromAppLevelConverter, ToAppLevelConverter
+from pypy.module._rawffi.interp_rawffi import got_libffi_error
 
 import os
 if os.name == 'nt':
                 raise operationerrfmt(
                     space.w_AttributeError,
                     "No symbol %s found in library %s", name, CDLL.name)
+            except LibFFIError:
+                raise got_libffi_error(space)
 
             return W_FuncPtr(func, argtypes_w, w_restype)
         elif space.isinstance_w(w_name, space.w_int):
                 raise operationerrfmt(
                     space.w_AttributeError,
                     "No ordinal %d found in library %s", ordinal, CDLL.name)
+            except LibFFIError:
+                raise got_libffi_error(space)
+
             return W_FuncPtr(func, argtypes_w, w_restype)
         else:
             raise OperationError(space.w_TypeError, space.wrap(
             raise operationerrfmt(
                 space.w_AttributeError,
                 "No symbol %s found in library %s", name, CDLL.name)
+        except LibFFIError:
+            raise got_libffi_error(space)
 
         return W_FuncPtr(func, argtypes_w, w_restype)
 
                                                                w_argtypes,
                                                                w_restype)
     addr = rffi.cast(rffi.VOIDP, addr)
-    func = libffi.Func(name, argtypes, restype, addr, flags)
     try:
+        func = libffi.Func(name, argtypes, restype, addr, flags)
         return W_FuncPtr(func, argtypes_w, w_restype)
-    except OSError:
-        raise OperationError(space.w_SystemError,
-                         space.wrap("internal error building the Func object"))
+    except LibFFIError:
+        raise got_libffi_error(space)
 
 
 W_FuncPtr.typedef = TypeDef(

pypy/module/_rawffi/callback.py

 from pypy.module._rawffi.array import push_elem
 from pypy.module._rawffi.structure import W_Structure
 from pypy.module._rawffi.interp_rawffi import (W_DataInstance, letter2tp,
-     unwrap_value, unpack_argshapes)
+     unwrap_value, unpack_argshapes, got_libffi_error)
 from pypy.rlib.clibffi import USERDATA_P, CallbackFuncPtr, FUNCFLAG_CDECL
-from pypy.rlib.clibffi import ffi_type_void
+from pypy.rlib.clibffi import ffi_type_void, LibFFIError
 from pypy.rlib import rweakref
 from pypy.module._rawffi.tracker import tracker
 from pypy.interpreter.error import OperationError
             self.result = None
             ffiresult = ffi_type_void
         self.number = global_counter.add(self)
-        self.ll_callback = CallbackFuncPtr(ffiargs, ffiresult,
-                                           callback, self.number, flags)
+        try:
+            self.ll_callback = CallbackFuncPtr(ffiargs, ffiresult,
+                                               callback, self.number, flags)
+        except LibFFIError:
+            raise got_libffi_error(space)
         self.ll_buffer = rffi.cast(rffi.VOIDP, self.ll_callback.ll_closure)
         if tracker.DO_TRACING:
             addr = rffi.cast(lltype.Signed, self.ll_callback.ll_closure)

pypy/module/_rawffi/interp_rawffi.py

     return [unpack_simple_shape(space, w_arg)
             for w_arg in space.unpackiterable(w_argtypes)]
 
+def got_libffi_error(space):
+    raise OperationError(space.w_SystemError,
+                         space.wrap("not supported by libffi"))
+
+
 class W_CDLL(Wrappable):
     def __init__(self, space, name, cdll):
         self.cdll = cdll
             except KeyError:
                 raise operationerrfmt(space.w_AttributeError,
                     "No symbol %s found in library %s", name, self.name)
+            except LibFFIError:
+                raise got_libffi_error(space)
 
         elif (_MS_WINDOWS and
               space.is_true(space.isinstance(w_name, space.w_int))):
             except KeyError:
                 raise operationerrfmt(space.w_AttributeError,
                     "No symbol %d found in library %s", ordinal, self.name)
+            except LibFFIError:
+                raise got_libffi_error(space)
         else:
             raise OperationError(space.w_TypeError, space.wrap(
                 "function name must be string or integer"))
     resshape = unpack_resshape(space, w_res)
     ffi_args = [shape.get_basic_ffi_type() for shape in argshapes]
     ffi_res = resshape.get_basic_ffi_type()
-    ptr = RawFuncPtr('???', ffi_args, ffi_res, rffi.cast(rffi.VOIDP, addr),
-                     flags)
+    try:
+        ptr = RawFuncPtr('???', ffi_args, ffi_res, rffi.cast(rffi.VOIDP, addr),
+                         flags)
+    except LibFFIError:
+        raise got_libffi_error(space)
     return space.wrap(W_FuncPtr(space, ptr, argshapes, resshape))
 
 W_FuncPtr.typedef = TypeDef(

pypy/rlib/clibffi.py

     def __init__(self, message):
         self.message = message
 
+class LibFFIError(Exception):
+    pass
+
 CHUNK = 4096
 CLOSURES = rffi.CArrayPtr(FFI_CLOSUREP.TO)
 
                              rffi.cast(rffi.UINT, argnum), restype,
                              self.ll_argtypes)
         if not res == FFI_OK:
-            raise OSError(-1, "Wrong typedef")
+            raise LibFFIError
 
     def __del__(self):
         if self.ll_cif:
                                  ll_callback, rffi.cast(rffi.VOIDP,
                                                         self.ll_userdata))
         if not res == FFI_OK:
-            raise OSError(-1, "Unspecified error calling ffi_prep_closure")
+            raise LibFFIError
 
     def __del__(self):
         AbstractFuncPtr.__del__(self)