diff options
author | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2022-01-05 16:40:54 +0100 |
---|---|---|
committer | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2022-04-11 11:47:54 +0200 |
commit | e51b975e38ae91ec5a577dd264b6b102e4535a53 (patch) | |
tree | 914a60be71dd0b2ed32979fc63772c20b16c1e07 | |
parent | b9b5a3aae8c776e1c2e1032951fa559d2d7ae293 (diff) |
Add readBytes and writeBytes functions from QDataStream
- along with the addition of these functions, a common.xml is created to stores all the common templates
Task-number: PYSIDE-890
Change-Id: I4fadfe77a38635f15a7aef04adeac949c2d61b5d
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
(cherry picked from commit af7ec7bef57b02a88a459869865eb197f2ed5a1a)
-rw-r--r-- | sources/pyside6/PySide6/CMakeLists.txt | 2 | ||||
-rw-r--r-- | sources/pyside6/PySide6/QtCore/typesystem_core_common.xml | 27 | ||||
-rw-r--r-- | sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml | 1 | ||||
-rw-r--r-- | sources/pyside6/PySide6/glue/qtcore.cpp | 15 | ||||
-rw-r--r-- | sources/pyside6/PySide6/templates/common.xml | 63 | ||||
-rw-r--r-- | sources/pyside6/tests/QtCore/qdatastream_test.py | 21 |
6 files changed, 123 insertions, 6 deletions
diff --git a/sources/pyside6/PySide6/CMakeLists.txt b/sources/pyside6/PySide6/CMakeLists.txt index 68ce0d2b8..84231a930 100644 --- a/sources/pyside6/PySide6/CMakeLists.txt +++ b/sources/pyside6/PySide6/CMakeLists.txt @@ -88,6 +88,8 @@ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/_config.py" DESTINATION "${PYTHON_SITE_PACKAGES}/${BINDING_NAME}${pyside6_SUFFIX}") install(FILES "${CMAKE_CURRENT_BINARY_DIR}/_git_pyside_version.py" DESTINATION "${PYTHON_SITE_PACKAGES}/${BINDING_NAME}${pyside6_SUFFIX}") +install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/templates/common.xml + DESTINATION share/PySide6${pyside_SUFFIX}/typesystems) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/templates/core_common.xml DESTINATION share/PySide6${pyside_SUFFIX}/typesystems) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/templates/gui_common.xml diff --git a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml index f69383c6c..66da81411 100644 --- a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml +++ b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml @@ -40,6 +40,7 @@ ****************************************************************************/ --> <typesystem package="PySide6.QtCore"> + <load-typesystem name="templates/common.xml" generate="no"/> <load-typesystem name="templates/core_common.xml" generate="no"/> <custom-type name="list of QAbstractAnimation"/> @@ -2741,10 +2742,30 @@ <add-function signature="writeString(QString)"> <inject-code class="target" position="end" file="../glue/qtcore.cpp" snippet="stream-write-method"/> </add-function> + <modify-function signature="readBytes(char*&,uint&)"> + <modify-argument index="return"> + <replace-type modified-type="PyTuple"/> + </modify-argument> + <modify-argument index="1"> + <remove-argument /> + </modify-argument> + <inject-code class="target" position="beginning" file="../glue/qtcore.cpp" snippet="qdatastream-read-bytes"/> + </modify-function> - <!-- ### deprecated method --> - <modify-function signature="readBytes(char*&,uint&)" remove="all"/> - <modify-function signature="writeBytes(const char*,uint)" remove="all"/> + <modify-function signature="writeBytes(const char*,uint)"> + <modify-argument index="1"> + <replace-type modified-type="PyBuffer"/> + <conversion-rule class="native"> + <insert-template name="pybuffer_const_char"/> + </conversion-rule> + </modify-argument> + <modify-argument index="2"> + <remove-argument/> + <conversion-rule class="native"> + <insert-template name="uint_remove"/> + </conversion-rule> + </modify-argument> + </modify-function> </object-type> <value-type name="QTextStreamManipulator" default-constructor="QTextStreamManipulator(0, 0)"> diff --git a/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml b/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml index dfc302da6..42479f091 100644 --- a/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml +++ b/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml @@ -41,6 +41,7 @@ --> <typesystem package="PySide6.QtGui"> <load-typesystem name="QtCore/typesystem_core.xml" generate="no"/> + <load-typesystem name="templates/common.xml" generate="no"/> <load-typesystem name="templates/core_common.xml" generate="no"/> <load-typesystem name="templates/gui_common.xml" generate="no"/> <load-typesystem name="templates/opengl_common.xml" generate="no"/> diff --git a/sources/pyside6/PySide6/glue/qtcore.cpp b/sources/pyside6/PySide6/glue/qtcore.cpp index 680949d7f..983f547a0 100644 --- a/sources/pyside6/PySide6/glue/qtcore.cpp +++ b/sources/pyside6/PySide6/glue/qtcore.cpp @@ -2016,3 +2016,18 @@ if (Shiboken::Enum::check(%PYARG_2)) { cppArg1 = QVariant(in); } // @snippet qmetaproperty_write_enum + +// @snippet qdatastream-read-bytes +QByteArray data; +data.resize(%2); +auto dataChar = data.data(); +cppSelf->readBytes(dataChar, %2); +const char *constDataChar = dataChar; +if (dataChar == nullptr) { + Py_INCREF(Py_None); + %PYARG_0 = Py_None; +} else { + %PYARG_0 = PyBytes_FromStringAndSize(constDataChar, %2); +} +// @snippet qdatastream-read-bytes + diff --git a/sources/pyside6/PySide6/templates/common.xml b/sources/pyside6/PySide6/templates/common.xml new file mode 100644 index 000000000..cd526821d --- /dev/null +++ b/sources/pyside6/PySide6/templates/common.xml @@ -0,0 +1,63 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt for Python. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +--> +<typesystem> + <template name="const_char_pybuffer"> + PyObject *%out = Shiboken::Buffer::newObject(%in, size); + </template> + + <template name="pybuffer_const_char"> + Py_ssize_t bufferLen; + char *%out = reinterpret_cast<char*>(Shiboken::Buffer::getPointer(%PYARG_1, &bufferLen)); + </template> + + <template name="uint_remove"> + uint %out = bufferLen; + </template> + + <template name="pybytes_const_uchar"> + const uchar *%out = reinterpret_cast<const uchar*>(PyBytes_AS_STRING(%PYARG_1)); + </template> + + <template name="pybytes_uint"> + uint %out = static_cast<uint>(PyBytes_Size(%PYARG_1)); + </template> +</typesystem> diff --git a/sources/pyside6/tests/QtCore/qdatastream_test.py b/sources/pyside6/tests/QtCore/qdatastream_test.py index cd0e3e0cb..eae83df10 100644 --- a/sources/pyside6/tests/QtCore/qdatastream_test.py +++ b/sources/pyside6/tests/QtCore/qdatastream_test.py @@ -35,8 +35,10 @@ import sys import unittest from pathlib import Path + sys.path.append(os.fspath(Path(__file__).resolve().parents[1])) from init_paths import init_test_paths + init_test_paths(False) from PySide6.QtCore import QBitArray, QByteArray, QIODevice, QDataStream, QDate, QTime, QDateTime @@ -275,7 +277,6 @@ class QDataStreamShift(unittest.TestCase): class QDataStreamShiftBitArray(unittest.TestCase): - def _check_bitarray(self, data_set): '''Check the >> operator for the given data set''' @@ -308,7 +309,7 @@ class QDataStreamShiftBitArray(unittest.TestCase): self._check_bitarray([(serialized, QDataStream.ReadPastEnd, QBitArray())]) -class QDataStreamRawData(unittest.TestCase): +class QDataStreamBuffer(unittest.TestCase): def testRawData(self): data = QDataStream() self.assertEqual(data.readRawData(4), None) @@ -321,7 +322,21 @@ class QDataStreamRawData(unittest.TestCase): data = QDataStream(ba) self.assertEqual(data.readRawData(4), bytes('AB\x00C', "UTF-8")) + def testBytes(self): + dataOne = QDataStream() + self.assertEqual(dataOne.readBytes(4), None) + + ba = QByteArray() + data = QDataStream(ba, QIODevice.WriteOnly) + # writeBytes() writes a quint32 containing the length of the data, + # followed by the data. + data.writeBytes(bytes('AB\x00C', 'UTF-8')) + self.assertEqual(ba.data(), bytes('\x00\x00\x00\x04AB\x00C', 'UTF-8')) + + data = QDataStream(ba) + buffer = data.readBytes(4) + self.assertEqual(buffer, bytes('AB\x00C', 'UTF-8')) + if __name__ == '__main__': unittest.main() - |