Question about __array_struct__
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?
Sasha wrote:
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.
Normally, the array that's viewing the data held by the __array_struct__ should keep a reference to the base object alive, thus preventing the issue. If the base object isn't a Python object, you'll have to create some kind of Python type that will ensure the original data is not freed, although this would normally take place via refcounts if the data source was a Python object.
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.
Yes, that's I do. (See http://www.scipy.org/Cookbook/ArrayStruct_and_Pyrex for example.)
participants (2)
-
Andrew Straw
-
Sasha