[Python-Dev] Extending types in C - help needed
Jack Jansen
jack@oratrix.com
Fri, 18 Jan 2002 11:24:01 +0100
On Friday, January 18, 2002, at 10:47 , M.-A. Lemburg wrote:
> Jack Jansen wrote:
>>
>> On Thursday, January 17, 2002, at 11:29 AM, M.-A. Lemburg wrote:
>>
>>> I am more in favour of
>>> exposing the pickle reduce API through "O@", that is
>>> have PyArgTuple_Parse() call the .__reduce__() method
>>> of the object. This will then return (factory, state_tuple)
>>> and these could then be exposed to the C function via two
>>> PyObject*.
>>
>> You've suggested this before, but at that time I ignored it
>> because it made absolutely no sense to me. "pickle" triggers one
>> set of ideas for me, "reduce" triggers a different set, "factory
>> function" yet another different set. None of these sets of ideas
>> have the least resemblance to what I'm trying to do:-)
>
> The idea is simple but extends what you are trying to
> achieve (I gave an example on how to use this somewhere
> in the "wrapper" thread). Basically, you'll just want to
> use the state tuple to access the underlying void* C pointer
> via a PyCObject which does the wrapping of the pointer.
> The "pickle" mechanism would store the PyCObject in the
> state tuple which you could then access to get at the
> C pointer.
>
I think you're missing a few points here. First of all, my objects
aren't PyCObjects but other extension objects. While the main pointer in
the object could be wrapped in a PyCObject there may be other
information in my objects that is important, such as a pointer to the
dispose routine to call on the c-pointer when the Python object reaches
refcount zero (and this pointer may change over time as ownership of,
say, a button is passed from Python to the system). The _New and
_Convert routines will know how to get from the C pointer to the
*correct* object, i.e. normally there will be only one Python object for
every C object.
Also, the method seems rather complicated for doing a simple thing. The
only thing I really want is a way to refer to an _New or _Convert method
from Python code. The most reasonable way to do that seems to be by
creating a way to get from te type object (which is available in Python)
to those routines. Thomas' suggestion looked very promising, and simple
too, until Guido said that unfortunately it couldn't be done. Your
suggestion, as far as I understand it, looks complicated and probably
inefficient too (remember the code will have to go through all these
hoops every time it needs to convert an object from Python to C or vice
versa).
Correct me if I'm wrong,