Unicode problem in ucs4

abhi abhigyan_agrawal at in.ibm.com
Thu Mar 19 11:27:47 CET 2009

    I have a C extension, which takes a unicode or string value from
python and convert it to unicode before doing more operations on it.
The skeleton looks like:

static PyObject *unicode_helper( PyObject *self, PyObject *args){
      PyObject *sampleObj = NULL;
      Py_UNICODE *sample = NULL;

      if (!PyArg_ParseTuple(args, "O", &sampleObj)){
		return NULL;
      // Explicitly convert it to unicode and get Py_UNICODE value
      sampleObj = PyUnicode_FromObject(sampleObj);
      sample = PyUnicode_AS_UNICODE(sampleObj);
      // perform other operations.

This piece of code is working fine on python with ucs2 configuration
but fails with python ucs4 config. By failing, I mean that only the
first letter comes in variable sample i.e. if I pass "test" from
python then sample will contain only "t". However, PyUnicode_GetSize
(sampleObj) function is returning correct value (4 in this case).

Any idea on why this is happening? Any help will be appreciated.


More information about the Python-list mailing list