From 821480b1f0d0cfb59df427315c29c364096818c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristi=C3=A1n=20Maureira-Fredes?= Date: Wed, 29 Jan 2020 11:57:05 +0100 Subject: Fix PyBuffer interface for QByteArray For the limited API, properly implement the PyBUF_ND flag (shape requested). Otherwise, use the convenience function PyBuffer_FillInfo() to properly populate the view. Fixes: PYSIDE-1204 Change-Id: I2a4c81885cf49b25c89823577c0d7ee2f2707b87 Reviewed-by: Qt CI Bot Reviewed-by: Cristian Maureira-Fredes --- sources/pyside2/PySide2/glue/qtcore.cpp | 10 +++++++++- sources/pyside2/tests/QtCore/qbytearray_test.py | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/sources/pyside2/PySide2/glue/qtcore.cpp b/sources/pyside2/PySide2/glue/qtcore.cpp index 8bcc315b2..d99a150ad 100644 --- a/sources/pyside2/PySide2/glue/qtcore.cpp +++ b/sources/pyside2/PySide2/glue/qtcore.cpp @@ -1046,6 +1046,7 @@ static int SbkQByteArray_getbufferproc(PyObject *obj, Py_buffer *view, int flags QByteArray * cppSelf = %CONVERTTOCPP[QByteArray *](obj); //XXX /|\ omitting this space crashes shiboken! + #ifdef Py_LIMITED_API view->obj = obj; view->buf = reinterpret_cast(cppSelf->data()); view->len = cppSelf->size(); @@ -1053,13 +1054,20 @@ static int SbkQByteArray_getbufferproc(PyObject *obj, Py_buffer *view, int flags view->itemsize = 1; view->format = const_cast("c"); view->ndim = 1; - view->shape = NULL; + view->shape = (flags & PyBUF_ND) == PyBUF_ND ? &(view->len) : nullptr; view->strides = &view->itemsize; view->suboffsets = NULL; view->internal = NULL; Py_XINCREF(obj); return 0; +#else // Py_LIMITED_API + const int result = PyBuffer_FillInfo(view, obj, reinterpret_cast(cppSelf->data()), + cppSelf->size(), 0, flags); + if (result == 0) + Py_XINCREF(obj); + return result; +#endif } #if PY_VERSION_HEX < 0x03000000 diff --git a/sources/pyside2/tests/QtCore/qbytearray_test.py b/sources/pyside2/tests/QtCore/qbytearray_test.py index dba9ecfea..4760fe20b 100644 --- a/sources/pyside2/tests/QtCore/qbytearray_test.py +++ b/sources/pyside2/tests/QtCore/qbytearray_test.py @@ -265,6 +265,12 @@ class QByteArraySliceAssignment(unittest.TestCase): b[9:2:-3] = bytearray(py3k.b('XYZ')) self.assertEqual(b, py3k.b('012Z45Y78X')) + def testBufferProtocol(self): + orig_bytes = py3k.b('0123456789') + byte_array = QByteArray(orig_bytes) + actual_bytes = bytes(byte_array) + self.assertEqual(orig_bytes, actual_bytes) + if __name__ == '__main__': unittest.main() -- cgit v1.2.3