Looking to access C array in numpy.
-----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? - ------------------------------ Trevor R.H. Clarke tclarke@ball.com Ball Aerospace & Technologies Corp GPG key available on random.sks.keyserver.penguin.de -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (MingW32) iD8DBQFHlkvo+xUTKUxH/LkRAuzCAJ4ojQRgMgQj6vbhnSU3pxfiilIRYACePqiR hZyD05z1CgJuOtzw7pdJ9TQ= =H+HS -----END PGP SIGNATURE----- This message and any enclosures are intended only for the addressee. Please notify the sender by email if you are not the intended recipient. If you are not the intended recipient, you may not use, copy, disclose, or distribute this message or its contents or enclosures to any other person and any such actions may be unlawful. Ball reserves the right to monitor and review all messages and enclosures sent to or from this email address.
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 numpy.frombuffer. 2) Use the "intentionally not well publicisized function" numpy.core.multiarray.int_asbuffer 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=) where 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). -Travis O.
Travis E. Oliphant wrote:
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 numpy.frombuffer.
Specifically: res = ctypes.ARRAY(ctypes.c_bytes, sizeofbuffer).from_address(void_ptr_as_long) array = np.frombuffer(res, dtype=mydtype)
2) Use the "intentionally not well publicisized function" numpy.core.multiarray.int_asbuffer
res = numpy.core.multiarray.int_asbuffer(void_ptr_as_long, sizeofbuffer, readonly=False, check=False) array = np.frombuffer(res, dtype=mydtype) Have fun! -Travis O.
participants (2)
-
Clarke, Trevor -
Travis E. Oliphant