我有一个PyUnicode对象,我正试图转换回一个C字符串(char*)。在
我尝试的方法似乎不起作用。这是我的代码:
PyObject * objectCompName = PyTuple_GET_ITEM(compTuple, (Py_ssize_t) 0);
PyObject * ooCompName = PyUnicode_AsASCIIString(objectCompName);
char * compName = PyBytes_AsString(ooCompName);
Py_DECREF(ooCompName);
我还有别的/更好的方法吗?在
如果UTF-8编码的} (这需要python3.3):
char *
是正常的,那么您肯定应该使用^{另外,请务必了解,必须检查代码中执行的每个
Py*
函数调用的返回值。在这里,},如果}导致{}。^如果}将返回
PyTuple_GetItem
将返回{compTuple
不是tuple
,或者{objectCompName
不是str
对象,则{NULL
。忽略返回值,当条件正确时,SIGSEGV
会导致CPython崩溃。在您需要首先将python PyUnicode转换为非unicode的python字符串(请阅读更多信息:https://docs.python.org/2/c-api/unicode.html#ascii-codecs),然后可以轻松地将结果转换为
char*
。在下面是一个伪代码,可帮助您继续:
相关问题 更多 >
编程相关推荐