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

Discussion in 'Python' started by sndive@gmail.com, Jan 24, 2007.

  1. Guest

    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!
    , Jan 24, 2007
    #1
    1. Advertising

  2. schrieb:
    > 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
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Jan 24, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Helmut Jarausch

    Py_BuildValue format code for unsigned int ?

    Helmut Jarausch, Feb 25, 2004, in forum: Python
    Replies:
    1
    Views:
    440
    Mike C. Fletcher
    Feb 25, 2004
  2. Christian Vogel
    Replies:
    2
    Views:
    3,716
    Christian Vogel
    Feb 25, 2004
  3. Replies:
    2
    Views:
    432
    John Machin
    Jul 7, 2005
  4. Martin Kulas

    Py_BuildValue("I", ...) does not work

    Martin Kulas, Aug 21, 2006, in forum: Python
    Replies:
    2
    Views:
    508
    Martin Kulas
    Aug 21, 2006
  5. Sheldon

    Py_BuildValue or PyList_SetItem()

    Sheldon, Jan 3, 2007, in forum: Python
    Replies:
    2
    Views:
    613
    Sheldon
    Jan 3, 2007
Loading...

Share This Page