From 78dad8180d797a647645b74255bfc29c46d7264a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristi=C3=A1n=20Maureira-Fredes?= Date: Wed, 29 May 2019 11:22:44 +0200 Subject: Add optional support for types in QSettings::value This add an optional named parameter to the function value() to automatically cast the type that is being returned by the function. An example of this situation could be an ini file that contains the value of a one-element list: settings.setValue('var', ['a']) The the ini file will be: [General] var=a # we cannot know that this is a list! Once we read it, we could specify if we want the default behavior, a str, or to cast the output to a list. settings.value('var') # Will get "a" settings.value('var', type=list) # Will get ["a"] The cppgenerator was modified to add a verification step before trying to get the named parameter, since it could be optional and having one named parameter was assumming that all of them were provided. Change-Id: I8f379debea86b42cf89019d432e990084c9e6614 Fixes: PYSIDE-1010 Reviewed-by: Qt CI Bot Reviewed-by: Christian Tismer --- .../PySide2/QtCore/typesystem_core_common.xml | 11 ++-- sources/pyside2/PySide2/glue/qtcore.cpp | 50 ++++++++++++++ sources/pyside2/tests/QtCore/CMakeLists.txt | 1 + sources/pyside2/tests/QtCore/qsettings_test.ini | 4 ++ sources/pyside2/tests/QtCore/qsettings_test.py | 77 ++++++++++++++++++++++ 5 files changed, 138 insertions(+), 5 deletions(-) create mode 100644 sources/pyside2/tests/QtCore/qsettings_test.ini create mode 100644 sources/pyside2/tests/QtCore/qsettings_test.py (limited to 'sources/pyside2') diff --git a/sources/pyside2/PySide2/QtCore/typesystem_core_common.xml b/sources/pyside2/PySide2/QtCore/typesystem_core_common.xml index 2173e747b..78a731458 100644 --- a/sources/pyside2/PySide2/QtCore/typesystem_core_common.xml +++ b/sources/pyside2/PySide2/QtCore/typesystem_core_common.xml @@ -2511,11 +2511,12 @@ - - - .. warning:: QSettings.value can return different types (QVariant types) depending on the platform it's running on, so the safest way to use it is always casting the result to the desired type, e.g.: int(settings.value("myKey")) - - + + + + + diff --git a/sources/pyside2/PySide2/glue/qtcore.cpp b/sources/pyside2/PySide2/glue/qtcore.cpp index 930ad9349..3e1bab97b 100644 --- a/sources/pyside2/PySide2/glue/qtcore.cpp +++ b/sources/pyside2/PySide2/glue/qtcore.cpp @@ -56,6 +56,56 @@ bool py2kStrCheck(PyObject *obj) } // @snippet pystring-check +// @snippet qsettings-value +QVariant out = %CPPSELF.value(%1, %2); +PyTypeObject *typeObj = reinterpret_cast(%PYARG_3); + +if (typeObj) { + if (typeObj == &PyList_Type) { + QByteArrayList valuesList = out.toByteArray().split(','); + const int valuesSize = valuesList.size(); + if (valuesSize > 0) { + PyObject *list = PyList_New(valuesSize); + for (int i = 0; i < valuesSize; i++) { + PyObject *item = PyUnicode_FromString(valuesList[i].data()); + PyList_SET_ITEM(list, i, item); + Py_DECREF(item); + } + %PYARG_0 = list; + + } else { + %PYARG_0 = %CONVERTTOPYTHON[QVariant](out); + } + } else if (typeObj == &PyBytes_Type) { + QByteArray asByteArray = out.toByteArray(); + %PYARG_0 = PyBytes_FromString(asByteArray.data()); + } else if (typeObj == &PyUnicode_Type) { + QByteArray asByteArray = out.toByteArray(); + %PYARG_0 = PyUnicode_FromString(asByteArray.data()); +#ifdef IS_PY3K + } else if (typeObj == &PyLong_Type) { + float asFloat = out.toFloat(); + pyResult = PyLong_FromDouble(asFloat); +#else + } else if (typeObj == &PyInt_Type) { + float asFloat = out.toFloat(); + pyResult = PyInt_FromLong(long(asFloat)); +#endif + } else if (typeObj == &PyFloat_Type) { + float asFloat = out.toFloat(); + %PYARG_0 = PyFloat_FromDouble(asFloat); + } + // TODO: PyDict_Type and PyTuple_Type +} +else { + if (out == 0) + %PYARG_0 = Py_None; + else + %PYARG_0 = %CONVERTTOPYTHON[QVariant](out); +} + +// @snippet qsettings-value + // @snippet qvariant-conversion static const char *QVariant_resolveMetaType(PyTypeObject *type, int *typeId) { diff --git a/sources/pyside2/tests/QtCore/CMakeLists.txt b/sources/pyside2/tests/QtCore/CMakeLists.txt index 08e63d043..d05699f16 100644 --- a/sources/pyside2/tests/QtCore/CMakeLists.txt +++ b/sources/pyside2/tests/QtCore/CMakeLists.txt @@ -96,6 +96,7 @@ PYSIDE_TEST(qrect_test.py) PYSIDE_TEST(qregexp_test.py) PYSIDE_TEST(qregularexpression_test.py) PYSIDE_TEST(qresource_test.py) +PYSIDE_TEST(qsettings_test.py) PYSIDE_TEST(qsize_test.py) PYSIDE_TEST(qslot_object_test.py) PYSIDE_TEST(qsocketnotifier_test.py) diff --git a/sources/pyside2/tests/QtCore/qsettings_test.ini b/sources/pyside2/tests/QtCore/qsettings_test.ini new file mode 100644 index 000000000..f54ae0029 --- /dev/null +++ b/sources/pyside2/tests/QtCore/qsettings_test.ini @@ -0,0 +1,4 @@ +[General] +var1=a, b, c +var2=a + diff --git a/sources/pyside2/tests/QtCore/qsettings_test.py b/sources/pyside2/tests/QtCore/qsettings_test.py new file mode 100644 index 000000000..6d64b0db3 --- /dev/null +++ b/sources/pyside2/tests/QtCore/qsettings_test.py @@ -0,0 +1,77 @@ +############################################################################# +## +## Copyright (C) 2019 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of Qt for Python. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## 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 General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## 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-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for QDate''' + +import unittest + +import os +from helper import adjust_filename +import py3kcompat as py3k +from PySide2.QtCore import QSettings + +class TestQSettings(unittest.TestCase): + def testConversions(self): + file_path = adjust_filename('qsettings_test.ini', __file__) + settings = QSettings(file_path, QSettings.IniFormat) + + r = settings.value('var1') + self.assertEqual(type(r), list) + + r = settings.value('var2') + if py3k.IS_PY3K: + self.assertEqual(type(r), str) + else: + self.assertEqual(type(r), unicode) + + r = settings.value('var2', type=list) + self.assertEqual(type(r), list) + + + def testDefaultValueConversion(self): + settings = QSettings('foo.ini', QSettings.IniFormat) + r = settings.value('lala', 22) + if py3k.IS_PY3K: + self.assertEqual(type(r), int) + else: + self.assertEqual(type(r), long) + + r = settings.value('lala', 22, type=str) + self.assertEqual(type(r), str) + + r = settings.value('lala', 22, type=bytes) + self.assertEqual(type(r), bytes) + + r = settings.value('lala', 22, type=int) + self.assertEqual(type(r), int) + + r = settings.value('lala', 22, type=float) + self.assertEqual(type(r), float) + +if __name__ == '__main__': + unittest.main() -- cgit v1.2.3 From 6c8fafb996e4cd2abd3dd82c3127c65b88157ff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristi=C3=A1n=20Maureira-Fredes?= Date: Thu, 4 Jul 2019 09:12:37 +0200 Subject: Change QtQml tests file permissions to 644 Change-Id: I0447da5e1ace3f6acc930aa4296576ec447f1be5 Reviewed-by: Friedemann Kleint --- sources/pyside2/tests/QtQml/CMakeLists.txt | 0 sources/pyside2/tests/QtQml/bug_1029.py | 0 sources/pyside2/tests/QtQml/bug_1029.qml | 0 sources/pyside2/tests/QtQml/bug_451.py | 0 sources/pyside2/tests/QtQml/bug_451.qml | 0 sources/pyside2/tests/QtQml/bug_456.py | 0 sources/pyside2/tests/QtQml/bug_456.qml | 0 sources/pyside2/tests/QtQml/bug_557.py | 0 sources/pyside2/tests/QtQml/bug_726.py | 0 sources/pyside2/tests/QtQml/bug_726.qml | 0 sources/pyside2/tests/QtQml/bug_814.py | 0 sources/pyside2/tests/QtQml/bug_814.qml | 0 sources/pyside2/tests/QtQml/bug_825.py | 0 sources/pyside2/tests/QtQml/bug_825.qml | 0 sources/pyside2/tests/QtQml/bug_847.py | 0 sources/pyside2/tests/QtQml/bug_847.qml | 0 sources/pyside2/tests/QtQml/bug_915.py | 0 sources/pyside2/tests/QtQml/bug_926.py | 0 sources/pyside2/tests/QtQml/bug_926.qml | 0 sources/pyside2/tests/QtQml/bug_951.py | 0 sources/pyside2/tests/QtQml/bug_951.qml | 0 sources/pyside2/tests/QtQml/bug_995.py | 0 sources/pyside2/tests/QtQml/bug_995.qml | 0 sources/pyside2/tests/QtQml/bug_997.py | 0 sources/pyside2/tests/QtQml/bug_997.qml | 0 sources/pyside2/tests/QtQml/connect_python_qml.qml | 0 sources/pyside2/tests/QtQml/hw.qml | 0 sources/pyside2/tests/QtQml/qqmlnetwork_test.py | 0 sources/pyside2/tests/QtQml/qquickview_test.py | 0 sources/pyside2/tests/QtQml/registertype.py | 0 sources/pyside2/tests/QtQml/registertype.qml | 0 sources/pyside2/tests/QtQml/signal_arguments.py | 0 sources/pyside2/tests/QtQml/view.qml | 0 sources/pyside2/tests/QtQml/viewmodel.qml | 0 34 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 sources/pyside2/tests/QtQml/CMakeLists.txt mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_1029.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_1029.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_451.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_451.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_456.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_456.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_557.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_726.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_726.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_814.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_814.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_825.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_825.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_847.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_847.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_915.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_926.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_926.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_951.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_951.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_995.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_995.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_997.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/bug_997.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/connect_python_qml.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/hw.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/qqmlnetwork_test.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/qquickview_test.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/registertype.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/registertype.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/signal_arguments.py mode change 100755 => 100644 sources/pyside2/tests/QtQml/view.qml mode change 100755 => 100644 sources/pyside2/tests/QtQml/viewmodel.qml (limited to 'sources/pyside2') diff --git a/sources/pyside2/tests/QtQml/CMakeLists.txt b/sources/pyside2/tests/QtQml/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_1029.py b/sources/pyside2/tests/QtQml/bug_1029.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_1029.qml b/sources/pyside2/tests/QtQml/bug_1029.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_451.py b/sources/pyside2/tests/QtQml/bug_451.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_451.qml b/sources/pyside2/tests/QtQml/bug_451.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_456.py b/sources/pyside2/tests/QtQml/bug_456.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_456.qml b/sources/pyside2/tests/QtQml/bug_456.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_557.py b/sources/pyside2/tests/QtQml/bug_557.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_726.py b/sources/pyside2/tests/QtQml/bug_726.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_726.qml b/sources/pyside2/tests/QtQml/bug_726.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_814.py b/sources/pyside2/tests/QtQml/bug_814.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_814.qml b/sources/pyside2/tests/QtQml/bug_814.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_825.py b/sources/pyside2/tests/QtQml/bug_825.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_825.qml b/sources/pyside2/tests/QtQml/bug_825.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_847.py b/sources/pyside2/tests/QtQml/bug_847.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_847.qml b/sources/pyside2/tests/QtQml/bug_847.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_915.py b/sources/pyside2/tests/QtQml/bug_915.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_926.py b/sources/pyside2/tests/QtQml/bug_926.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_926.qml b/sources/pyside2/tests/QtQml/bug_926.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_951.py b/sources/pyside2/tests/QtQml/bug_951.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_951.qml b/sources/pyside2/tests/QtQml/bug_951.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_995.py b/sources/pyside2/tests/QtQml/bug_995.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_995.qml b/sources/pyside2/tests/QtQml/bug_995.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_997.py b/sources/pyside2/tests/QtQml/bug_997.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/bug_997.qml b/sources/pyside2/tests/QtQml/bug_997.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/connect_python_qml.qml b/sources/pyside2/tests/QtQml/connect_python_qml.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/hw.qml b/sources/pyside2/tests/QtQml/hw.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/qqmlnetwork_test.py b/sources/pyside2/tests/QtQml/qqmlnetwork_test.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/qquickview_test.py b/sources/pyside2/tests/QtQml/qquickview_test.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/registertype.py b/sources/pyside2/tests/QtQml/registertype.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/registertype.qml b/sources/pyside2/tests/QtQml/registertype.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/signal_arguments.py b/sources/pyside2/tests/QtQml/signal_arguments.py old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/view.qml b/sources/pyside2/tests/QtQml/view.qml old mode 100755 new mode 100644 diff --git a/sources/pyside2/tests/QtQml/viewmodel.qml b/sources/pyside2/tests/QtQml/viewmodel.qml old mode 100755 new mode 100644 -- cgit v1.2.3 From 295d9d7fb52505666a6dc9ac7b6fba2323814c48 Mon Sep 17 00:00:00 2001 From: Christian Tismer Date: Fri, 15 Feb 2019 18:39:45 +0100 Subject: Cleanup signature module before substantial change There were some refinements applied while developing "Support Pointer Primitive Types by Arrays or Result Tuples". This patch moves these changes out which are not essential for that patch. They include - sort all mapping groups by name - replace huge regex by a pattern generator - replace dictionary string entries by SimpleNameSpace - improve PEP 563 handling - simplify "zero(sometype)" substantially - better handling of "QGenericMatrix" (preview) A test for the generated pattern against a reference parser was added. Task-number: PYSIDE-795 Task-number: PYSIDE-951 Change-Id: I5a6b236850c63a7db77b7f7b88881486fd1e61be Reviewed-by: Cristian Maureira-Fredes --- sources/pyside2/PySide2/support/generate_pyi.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'sources/pyside2') diff --git a/sources/pyside2/PySide2/support/generate_pyi.py b/sources/pyside2/PySide2/support/generate_pyi.py index 294cdc91b..c732227f4 100644 --- a/sources/pyside2/PySide2/support/generate_pyi.py +++ b/sources/pyside2/PySide2/support/generate_pyi.py @@ -252,6 +252,10 @@ def generate_all_pyi(outpath, options): from PySide2.support.signature import inspect from PySide2.support.signature.lib.enum_sig import HintingEnumerator + # propagate USE_PEP563 to the mapping module. + # Perhaps this can be automated? + PySide2.support.signature.mapping.USE_PEP563 = USE_PEP563 + outpath = outpath or os.path.dirname(PySide2.__file__) name_list = PySide2.__all__ if options.modules == ["all"] else options.modules errors = ", ".join(set(name_list) - set(PySide2.__all__)) -- cgit v1.2.3