[Numpy-discussion] NumPy wrapper for C output buffer of unknown size

Mark Heslep mark at mitre.org
Fri Mar 10 18:15:01 CST 2006

I'm trying to come up with a _generic_, _reusable_ numpy wrapper for a C 
output buffer of unknown size.  That is, only the buffer pointer is 
available from a function call, and it's size is available elsewhere.  
For example, the buffer access might look like this:

char * func_returns_ptr_to_allocated_buffer() { ...}
And the size of the buffer is determined elsewhere by some other 

I'm using Swig* here, and left to itself Swig will neatly map the char* 
to a  Swig Python object, something like <Swig Object __p__char...>.  
That's fine unless you want access to the buffer elements via NumPy.  
I'm familiar with many of the fine NumPy typemaps posted here and on the 
net, but they all assume a known array size and use a 
PyArray_FromDimsandData (...SimpleFrom... w/ recent Numpy) with a dims 
argument provided.  Instead, is there some method where I can return a 
numpy array of size zero (one?) that effectively captures the data 
pointer and then allows me to resize on the Python side?  I note the the 
restrictions on the NumPy resize method:  no previous reference, and 

Its simple to solve this with a helper function custom to this 
application but its not maintainable with the app-library as it 
evolves.  Extending the solution to any other data type is also no 
problem.  So, as stated above, is there a generic, numpy-thonic way to 
solve this? 


* Im open to other methods -ctypes, etc

More information about the Numpy-discussion mailing list