[Python-Dev] Convert Py_Buffer to Py_UNICODE

Sijin Joseph sijinjoseph at gmail.com
Mon May 2 17:27:49 CEST 2011

Hi - I am working on a patch where I have an argument that can either be a
unicode string or binary data, I parse the argument using the
PyArg_ParseTuple method using the s* format specification and get a

I now need to convert this Py_Buffer object to a Py_Unicode and pass it into
a function. What is the best way to do this? If I determine that the passed
argument was binary using another flag parameter then I am passing
Py_Buffer->buf as a pointer to the start of the data.

This is in winsound module, here's the relevant code snippet

sound_playsound(PyObject *s, PyObject *args)
    Py_buffer *buffer;
    int flags;
    int ok;
    LPCWSTR pszSound;

    if (PyArg_ParseTuple(args, "s*i:PlaySound", &buffer, &flags)) {
        if (flags & SND_ASYNC && flags & SND_MEMORY) {
            /* Sidestep reference counting headache; unfortunately this also
               prevent SND_LOOP from memory. */
            PyErr_SetString(PyExc_RuntimeError, "Cannot play asynchronously
from memory");
            return NULL;

        if(flags & SND_MEMORY) {
            pszSound = buffer->buf;
        else {
            /* pszSound = ????; */

-- Sijin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20110502/8a0f0250/attachment.html>

More information about the Python-Dev mailing list