Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-153695: Fix sqlite3.Row.__hash__ with an unhashable value (GH-153696)
(cherry picked from commit b704b64)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
  • Loading branch information
tonghuaroot authored and miss-islington committed Jul 14, 2026
commit 70e81e6bf6e8901aed3d1c348f4a9b54c5c58067
11 changes: 11 additions & 0 deletions Lib/test/test_sqlite3/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ def test_sqlite_row_hash_cmp(self):

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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 9 additions & 1 deletion Modules/_sqlite/row.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
Loading