Skip to content
Open
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
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