Character strings / Python 3.0 C API

rch.astra at googlemail.com rch.astra at googlemail.com
Wed Apr 8 23:58:36 CEST 2009


Sorry but it did not help... I tried this:

        PyObject* key = PyList_GetItem(moduleKeys,idx);
        char* theKeyName = PyUnicode_AsUTF8String( key );

And this, just in case:

        PyObject* key = PyList_GetItem(moduleKeys,idx);
        char* theKeyName = PyUnicode_AsUTF8String( PyObject_Str
(key) );

(Although it should work in the first case since key is suposed to be
a string already, the module name). In all the cases, I obtained an
empty string when using theKeyName:

PySys_WriteStdout("Module '%s'\n", theKeyName);

The issue is that all PyUnicode* functions are returning PyObjects.
PyString_AsString return value was char*. Is there any real equivalent
of this function?

Thanks again. Best regards,

RC

On 8 abr, 23:35, "Martin v. Löwis" <mar... at v.loewis.de> wrote:
> >         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).
>
> Regards,
> Martin




More information about the Python-list mailing list