[Numpy-discussion] Question about __array_struct__

Sasha ndarray at mac.com
Tue Apr 25 10:27:10 CDT 2006

I am trying to add __array_struct__ attribute to R object wrappers in
RPy.  This is desirable because it eliminates a compile-time
dependency on an array module and makes the binary compatible with
either Numeric or numpy.  R has four types of data: logical, integer,
float, and  character.  The first three map perfectly to Numpy with
inter->data simply pointing to an appropriate internal memory area. 
The character type, however is more problematic.  In R character
arrays are arrays of variable length strings and therefore similar to
Numpy object arrays holding python strings.  Obviously, there is no
memory area that can be reused.  I've tried to allocate new memory in 
 __array_struct__  getter, but this presents a problem:

I cannot deallocate that memory in CObject destructor because it is
passed to the newly created array which lives long after the interface
object is deleted. The __array_struct__ mechanism does not seem to
allow to cause the new array assume ownership of the data, but even if
it did, I do not know what memory allocator is appropriate.

The only solution that I can think of is to create a dummy buffer type
with the sole purpose of deleting an array of PyObjects and make an
instance of that type the "base" of the new array.

Can anyone suggest a better approach?

More information about the Numpy-discussion mailing list