Character strings / Python 3.0 C API

rch.astra at rch.astra at
Wed Apr 8 23:28:36 CEST 2009

Hi everybody,

I'm doing some experiments with the python 3.0 C API, and I'm having
some problems for obtaining character strings. In python 2.X APIs, a
function called PyString_AsString was available. This function
provided a C string (char*) given a PyObject. But, in python 3.0 API
there is no such a function, and they mention that it has been
substituted by PyUnicode* functions. The issue is that I still don't
get how to use these functions to obtain char strings in C!

This is one of the things I try to do:

    PyThreadState* state = PyThreadState_GET();
    PyInterpreterState* interp = state->interp;
    int numModules = PyDict_Size(interp->modules);

    PyObject* moduleKeys = PyDict_Keys(interp->modules);
    int idx=0;
    for(idx=0; idx<numModules; idx++)
        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...

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

Thank you very much in advance,



More information about the Python-list mailing list