How do I read a C char array into a python bytearray with cython?

时光总嘲笑我的痴心妄想 提交于 2019-12-24 09:17:34

问题


I have an array with bytes and its size:

cdef char *bp
cdef size_t size

How do I read the array into a Python bytearray (or another appropriate structure that can easily be pickled)?


回答1:


Three reasonably straightforward ways to do it:

  1. Use the appropriate C API function as I suggested in the comments:

     from cpython.bytes cimport PyBytes_FromStringAndSize
    
     output = PyBytes_FromStringAndSize(bp,size)
    

    This makes a copy, which may be an issue with a sufficiently large string. For Python 2 the functions are similarly named but with PyString rather than PyBytes.

  2. View the char pointer with a typed memoryview, get a numpy array from that:

    cdef char[::1] mview = <char[:size:1]>(bp)
    output = np.asarray(mview)
    

    This shouldn't make a copy, so could be more efficient if large.

  3. Do the copy manually:

     output = bytearray(size)
     for i in range(size):
         output[i] = bp[i]
    

    (this could be somewhat accelerated with Cython if needed)


This issue I think you're having with ctypes (based on the subsequent question you linked to in the comments) is that you cannot pass C pointer to the ctypes Python interface. If you try to pass a char* to a Python function Cython will try to convert it to a string. This fails because it stops at the first 0 element (hence you need size). Therefore you aren't passing ctypes a char*, you're passing it a nonsense Python string.



来源:https://stackoverflow.com/questions/50228544/how-do-i-read-a-c-char-array-into-a-python-bytearray-with-cython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!