]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.15] gh-153695: Fix sqlite3.Row.__hash__ with an unhashable value (GH-153696) ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 14 Jul 2026 22:55:51 +0000 (00:55 +0200)
committerGitHub <noreply@github.com>
Tue, 14 Jul 2026 22:55:51 +0000 (22:55 +0000)
gh-153695: Fix sqlite3.Row.__hash__ with an unhashable value (GH-153696)
(cherry picked from commit b704b647b2295924ebaf8d20ddf88925095039bb)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Lib/test/test_sqlite3/test_factory.py
Misc/NEWS.d/next/Library/2026-07-14-19-45-21.gh-issue-153695.UYdZJZ.rst [new file with mode: 0644]
Modules/_sqlite/row.c

index a9abeab31936880b681e32c275ee5996ffc0eed4..b9b18fdee87226938823ecf42f0d33f2005fde59 100644 (file)
@@ -239,6 +239,17 @@ class RowFactoryTests(MemoryDatabaseMixin, unittest.TestCase):
 
         self.assertEqual(hash(row_1), hash(row_2))
 
+    def test_sqlite_row_hash_unhashable(self):
+        # An unhashable value must raise TypeError, not SystemError.
+        sqlite.register_converter("LST", lambda b: [1, 2, 3])
+        self.addCleanup(sqlite.converters.pop, "LST", None)
+        with memory_database(detect_types=sqlite.PARSE_DECLTYPES) as con:
+            con.row_factory = sqlite.Row
+            con.execute("create table t(x LST)")
+            con.execute("insert into t values(?)", (b"x",))
+            row = con.execute("select x from t").fetchone()
+            self.assertRaisesRegex(TypeError, "unhashable", hash, row)
+
     def test_sqlite_row_as_sequence(self):
         # Checks if the row object can act like a sequence.
         row = self.con.execute("select 1 as a, 2 as b").fetchone()
diff --git a/Misc/NEWS.d/next/Library/2026-07-14-19-45-21.gh-issue-153695.UYdZJZ.rst b/Misc/NEWS.d/next/Library/2026-07-14-19-45-21.gh-issue-153695.UYdZJZ.rst
new file mode 100644 (file)
index 0000000..43d7fee
--- /dev/null
@@ -0,0 +1,2 @@
+Hashing a :class:`sqlite3.Row` that contains an unhashable value now raises
+:exc:`TypeError` instead of :exc:`SystemError`. Patch by tonghuaroot.
index 94565a01d18f245315e952bfb5763787819d13ae..8646f591ef5169f520dd77b1f78ec1dc04e4e0e9 100644 (file)
@@ -232,7 +232,15 @@ static Py_hash_t
 pysqlite_row_hash(PyObject *op)
 {
     pysqlite_Row *self = _pysqlite_Row_CAST(op);
-    return PyObject_Hash(self->description) ^ PyObject_Hash(self->data);
+    Py_hash_t hash_description = PyObject_Hash(self->description);
+    if (hash_description == -1) {
+        return -1;
+    }
+    Py_hash_t hash_data = PyObject_Hash(self->data);
+    if (hash_data == -1) {
+        return -1;
+    }
+    return hash_description ^ hash_data;
 }
 
 static PyObject *