[Pyrex] Sharing a C pointer between Pyrex objects
John Arbash Meinel
john at arbash-meinel.com
Thu Jan 8 18:01:41 CET 2009
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Anand Patil wrote:
> Hi all,
>
> I'd like to make an int* pointer stored in one Pyrex object available to
> another:
>
> cdef class NumberHolder:
> cdef int number
> def __init__(self):
> self.number = 0
>
> cdef class PointerHolder:
> cdef int* my_pointer
> def __init__(self, number_holder):
> my_pointer = number_holder.number
>
> If I try it this way, of course, I get errors like 'NumberHolder' object
> has no attribute 'number'. I'm currently creating a NumPy array in
> NumberHolder and using PyArray_DATA from PointerHolder, but it seems
> excessive and possibly dangerous, since PointerHolder may change
> *my_pointer later. What's the best way to do this?
>
> Thanks,
> Anand
>
I believe you can do:
cdef class PointerHolder:
cdef int* my_pointer
def __init__(self, number_holder):
cdef NumberHolder real_holder
real_holder = number_holder
self.my_pointer = &real_holder.number
You need to indicate to pyrex that number_holder is really a
NumberHolder object, and not just a generic python object. Further, I'm
pretty sure the & is necessary, since you want to point to the int, and
not point at whatever the int points at.
John
=:->
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEARECAAYFAklmMXUACgkQJdeBCYSNAAO2ygCfSsARbUIzUJODVy3moKdFCCJ2
638AoKAyWfJ8vMo2l1oRUUgV1x8hbQEM
=owSx
-----END PGP SIGNATURE-----
More information about the Pyrex
mailing list