diff options
Diffstat (limited to 'PySide')
-rw-r--r-- | PySide/QtCore/glue/qstring_bufferprotocol.cpp | 43 | ||||
-rw-r--r-- | PySide/QtCore/typesystem_core.xml | 8 |
2 files changed, 51 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 diff --git a/PySide/QtCore/typesystem_core.xml b/PySide/QtCore/typesystem_core.xml index 8b36d2e3b..b87eb90f1 100644 --- a/PySide/QtCore/typesystem_core.xml +++ b/PySide/QtCore/typesystem_core.xml @@ -939,6 +939,14 @@ <conversion-rule file="qstring_conversions.h" /> <modify-documentation xpath="/description/section[@id='initializing-a-string']/para[2]" /> <modify-documentation xpath="/description/section[@id='initializing-a-string']/para[3]" /> + <!-- buffer protocol --> + <inject-code class="native" position="beginning" file="glue/qstring_bufferprotocol.cpp" /> + <inject-code class="target" position="end"> + #if PY_VERSION_HEX < 0x03000000 + SbkQString_Type.super.ht_type.tp_as_buffer = &SbkQStringBufferProc; + SbkQString_Type.super.ht_type.tp_flags |= Py_TPFLAGS_HAVE_GETCHARBUFFER; + #endif + </inject-code> <modify-function signature="insert(int,const QChar*,int)" remove="all" /> <modify-function signature="QString(const QChar*, int)" remove="all" /> |