extracting null pointer address from PyCObject with ctypes

Gordon Allott gordallott at gmail.com
Sat Oct 11 02:59:41 CEST 2008

Aaron "Castironpi" Brady wrote:

> Yes, well said.  But no, not true, not necessarily.  You can choose/
> change return types with your code.  If the call is defined already
> and you can't change the return, just define a new one that returns
> long.
> --
> http://mail.python.org/mailman/listinfo/python-list

the problem is that the pointer or long or whatever it is thats returned
won't be the data I am after. the code for PyCObject_FromVoidPtr is as

PyObject *
PyCObject_FromVoidPtr(void *cobj, void (*destr)(void *))
    PyCObject *self;

    self = PyObject_NEW(PyCObject, &PyCObject_Type);
    if (self == NULL)
        return NULL;

    return (PyObject *)self;

it obviously creates a new PyObject and returns that, which has already
happened once (the address I am after is passed to python via
PyCObject_FromVoidPtr(adress_i_am_after, NULL), doing that puts the
address I am after into the .cobject attribute of a new pyobject
structure and passes that to the python script via the 'display' key in
a dictionary.

If I were to then take the pycobject in this display key and pass it via
ctypes into PyCObject_FromVoidPtr it would simply create a new pycobject
 and put a pointer to the old pycobject in the new pycobject's .cobject
attribute. it just means that I am getting further and further away from
where I want to be surely? if I were to take the current pointer at this
stage, to get to the address I actually want in C it would have to
follow something along the lines of
long address_i_want = (long)(new_pycobj->cobject->cobject);

What would be great is if there is some easy simple way of accessing the
.cobject attribute of the first pycobject thats passed via the
dictionary to python.

Gord Allott (gordallott at gmail.com)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20081011/23b1b92d/attachment.pgp>

More information about the Python-list mailing list