diff options
author | Hugo Lima <hugo.lima@openbossa.org> | 2010-01-27 17:51:56 -0200 |
---|---|---|
committer | Hugo Lima <hugo.lima@openbossa.org> | 2010-01-27 18:01:25 -0200 |
commit | f33140602f8aee732cfa9c07311a555d28fa0621 (patch) | |
tree | 46408b515b65f1a1d84ab0525f4eac7f26cfb40e /PySide/QtCore/glue | |
parent | 3592cd622e61ee689b096c99d46b6d936109e383 (diff) |
Add support for buffer protocol on QStrings.
Diffstat (limited to 'PySide/QtCore/glue')
-rw-r--r-- | PySide/QtCore/glue/qstring_bufferprotocol.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/PySide/QtCore/glue/qstring_bufferprotocol.cpp b/PySide/QtCore/glue/qstring_bufferprotocol.cpp new file mode 100644 index 000000000..9e7fb2cf1 --- /dev/null +++ b/PySide/QtCore/glue/qstring_bufferprotocol.cpp @@ -0,0 +1,43 @@ + +#if PY_VERSION_HEX < 0x03000000 + +// QByteArray buffer protocol functions +// see: http://www.python.org/dev/peps/pep-3118/ + +extern "C" { + +static Py_ssize_t SbkQString_segcountproc(PyObject* self, Py_ssize_t* lenp) +{ + if (lenp) + *lenp = self->ob_type->tp_as_sequence->sq_length(self); + return 1; +} + +static Py_ssize_t SbkQString_readbufferproc(PyObject* self, Py_ssize_t segment, char** ptrptr) +{ + if (segment || Shiboken::cppObjectIsInvalid(self)) + return -1; + + QString* cppSelf = SbkQString_cptr(self); + QByteArray decodedData = cppSelf->toLocal8Bit(); + Shiboken::AutoDecRef decodedString(PyString_FromStringAndSize(decodedData.constData(), decodedData.size())); + + // delete __encodedStr attr if it exists + Shiboken::AutoDecRef attrName(PyString_FromStringAndSize("__encodedStr", sizeof("__encodedStr")-1)); + if (PyObject_HasAttr(self, attrName)) + PyObject_DelAttr(self, attrName); + PyObject_SetAttr(self, attrName, decodedString); + *ptrptr = PyString_AS_STRING(decodedString.object()); + return decodedData.size(); +} + +PyBufferProcs SbkQStringBufferProc = { + /*bf_getreadbuffer*/ 0, + /*bf_getwritebuffer*/ 0, + /*bf_getsegcount*/ &SbkQString_segcountproc, + /*bf_getcharbuffer*/ &SbkQString_readbufferproc +}; + +} + +#endif |