[pypy-commit] pypy bsd-patches: patches from issue 2694, implement ctypes.CDLL(... handle=n)

mattip pypy.commits at gmail.com
Wed Nov 1 15:30:42 EDT 2017


Author: Matti Picus <matti.picus at gmail.com>
Branch: bsd-patches
Changeset: r92896:86e686981d73
Date: 2017-11-01 21:13 +0200
http://bitbucket.org/pypy/pypy/changeset/86e686981d73/

Log:	patches from issue 2694, implement ctypes.CDLL(... handle=n)

diff --git a/lib-python/2.7/ctypes/__init__.py b/lib-python/2.7/ctypes/__init__.py
--- a/lib-python/2.7/ctypes/__init__.py
+++ b/lib-python/2.7/ctypes/__init__.py
@@ -360,14 +360,15 @@
         self._FuncPtr = _FuncPtr
 
         if handle is None:
-            if flags & _FUNCFLAG_CDECL:
-                pypy_dll = _ffi.CDLL(name, mode)
-            else:
-                pypy_dll = _ffi.WinDLL(name, mode)
-            self.__pypy_dll__ = pypy_dll
-            handle = int(pypy_dll)
-            if _sys.maxint > 2 ** 32:
-                handle = int(handle)   # long -> int
+            handle = 0
+        if flags & _FUNCFLAG_CDECL:
+            pypy_dll = _ffi.CDLL(name, mode, handle)
+        else:
+            pypy_dll = _ffi.WinDLL(name, mode, handle)
+        self.__pypy_dll__ = pypy_dll
+        handle = int(pypy_dll)
+        if _sys.maxint > 2 ** 32:
+            handle = int(handle)   # long -> int
         self._handle = handle
 
     def __repr__(self):
diff --git a/pypy/module/_rawffi/alt/interp_funcptr.py b/pypy/module/_rawffi/alt/interp_funcptr.py
--- a/pypy/module/_rawffi/alt/interp_funcptr.py
+++ b/pypy/module/_rawffi/alt/interp_funcptr.py
@@ -314,7 +314,7 @@
 # ========================================================================
 
 class W_CDLL(W_Root):
-    def __init__(self, space, name, mode):
+    def __init__(self, space, name, mode, handle):
         self.flags = libffi.FUNCFLAG_CDECL
         self.space = space
         if name is None:
@@ -322,7 +322,7 @@
         else:
             self.name = name
         try:
-            self.cdll = libffi.CDLL(name, mode)
+            self.cdll = libffi.CDLL(name, mode, handle)
         except DLOpenError as e:
             raise wrap_dlopenerror(space, e, self.name)
         except OSError as e:
@@ -344,9 +344,9 @@
     def getidentifier(self, space):
         return space.newint(self.cdll.getidentifier())
 
- at unwrap_spec(name='fsencode_or_none', mode=int)
-def descr_new_cdll(space, w_type, name, mode=-1):
-    return W_CDLL(space, name, mode)
+ at unwrap_spec(name='fsencode_or_none', mode=int, handle=int)
+def descr_new_cdll(space, w_type, name, mode=-1, handle=0):
+    return W_CDLL(space, name, mode, handle)
 
 
 W_CDLL.typedef = TypeDef(
@@ -359,13 +359,13 @@
     )
 
 class W_WinDLL(W_CDLL):
-    def __init__(self, space, name, mode):
-        W_CDLL.__init__(self, space, name, mode)
+    def __init__(self, space, name, mode, handle):
+        W_CDLL.__init__(self, space, name, mode, handle)
         self.flags = libffi.FUNCFLAG_STDCALL
 
- at unwrap_spec(name='fsencode_or_none', mode=int)
-def descr_new_windll(space, w_type, name, mode=-1):
-    return W_WinDLL(space, name, mode)
+ at unwrap_spec(name='fsencode_or_none', mode=int, handle=int)
+def descr_new_windll(space, w_type, name, mode=-1, handle=0):
+    return W_WinDLL(space, name, mode, handle)
 
 
 W_WinDLL.typedef = TypeDef(
@@ -380,4 +380,4 @@
 # ========================================================================
 
 def get_libc(space):
-    return W_CDLL(space, get_libc_name(), -1)
+    return W_CDLL(space, get_libc_name(), -1, 0)
diff --git a/rpython/rlib/libffi.py b/rpython/rlib/libffi.py
--- a/rpython/rlib/libffi.py
+++ b/rpython/rlib/libffi.py
@@ -434,11 +434,12 @@
 
 # XXX: it partially duplicate the code in clibffi.py
 class CDLL(object):
-    def __init__(self, libname, mode=-1):
+    def __init__(self, libname, mode=-1, lib=0):
         """Load the library, or raises DLOpenError."""
-        self.lib = rffi.cast(DLLHANDLE, 0)
-        with rffi.scoped_str2charp(libname) as ll_libname:
-            self.lib = dlopen(ll_libname, mode)
+        self.lib = rffi.cast(DLLHANDLE, lib)
+        if lib == 0:
+            with rffi.scoped_str2charp(libname) as ll_libname:
+                self.lib = dlopen(ll_libname, mode)
 
     def __del__(self):
         if self.lib:


More information about the pypy-commit mailing list