[pypy-svn] pypy jitypes2: explicitly set the address of a function after the lookup by name. This is

antocuni commits-noreply at bitbucket.org
Tue Jan 4 14:04:49 CET 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: jitypes2
Changeset: r40375:fc183ee7f362
Date: 2011-01-03 16:40 +0100
http://bitbucket.org/pypy/pypy/changeset/fc183ee7f362/

Log:	explicitly set the address of a function after the lookup by name.
	This is needed to be able to cast a function to voidp and back again
	to a function.

diff --git a/lib_pypy/_ctypes/function.py b/lib_pypy/_ctypes/function.py
--- a/lib_pypy/_ctypes/function.py
+++ b/lib_pypy/_ctypes/function.py
@@ -107,6 +107,14 @@
             restype = 'O' # void
         return argtypes, restype
 
+    def _set_address(self, address):
+        if not self._buffer:
+            self._buffer = _rawffi.Array('P')(1)
+        self._buffer[0] = address
+
+    def _get_address(self):
+        return self._buffer[0]
+
     def __init__(self, *args):
         self.name = None
         self._objects = {keepalive_key(0):self}
@@ -117,9 +125,9 @@
 
         if isinstance(argument, (int, long)):
             # direct construction from raw address
+            self._set_address(argument)
             argshapes, resshape = self._ffishapes(self._argtypes_, self._restype_)
-            self._address = argument
-            self._ptr = self._getfuncptr_fromaddress(self._address, argshapes, resshape)
+            self._ptr = self._getfuncptr_fromaddress(argshapes, resshape)
         elif callable(argument):
             # A callback into python
             self.callable = argument
@@ -136,8 +144,7 @@
                 self.dll = ctypes.CDLL(self.dll)
             # we need to check dll anyway
             ptr = self._getfuncptr([], ctypes.c_int)
-            #self._buffer = ptr.byptr()
-            self._buffer = None
+            self._set_address(ptr.getaddr())
 
         elif (sys.platform == 'win32' and
               len(args) >= 2 and isinstance(args[0], (int, long))):
@@ -152,7 +159,7 @@
         elif len(args) == 0:
             # Empty function object.
             # this is needed for casts
-            self._buffer = _rawffi.Array('P')(1)
+            self._set_address(0)
             return
         else:
             raise TypeError("Unknown constructor %s" % (args,))
@@ -254,7 +261,8 @@
         print 'unknown shape %s' % (shape,)
         assert False, 'TODO5'
 
-    def _getfuncptr_fromaddress(self, address, argshapes, resshape):
+    def _getfuncptr_fromaddress(self, argshapes, resshape):
+        address = self._get_address()
         ffiargs = [self._shape_to_ffi_type(shape) for shape in argshapes]
         ffires = self._shape_to_ffi_type(resshape)
         return _ffi.FuncPtr.fromaddr(address, '', ffiargs, ffires)
@@ -267,16 +275,8 @@
             restype = ctypes.c_int
         argshapes = [arg._ffiargshape for arg in argtypes]
         resshape = restype._ffiargshape
-        if self._address is not None:
-            ptr = self._getfuncptr_fromaddress(self._address, argshapes, resshape)
-            if argtypes is self._argtypes_:
-                self._ptr = ptr
-            return ptr
-
         if self._buffer is not None:
-            assert False, 'TODO'
-            ptr = _rawffi.FuncPtr(self._buffer[0], argshapes, resshape,
-                                  self._flags_)
+            ptr = self._getfuncptr_fromaddress(argshapes, resshape)
             if argtypes is self._argtypes_:
                 self._ptr = ptr
             return ptr


More information about the Pypy-commit mailing list