[Python-Dev] Convert Py_Buffer to Py_UNICODE (original) (raw)
M.-A. Lemburg mal at egenix.com
Mon May 2 21:12:27 CEST 2011
- Previous message: [Python-Dev] Convert Py_Buffer to Py_UNICODE
- Next message: [Python-Dev] PEP 386 and dev repository versions workflow
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Sijin Joseph wrote:
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 PyArgParseTuple method using the s* format specification and get a PyBuffer.
I now need to convert this PyBuffer object to a PyUnicode 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 PyBuffer->buf as a pointer to the start of the data.
I don't understand why you'd want to convert PyUnicode to PyBytes (encoded as UTF-8), only to decode it again afterwards in order to pass it to some other PyUnicode API.
It'd be more efficient to use the "O" parser marker and then use PyObject_GetBuffer() to convert non-PyUnicode objects to a Py_buffer.
This is in winsound module, here's the relevant code snippet
soundplaysound(PyObject *s, PyObject *args) { Pybuffer *buffer; int flags; int ok; LPCWSTR pszSound; if (PyArgParseTuple(args, "s*i:PlaySound", &buffer, &flags)) { if (flags & SNDASYNC && flags & SNDMEMORY) { /* Sidestep reference counting headache; unfortunately this also prevent SNDLOOP from memory. */ PyBufferRelease(buffer); PyErrSetString(PyExcRuntimeError, "Cannot play asynchronously from memory"); return NULL; } if(flags & SNDMEMORY) { pszSound = buffer->buf; } else { /* pszSound = ????; */ } -- Sijin
Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/mal%40egenix.com
-- Marc-Andre Lemburg eGenix.com
Professional Python Services directly from the Source (#1, May 02 2011)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
2011-06-20: EuroPython 2011, Florence, Italy 49 days to go
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/
- Previous message: [Python-Dev] Convert Py_Buffer to Py_UNICODE
- Next message: [Python-Dev] PEP 386 and dev repository versions workflow
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]