Character strings / Python 3.0 C API

"Martin v. Löwis" martin at
Wed Apr 8 23:35:41 CEST 2009

>         PyObject* key = PyList_GetItem(moduleKeys,idx);
>         PyObject* module = PyDict_GetItem( interp->modules, key );
>         char* theKeyName = <????>
>         PySys_WriteStdout("Module '%s'\n", theKeyName);
>     }
> I was not able to obtain theKeyName, knowing that the "key" PyObject
> is in fact a unicode string containing the character string I want...

Use PyUnicode_AsUTF8String.

> Could anybody help me? How can I, in general, obtain char* strings
> from a corresponding PyObject? What should I use for "<????>"?

In general, this is much more difficult. If the object is an integer
(say), or a file object - what is the char* that you want to get?
You would need to call PyObject_Str first, getting str() (or repr())
of the object, and then use PyUnicode_AsUTF8String (releasing
the _Str result afterwards).


More information about the Python-list mailing list