Passing back an array from an extension module?
Martin von Loewis
loewis at informatik.hu-berlin.de
Fri Jun 8 13:34:03 EDT 2001
"Bob Greschke" <bob at passcal.nmt.edu> writes:
> Then I need to pass that back to the Python side for graphing
> the values. I initially thought I could just create a tuple and pass
> that back, since nothing is going to be done with the values except to
> draw a graph, but there is the possibility of there being 16,000,000
> data points...that makes for a slightly large statement, doesn't it?
> It doesn't look like I can do this with arrays in stock Python, but it
> does look like I can do it with the NumPy arrays. Is that correct?
Not sure what 'this' is here. You certainly can create 'custom' array
types in Python, see Modules/arraymodule for an example of an array
type that is implemented using a C memory buffer of primitive C
values.
I don't know whether you can use a NumPy array off-the-shelf; I assume
no since it probably manages its own memory.
So you need to create a new type (see xxmodule.c for an example), and
implement its tp_as_sequence slot.
> Now what about Py_DECREF, INCREF and all of that stuff in these
> situations? I read through a number of posts on dejagoogle that were
> talking about this, but they only confused me more. :-)
I recommend you read the "Embedding and Extending
tutorial". Basically, you need to implement an array accessor, which
creates and returns a PyInt_FromLong every time you access the nth
element of your array. When the reference counter of your Python
wrapper object drops to zero, you should free(3) the memory of the
very large array.
Regards,
Martin
More information about the Python-list
mailing list