How do I get string representation of PyObject in Python3?
python, python-3.x, python-c-api
Solution
Both `PyObject_Repr` and `PyObject_ASCII` return a unicode `str` representation of the object. If your terminal doesn't use UTF-8, you probably want the ASCII escaped representation. Get the multibyte string with `PyUnicode_AsUTF8` or `PyUnicode_AsUTF8AndSize`. The new implementation in 3.3 caches the UTF-8 byte string, so don't deallocate it after printing.
As to `PyString_AsString`, that became `PyBytes_AsString` or `PyBytes_AS_STRING` for the 3.x `bytes` type.
Problem
I am debugging Python source code (CPython 3.4). I'm receiving a PyObject*. I would like to `printf` it. For example (from Objects/floatobject.c of Python 3.4 source code): ``` PyObject *o_ndigits = NULL; Py_ssize_t ndigits; x = PyFloat_AsDouble(v); if (!PyArg_ParseTuple(args, "|O", &o_ndigits)) return NULL; ``` How do I `printf` o_ndigits? It is almost same as this question: Python: get string representation of PyObject?, but for Python3. With original solution, I got `"undefined reference to PyString_AsString"` error.