How Does String Conversion Between PyUnicode String and C String Work?
If UTF-8 encoded char *
is OK, you should definitely use PyUnicode_AsUTF8AndSize
(which requires Python 3.3):
PyObject * objectCompName = PySequence_GetItem(compTuple, 0);
if (! objectCompName) {
return NULL;
}
Py_ssize_t size;
char *ptr = PyUnicode_AsUTF8AndSize(objectCompName, &size);
if (!ptr) {
return NULL;
}
// notice that the string pointed to by ptr is not guaranteed to stay forever,
// and you need to copy it, perhaps by `strdup`.
Also, do understand that is mandatory to check the return value of each and every Py*
function call that you ever execute in your code.
Here the PyTuple_GetItem
will return NULL
if compTuple
is not a tuple
, or 0
causes IndexError
. PyUnicode_AsUTF8AndSize
will return NULL
if objectCompName
is not a str
object. Ignore the return value and CPython crashes with SIGSEGV
when the conditions are right.