[Numpy-discussion] Looking to access C array in numpy.
Travis E. Oliphant
Tue Jan 22 14:30:41 CST 2008
Clarke, Trevor wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> I'm calling some python code from a C++ app via an intermediary library
> (i.e. I can't directly create Python C objects like Buffers). I'm
> passing a void* (cast to a long) to the python method and I'd like to
> use numpy to access that memory as an array. I'll know what the C data
> type is and the minimum length of the data. It's a continuous array but
> is multiple dimensions (either 2 dimensional or 3 dimensional BIP). I
> was able to find a may to create an array accessor over a python buffer
> but I'm not sure how to back an array with a void* (as a long) or create
> a buffer object for that memory inside python. Could someone point me in
> the right direction?
A couple of options that I see:
1) Use ctypes to wrap your void* into an object that can be passed into
2) Use the "intentionally not well publicisized function"
The latter function is not well publicisized because I think solution #1
if you can figure it out may be better long term. But, #2 has some
interesting features like being able to classify the memory as read-only
and being able to check to see if de-referencing the beginning and end
of the region causes a segfault (which is caught on some platforms) and
handled gracefully with a Python error.
The calling syntax is
int_asbuffer(mem=, size=, readonly=, check=)
mem: long integer representing a void *
size: The size of the memory region being converted to a buffer object.
readonly: Should this memory object be readonly or not (default FALSE)
check: Should the routine try to de-reference the first and last byte
of the memory to catch any segfault? (default TRUE).
More information about the Numpy-discussion