Character strings / Python 3.0 C API

R

rch.astra

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,

Cheers

RC
 
M

Martin v. Löwis

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
 
R

rch.astra

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
 
M

Martin v. Löwis

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

Ah, right. PyString_AsUTF8String returns a bytes object, to which you
need to apply PyBytes_AsString to. At the end, you need to decref the
bytes object.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top