[pypy-svn] r54694 - in pypy/dist/pypy/lib: _ctypes app_test/ctypes_tests
fijal at codespeak.net
fijal at codespeak.net
Tue May 13 13:22:06 CEST 2008
Author: fijal
Date: Tue May 13 13:22:03 2008
New Revision: 54694
Modified:
pypy/dist/pypy/lib/_ctypes/primitive.py
pypy/dist/pypy/lib/app_test/ctypes_tests/_ctypes_test.c
pypy/dist/pypy/lib/app_test/ctypes_tests/test_callbacks.py
Log:
somewhat lame support for pyobjs. Leak memory right now.
Modified: pypy/dist/pypy/lib/_ctypes/primitive.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/primitive.py (original)
+++ pypy/dist/pypy/lib/_ctypes/primitive.py Tue May 13 13:22:03 2008
@@ -1,4 +1,5 @@
import _rawffi
+import weakref
SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOv"
@@ -34,6 +35,20 @@
DEFAULT_VALUE = object()
+class GlobalPyobjContainer(object):
+ def __init__(self):
+ self.objs = []
+
+ def add(self, obj):
+ num = len(self.objs)
+ self.objs.append(weakref.ref(obj))
+ return num
+
+ def get(self, num):
+ return self.objs[num]()
+
+pyobj_container = GlobalPyobjContainer()
+
def generic_xxx_p_from_param(self, value):
from _ctypes import Array, _Pointer
if value is None:
@@ -182,6 +197,14 @@
return self._buffer[0]
result.value = property(_getvalue, _setvalue)
+ elif tp == 'O':
+ def _setvalue(self, val):
+ num = pyobj_container.add(val)
+ self._buffer[0] = num
+ def _getvalue(self):
+ return pyobj_container.get(self._buffer[0])
+ result.value = property(_getvalue, _setvalue)
+
return result
from_address = cdata_from_address
Modified: pypy/dist/pypy/lib/app_test/ctypes_tests/_ctypes_test.c
==============================================================================
--- pypy/dist/pypy/lib/app_test/ctypes_tests/_ctypes_test.c (original)
+++ pypy/dist/pypy/lib/app_test/ctypes_tests/_ctypes_test.c Tue May 13 13:22:03 2008
@@ -160,6 +160,11 @@
return (*func)(table);
}
+EXPORT(int) _testfunc_callback_opaque(int (*func)(void*), void* arg)
+{
+ return (*func)(arg);
+}
+
#ifdef HAVE_LONG_LONG
EXPORT(LONG_LONG) _testfunc_q_bhilfdq(signed char b, short h, int i, long l, float f,
double d, LONG_LONG q)
@@ -526,3 +531,4 @@
inp.y = inp.x * 10000;
return inp;
}
+
Modified: pypy/dist/pypy/lib/app_test/ctypes_tests/test_callbacks.py
==============================================================================
--- pypy/dist/pypy/lib/app_test/ctypes_tests/test_callbacks.py (original)
+++ pypy/dist/pypy/lib/app_test/ctypes_tests/test_callbacks.py Tue May 13 13:22:03 2008
@@ -191,3 +191,18 @@
assert res == [1,2,3,4,5]
+ def test_pyobject_as_opaque(self):
+ import conftest
+ _ctypes_test = str(conftest.sofile)
+ dll = CDLL(_ctypes_test)
+
+ def callback(arg):
+ return arg()
+
+ CTP = CFUNCTYPE(c_int, py_object)
+ cfunc = dll._testfunc_callback_opaque
+ cfunc.argtypes = [CTP, py_object]
+ cfunc.restype = c_int
+ res = cfunc(CTP(callback), lambda : 3)
+ assert res == 3
+
More information about the Pypy-commit
mailing list