What's the reverse of Py_BuildValue("u#" ?

S

sndive

How could I get the pointer to and the length of ucs2 array out of a
PyObject representing
a string? Something that works whether PyObject string is in unicode or
not.

Also could I replace a sequence

if(PyBool_Check(obj)) {
....
}
if(PyString_Check(obj)) { // would this be true for any string
type?
....
}
if(PyFloat_Check(obj)) {
....

with a switch?

Thank you!
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

How could I get the pointer to and the length of ucs2 array out of a
PyObject representing a string? Something that works whether PyObject
string is in unicode or not.

You can use PyObject_Unicode(o) to convert the object to Unicode first,
then use PyUnicode_AsUnicode to convert it to a Py_UNICODE array, and
PyUnicode_GetSize to find out what the length is. Notice that this will
be UCS-2 only if Py_UNICODE is 16 bits on your platform. If you really
want UCS-2 always, you need to convert the string again using
PyUnicode_AsEncodedObject, then PyString_AsString to find out what
the UCS-2 bytes are.

Remember to check for errors for all these functions, and remember
to decref the results when you don't need them any longer.
Also could I replace a sequence

if(PyBool_Check(obj)) {
...
}
if(PyString_Check(obj)) { // would this be true for any string
type?
...
}
if(PyFloat_Check(obj)) {
...
with a switch?

Not easily. Also, PyString_Check is true only for the byte string
type (and its subtypes).

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top