diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-12-18 10:19:43 +0100 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-12-19 17:49:19 +0100 |
| commit | 67a8f157eeb7d9934457f58d78a0f90c5396c149 (patch) | |
| tree | 6437e0cdd5f5743b473fd7a9389bbd2e0f65c538 | |
| parent | f3f0c160dbf84f5f7e616709f2b829582d94ffc0 (diff) | |
Avoid converting to string for calculating the hash value
Partially revert bf8a60db4cdbfc3e7c9c98778b219e9c83746d44, which
introduced a helper function converting to QString for Q(Date)(Time)
and QUrl. For these classes, the qHash() function should be found by
the code model after 9c37876d6f649b3c9bd1411d3c7ffe620786f1a8.
Extend the test accordingly.
Deprecate the QString-helper.
Task-number: PYSIDE-1906
Change-Id: Ia210a2210bc6a43991d5b26374039f4e86d0e71e
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
| -rw-r--r-- | sources/pyside6/PySide6/QtCore/typesystem_core_common.xml | 18 | ||||
| -rw-r--r-- | sources/pyside6/libpyside/pysideqhash.h | 2 | ||||
| -rw-r--r-- | sources/pyside6/tests/QtCore/hash_test.py | 35 |
3 files changed, 38 insertions, 17 deletions
diff --git a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml index 2dbafc8b8..be472ecd8 100644 --- a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml +++ b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml @@ -774,10 +774,7 @@ <enum-type name="System"/> <enum-type identified-by-value="Unspecified"/> </value-type> - <value-type name="QDate" hash-function="PySide::hash" > - <extra-includes> - <include file-name="pysideqhash.h" location="global"/> - </extra-includes> + <value-type name="QDate"> <inject-code class="native" position="beginning" file="../glue/qtcore.cpp" snippet="core-snippets-p-h"/> <conversion-rule> @@ -835,10 +832,7 @@ <inject-code class="target" position="beginning" file="../glue/qtcore.cpp" snippet="qdate-weeknumber"/> </modify-function> </value-type> - <value-type name="QDateTime" hash-function="PySide::hash"> - <extra-includes> - <include file-name="pysideqhash.h" location="global"/> - </extra-includes> + <value-type name="QDateTime"> <inject-code class="native" position="beginning" file="../glue/qtcore.cpp" snippet="core-snippets-p-h"/> <enum-type name="YearRange"/> @@ -1222,10 +1216,7 @@ <!--### --> </value-type> - <value-type name="QTime" hash-function="PySide::hash"> - <extra-includes> - <include file-name="pysideqhash.h" location="global"/> - </extra-includes> + <value-type name="QTime"> <inject-code class="native" position="beginning" file="../glue/qtcore.cpp" snippet="core-snippets-p-h"/> <conversion-rule> @@ -1852,7 +1843,7 @@ </object-type> <value-type name="QUrlQuery"/> - <value-type name="QUrl" hash-function="PySide::hash"> + <value-type name="QUrl"> <!-- Qt5: lots of changes --> <enum-type name="ComponentFormattingOption" python-type="IntFlag" flags="ComponentFormattingOptions,FormattingOptions"/> <!-- note: above duplication of attribute is not by default XML compliant! --> @@ -1862,7 +1853,6 @@ <enum-type name="AceProcessingOption" flags="AceProcessingOptions" since="6.3"/> <extra-includes> <include file-name="QStringList" location="global"/> - <include file-name="pysideqhash.h" location="global"/> </extra-includes> <add-function signature="__repr__" return-type="PyObject*"> <inject-code class="target" position="beginning"> diff --git a/sources/pyside6/libpyside/pysideqhash.h b/sources/pyside6/libpyside/pysideqhash.h index e6e1392a9..ae2d295f6 100644 --- a/sources/pyside6/libpyside/pysideqhash.h +++ b/sources/pyside6/libpyside/pysideqhash.h @@ -14,7 +14,7 @@ namespace PySide /// Hash function used to enable hash on objects not supported by the native Qt /// library which have a toString() function. template<class T> -inline Py_ssize_t hash(const T& value) +[[deprecated]] inline Py_ssize_t hash(const T& value) { return qHash(value.toString()); } diff --git a/sources/pyside6/tests/QtCore/hash_test.py b/sources/pyside6/tests/QtCore/hash_test.py index acd100786..aee2f516c 100644 --- a/sources/pyside6/tests/QtCore/hash_test.py +++ b/sources/pyside6/tests/QtCore/hash_test.py @@ -15,13 +15,17 @@ from PySide6.QtCore import QDate, QDateTime, QTime, QUrl from PySide6.QtCore import QLine, QPoint, QRect, QSize +URL = "https://qt.io/" + + class HashTest(unittest.TestCase): def testInsert(self): myHash = {} qdate = QDate.currentDate() qdatetime = QDateTime.currentDateTime() qtime = QTime.currentTime() - qurl = QUrl("http://www.pyside.org") + qurl = QUrl(URL) + self.assertTrue(qurl.isValid()) qpoint = QPoint(12, 42) myHash[qdate] = "QDate" @@ -64,7 +68,34 @@ class HashTest(unittest.TestCase): self.assertEqual(l1, l2) self.assertEqual(hash(l1), hash(l2)) + def testQTimeHash(self): + t1 = QTime(5, 5, 5) + t2 = QTime(5, 5, 5) + self.assertFalse(t1 is t2) + self.assertEqual(t1, t2) + self.assertEqual(hash(t1), hash(t2)) + + def testQDateHash(self): + d1 = QDate(1968, 3, 9) + d2 = QDate(1968, 3, 9) + self.assertFalse(d1 is d2) + self.assertEqual(d1, d2) + self.assertEqual(hash(d1), hash(d2)) + + def testQDateTimeHash(self): + d1 = QDateTime(QDate(1968, 3, 9), QTime(5, 5, 5)) + d2 = QDateTime(QDate(1968, 3, 9), QTime(5, 5, 5)) + self.assertFalse(d1 is d2) + self.assertEqual(d1, d2) + self.assertEqual(hash(d1), hash(d2)) + + def testQUrlHash(self): + u1 = QUrl(URL) + u2 = QUrl(URL) + self.assertFalse(u1 is u2) + self.assertEqual(u1, u2) + self.assertEqual(hash(u1), hash(u2)) + if __name__ == '__main__': unittest.main() - |
