aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCristián Maureira-Fredes <cristian.maureira-fredes@qt.io>2020-01-29 11:57:05 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2020-02-05 10:16:52 +0100
commit821480b1f0d0cfb59df427315c29c364096818c4 (patch)
treec033e26734c0ea19081347651ae6425cd5870974
parent95ec3e2ca7a0a06c0750f78e0c4e44e09f5347f2 (diff)
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 <qt_ci_bot@qt-project.org> Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
-rw-r--r--sources/pyside2/PySide2/glue/qtcore.cpp10
-rw-r--r--sources/pyside2/tests/QtCore/qbytearray_test.py6
2 files changed, 15 insertions, 1 deletions
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<void *>(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<char *>("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<void *>(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()