arj*_*kok 6 python python-c-api python-3.x
我正在调试Python源代码(CPython 3.4).我正在收到PyObject*.我想printf.
例如(来自Python 3.4的Objects/floatobject.c源代码):
PyObject *o_ndigits = NULL;
Py_ssize_t ndigits;
x = PyFloat_AsDouble(v);
if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
return NULL;
Run Code Online (Sandbox Code Playgroud)
我如何printfo_ndigits?它与这个问题几乎相同:Python:获取PyObject的字符串表示形式?,但对于Python3.使用原始解决方案,我收到了"undefined reference to PyString_AsString"错误.
和 都PyObject_Repr返回对象的PyObject_ASCIIunicode表示形式。str如果您的终端不使用 UTF-8,您可能需要 ASCII 转义表示。PyUnicode_AsUTF8使用或获取多字节字符串PyUnicode_AsUTF8AndSize。3.3 中的新实现会缓存 UTF-8 字节字符串,因此在打印后不要释放它。
至于PyString_AsString,则成为3.x 类型的PyBytes_AsString或。PyBytes_AS_STRINGbytes