[C++-sig] boost::python, PyObject* members, and cyclic references

Albert Strasheim fullung at gmail.com
Thu Jun 21 23:19:03 CEST 2007


Hello all

----- Original Message ----- 
From: "Jim Bosch" <jfbosch at ucdavis.edu>
To: <c++-sig at python.org>
Sent: Thursday, June 21, 2007 10:49 PM
Subject: [C++-sig] boost::python, PyObject* members, and cyclic references


>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.

You might be interested in this code for some inspiration:

http://pyspkrec.googlecode.com/svn/trunk/numpycpp/

It implements __array_struct__ for some uBLAS and multi_array stuff and has 
some ideas for automatically converting a wrapper object to a NumPy array 
and vice versa when returning from C++ or calling into C++, respectively.

__array_struct__ is described here:

http://numpy.scipy.org/array_interface.shtml

Cheers,

Albert 




More information about the Cplusplus-sig mailing list