Add support for buffer protocol on QStrings.
This commit is contained in:
parent
3592cd622e
commit
f33140602f
3 changed files with 52 additions and 6 deletions
43
PySide/QtCore/glue/qstring_bufferprotocol.cpp
Normal file
43
PySide/QtCore/glue/qstring_bufferprotocol.cpp
Normal file
|
|
@ -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
|
||||||
|
|
@ -939,6 +939,14 @@
|
||||||
<conversion-rule file="qstring_conversions.h" />
|
<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[2]" />
|
||||||
<modify-documentation xpath="/description/section[@id='initializing-a-string']/para[3]" />
|
<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="insert(int,const QChar*,int)" remove="all" />
|
||||||
<modify-function signature="QString(const QChar*, int)" remove="all" />
|
<modify-function signature="QString(const QChar*, int)" remove="all" />
|
||||||
|
|
|
||||||
|
|
@ -13,12 +13,7 @@ class QStringBufferProtocolTest(unittest.TestCase):
|
||||||
def testQStringBufferProtocol(self):
|
def testQStringBufferProtocol(self):
|
||||||
#Tests QString implementation of Python buffer protocol using the os.path.isdir
|
#Tests QString implementation of Python buffer protocol using the os.path.isdir
|
||||||
#function which an unicode object or other object implementing the Python buffer protocol
|
#function which an unicode object or other object implementing the Python buffer protocol
|
||||||
os_path_isdir_function_correctly_called_with_a_qstring = True
|
isdir(QString('/tmp'))
|
||||||
try:
|
|
||||||
isdir(QString('/tmp'))
|
|
||||||
except:
|
|
||||||
os_path_isdir_function_correctly_called_with_a_qstring = False
|
|
||||||
self.assertTrue(os_path_isdir_function_correctly_called_with_a_qstring)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue