How to wrap a C function whose parameter is wchar_t pointer with cython

放肆的年华 提交于 2020-01-05 05:25:34

问题


I want to use cython to wrap a C library. One function in the library is like

int hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen);

There are two questions:

  1. What can I do with the wchar_t in cython;

  2. How to convert the string pointer in my .pyx file.


回答1:


Declare wchar_t:

cdef extern from "stddef.h":
    ctypedef void wchar_t

Or import from libc module:

from libc.stddef cimport wchar_t

A function to convert wchar_t to python string by using WideCharToMultiByte (see CefStringToPyString):

# Declare these in .pxd file:
#
# cdef extern from "Windows.h":
#     cdef int CP_UTF8
#     cdef int WideCharToMultiByte(int, int, wchar_t*, int, char*, int, char*, int*)

cdef object WideCharToPyString(wchar_t *wcharstr):
    cdef int charstr_bytes = WideCharToMultiByte(CP_UTF8, 0, wcharstr, -1, NULL, 0, NULL, NULL)
    # Do not use malloc, otherwise you get trash data when string is empty.
    cdef char* charstr = <char*>calloc(charstr_bytes, sizeof(char))
    cdef int copied_bytes = WideCharToMultiByte(CP_UTF8, 0, wcharstr, -1, charstr, charstr_bytes, NULL, NULL)
    if bytes == str:
        pystring = "" + charstr # Python 2.7
    else:
        pystring = (b"" + charstr).decode("utf-8", "ignore") # Python 3
    free(charstr)
    return pystring

Since Python 3.2 you can do this with the use of PyUnicode_FromWideChar(wcharstr, -1), see the comment by compostus.



来源:https://stackoverflow.com/questions/12778832/how-to-wrap-a-c-function-whose-parameter-is-wchar-t-pointer-with-cython

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