Unicode problem in ucs4

abhi abhigyan_agrawal at in.ibm.com
Mon Mar 23 03:18:19 EDT 2009


On Mar 20, 5:47 pm, "M.-A. Lemburg" <m... at egenix.com> wrote:
> On 2009-03-20 12:13, abhi wrote:
>
>
>
>
>
> > On Mar 20, 11:03 am, "Martin v. Löwis" <mar... at v.loewis.de> wrote:
> >>> Any idea on why this is happening?
> >> Can you provide a complete example? Your code looks correct, and should
> >> just work.
>
> >> How do you know the result contains only 't' (i.e. how do you know it
> >> does not contain 'e', 's', 't')?
>
> >> Regards,
> >> Martin
>
> > Hi Martin,
> >  Here is the code:
> > unicodeTest.c
>
> > #include<Python.h>
>
> > 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);
> >       wprintf(L"database value after unicode conversion is : %s\n",
> > sample);
>
> You have to use PyUnicode_AsWideChar() to convert a Python
> Unicode object to a wchar_t representation.
>
> Please don't make any assumptions on what Py_UNICODE maps
> to and always use the the Unicode API for this. It is designed
> to provide a portable interface and will not do more conversion
> work than necessary.
>
>
>
>
>
> >       return Py_BuildValue("");
> > }
>
> > static PyMethodDef funcs[]={{"unicodeTest",(PyCFunction)
> > unicode_helper,METH_VARARGS,"test ucs2, ucs4"},{NULL}};
>
> > void initunicodeTest(void){
> >    Py_InitModule3("unicodeTest",funcs,"");
> > }
>
> > When i install this unicodeTest on python ucs2 wprintf prints whatever
> > is passed eg
>
> > import unicodeTest
> > unicodeTest.unicodeTest("hello world")
> > database value after unicode conversion is : hello world
>
> > but it prints the following on ucs4 configured python:
> > database value after unicode conversion is : h
>
> > Regards,
> > Abhigyan
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> --
> Marc-Andre Lemburg
> eGenix.com
>
> Professional Python Services directly from the Source  (#1, Mar 20 2009)>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
> >>> mxODBC.Zope.Database.Adapter ...            http://zope.egenix.com/
> >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
>
> ________________________________________________________________________
>
> ::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
>
>    eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
>     D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
>            Registered at Amtsgericht Duesseldorf: HRB 46611
>                http://www.egenix.com/company/contact/- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

Hi Mark,
     Thanks for the help. I tried PyUnicode_AsWideChar() but I am
getting the same result i.e. only the first letter.

sample code:

#include<Python.h>

static PyObject *unicode_helper(PyObject *self,PyObject *args){
        PyObject *sampleObj = NULL;
        wchar_t *sample = NULL;
        int size = 0;

      if (!PyArg_ParseTuple(args, "O", &sampleObj)){
                return NULL;
      }

         // use wide char function
      size = PyUnicode_AsWideChar(databaseObj, sample,
PyUnicode_GetSize(databaseObj));
      printf("%d chars are copied to sample\n", size);
      wprintf(L"database value after unicode conversion is : %s\n",
sample);
      return Py_BuildValue("");

}


static PyMethodDef funcs[]={{"unicodeTest",(PyCFunction)
unicode_helper,METH_VARARGS,"test ucs2, ucs4"},{NULL}};

void initunicodeTest(void){
        Py_InitModule3("unicodeTest",funcs,"");

}

This prints the following when input value is given as "test":
4 chars are copied to sample
database value after unicode conversion is : t

Any ideas?

-
Abhigyan



More information about the Python-list mailing list