Reputation: 169
In C-extension of Python, I can use Py_BuildValue()
to Conver char string to Python Value like this: Py_BuildValue("s", str)
. But, if the string is wchar array, the Py_BuildValue("s", str)
can not be used.
I think I can use PyUnicode like this:
PyObject* pb=PyUnicode_FromUnicode( (const Py_UNICODE*)(str), wcslen(str) );
PyObject *val = Py_BuildValue("o", pb);
But, it does not work. How can I conver wchar string to Python Value?
Upvotes: 3
Views: 1344
Reputation: 3393
I think you don't need to combine PyUnicode_FromUnicode
and Py_BuildValue
.
The following pb
is already a Python unicode object.
PyObject* pb=PyUnicode_FromUnicode( (const Py_UNICODE*)(str), wcslen(str) );
Using Py_BuildValue
to process it is redundant, but it's harmless.
I think your problem is caused by the incorrect format o
. It should be O
.
You can also use Py_BuildValue
to convert a null-terminated buffer of Unicode (UCS-2 or UCS-4) data to a Python Unicode object.
For example:
const wchar_t *w = L"Hello world!";
PyObject* pb = Py_BuildValue("u", w);
More
Upvotes: 2