问题描述
我正在使用Cython将C库包装到Pyhon 3,并且我正在寻找一种将wchar_t字符串转换为要从函数返回的python对象的方法。在,但是它涉及将字符串编码为多字节str,然后将其解码回unicode。我希望有更直接的解决方案。我尝试从Python C API使用,但是我我遇到了段错误。这是我的.pyx代码:
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
结果
实际上是指向<$的指针c $ c> buf 。怎么了有没有编码/解码的另一种方法吗?
result
is actually the pointer to buf
. What's wrong with this? Is there another way without encoding/decoding?
编辑:我发现 PyUnicode_FromWideChar()
返回NULL,但是为什么?我检查了结果
是有效的wchar_t *字符串。
I found that PyUnicode_FromWideChar()
returns NULL, but why? I checked, that result
is a valid wchar_t * string.
推荐答案
使用-1作为 PyUnicode_FromWideChar(result,-1)
的第二个参数(以便该函数在内部使用wcslen),解决了此问题。
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.
这篇关于Cython-将宽字符串(wchar_t *)转换为Python 3 unicode对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!