[C++-sig] boost::python, PyObject* members, and cyclic references
Jim Bosch
jfbosch at ucdavis.edu
Thu Jun 21 22:49:55 CEST 2007
I am trying to wrap a class in which I'd like to expose some of its data
as a numpy array, so in python it works like this:
>>> o = array_holder()
>>> a = o.array
>>> a[4:6] = 3.14
Rather than constructing the array every time, I'd like to store the
PyObject* (or something equivalent) in my array_holder wrapper, but I
need to tie the lifetimes of 'o' and 'a' together, so nothing bad
happens if someone does 'del o' while 'a' still exists.
The numpy array I'm constructing holds a reference to the PyObject that
owns its data (which I assume I can get from 'object(this).ptr()'), so
I'm not worried about that end. And I assume something like
'return_internal_reference' is what I'm looking for at the other end,
but I'm not quite sure how to work out the details (do I return a
'PyObject*', a 'handle', or an 'object'?)
Finally, there's the question of whether either of these will ever get
deleted at all - do boost::python wrapped objects participate in cyclic
gc? If not, is there any way I can manually add it for this class (by
suppling traverse and clear methods using the C API, for instance)?
Thanks!
Jim Bosch
More information about the Cplusplus-sig
mailing list