[pypy-issue] [issue1233] str() types can't be used with ctypes's py_object()

Marcus von Appen tracker at bugs.pypy.org
Mon Aug 27 19:56:02 CEST 2012


Marcus von Appen <mva at sysfault.org> added the comment:

The function is a C callback, which passes user data through on receiving 
events.

1) register callback with user data payload via CFUNCTYPE() capsule
2) Some C code executes
3) C code fires event, causing the (Python) callback to be raised
4) Python callback is invoked with the payload that was registered as argument.

Since the user can use an arbitrary payload in the callback, casting the data to 
c_void_p would require a safe conversion for each and every Python object and 
Python type, which - to my knowledge - is not supported by ctypes (at least in 
CPython).

py_object() comes in handy, when dealing with e.g. object()-based class 
instances, lists, dictionaries, ... to be used as user data argument.
Thus, wrapping a base type like str, int, ... into a py_object() would ease 
callback interactions with void* pass-throughs.

You can see such an example at 
http://code.google.com/p/pgreloaded/source/browse/pygame2/sdl/timer.py and 
http://code.google.com/p/pgreloaded/source/browse/pygame2/sdl/log.py

________________________________________
PyPy bug tracker <tracker at bugs.pypy.org>
<https://bugs.pypy.org/issue1233>
________________________________________


More information about the pypy-issue mailing list