Re: Sharing a C pointer between Pyrex objects
"Anand Patil" <[email protected]> Thu, 8 Jan 2009 18:16:00 +0000
| Newsgroups | gmane.comp.python.pyrex |
|---|---|
| Message-ID | <[email protected]> |
It really works! Thanks! On Thu, Jan 8, 2009 at 5:01 PM, John Arbash Meinel <[email protected]>wrote: > -----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----- > _______________________________________________ Pyrex mailing list [email protected] http://lists.copyleft.no/mailman/listinfo/pyrex