[Python-Dev] Py_UNICODE madness

"Martin v. Löwis" martin at v.loewis.de
Thu May 5 00:00:34 CEST 2005


Nicholas Bastin wrote:
> That makes PyUnicode_FromUnicode() a lot less useful.  Well, really, 
> not useful at all.

Why do you say that? Py_UNICODE is as good a type to store characters
as any other, and if you happen to have a Py_UNICODE[], you can use
that function to build a unicode object.

> You might suggest that PyUnicode_FromWideChar is more useful, but 
> that's only true on platforms that support wchar_t.

Useful to do what? PyInt_FromLong isn't useful if you have a void*,
either...

> Is there no universally supported way of moving buffers of unicode data 
> (as common data types, like unsigned short, etc.) into Python from C?

There is no common Unicode type in C, period (be it Python or not).
Your best bet is to prepare a Py_UNICODE[], either by copying from
your favourite Unicode type, or by casting it, e.g.

#if Py_UNICODE_IS_AS_WIDE_AS_MY_UNICODE_TYPE

   Py_UNICODE* data = (Py_UNICODE*) my_data;
   do_free=0;

#else

   Py_UNICODE* data = malloc(sizeof(Py_UNICODE)*my_data_len);
   for(int i=0;i<=my_data_len)
       data[i] = my_data[i];
   do_free=1;
#endif

  PyObject *uni = PyUnicode_FromUnicode(data);
  if(do_free)free(data);

Regards,
Martin


More information about the Python-Dev mailing list