Cython - convert wide string (wchar_t *) to Python

2019-08-08 18:55发布

问题:

I'm wrapping a C library to Pyhon 3 using Cython and i'm looking for a way of converting wchar_t string to python object which i want to return from a function. There's an answer in this question, but it involves encoding the string as multibyte str, and decoding it back to unicode. I hope for more straightforward solution. I tried using PyUnicode_FromWideChar from Python C API, but i'm getting a segfault. Here's my .pyx code:

from cpython.ref cimport PyObject 
from libc.stddef cimport wchar_t

cdef extern from "Python.h":
    PyObject* PyUnicode_FromWideChar(wchar_t *w, Py_ssize_t size)

cdef extern from "../../src/my_c_lib.h":
    wchar_t * myObjToStr(wchar_t * result, size_t size, myObj * obj)


cdef class MyClass:
    cdef myObj * ptr
    ...
    def to_str(self):
        cdef wchar_t buf[64]

        cdef wchar_t * result = myObjToStr(buf, 64, self.ptr)
        if result is NULL:
            raise Exception("Error converting object to string.")

        cdef PyObject * pystr = PyUnicode_FromWideChar(result, 64)
        return <object>pystr

result is actually the pointer to buf. What's wrong with this? Is there another way without encoding/decoding?

Edit: I found that PyUnicode_FromWideChar() returns NULL, but why? I checked, that result is a valid wchar_t * string.

回答1:

Using -1 as second argument to PyUnicode_FromWideChar(result, -1)(so that the function uses wcslen internally), fixed the problem.

So PyUnicode_FromWideChar really is the way to go.