Problems in Using C-API for Unicode handling

abhi abhigyan_agrawal at
Tue Jan 13 08:03:32 CET 2009

    I am trying to handle Unicode objects in C (Python 2.5.2). I am
getting PyObjects from and want to coerce them to unicode objects. The
documentation provides two APIs for that:

 PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding,
const char *errors)
 PyUnicode_FromObject(PyObject *obj)

Now I want to utf-16 so I am trying to use the first one, but it is
giving back NULL in case of PyObject is already Unicode type which is
expected. What puzzles me is that PyUnicode_FromObject(PyObject *obj)
is passing irrespective of type of PyObject. The API says it is
Shortcut for PyUnicode_FromEncodedObject(obj, NULL, "strict") but if I
use that, it returns NULL where as PyUnicode_FromObject works.

Is there any way by which I can take in any PyObject and convert it to
utf-16 object? Any help is appreciated.


More information about the Python-list mailing list