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
20 changes: 20 additions & 0 deletions Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,26 @@ def __hash__(self):
with self.assertRaises(KeyError):
d.get(key2)

def test_unhashable_key_preserves_exception_info(self):
"""gh-149313: exception notes and cause must survive the improved message."""
class Key:
def __hash__(self):
try:
hash([])
except TypeError as e:
e.add_note("custom note")
raise

with self.assertRaises(TypeError) as cm:
{Key(): 1}

exc = cm.exception
self.assertIn("cannot use", str(exc))
# The original exception is chained as __cause__
self.assertIsNotNone(exc.__cause__)
self.assertIn("unhashable type: 'list'", str(exc.__cause__))
self.assertIn("custom note", exc.__cause__.__notes__[0])

def test_clear_at_lookup(self):
# gh-140551 dict crash if clear is called at lookup stage
class X:
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,25 @@ def __hash__(self):
with self.assertRaises(KeyError):
myset.discard(elem2)

def test_unhashable_element_preserves_exception_info(self):
"""gh-149313: exception notes and cause must survive the improved message."""
class Elem:
def __hash__(self):
try:
hash([])
except TypeError as e:
e.add_note("custom note")
raise

with self.assertRaises(TypeError) as cm:
{Elem()}

exc = cm.exception
self.assertIn("cannot use", str(exc))
self.assertIsNotNone(exc.__cause__)
self.assertIn("unhashable type: 'list'", str(exc.__cause__))
self.assertIn("custom note", exc.__cause__.__notes__[0])

def test_hash_collision_remove_add(self):
self.maxDiff = None
# There should be enough space, so all elements with unique hash
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Preserve original exception's ``__notes__``, ``__traceback__``, and
``__cause__`` when rewriting :exc:`TypeError` for unhashable dict keys and
set elements.
4 changes: 4 additions & 0 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2416,7 +2416,11 @@ dict_unhashable_type(PyObject *op, PyObject *key)
errmsg = "cannot use '%T' as a dict key (%S)";
}
PyErr_Format(PyExc_TypeError, errmsg, key, exc);
PyObject *exc2 = PyErr_GetRaisedException();
PyException_SetCause(exc2, Py_NewRef(exc));
PyException_SetContext(exc2, Py_NewRef(exc));
Py_DECREF(exc);
PyErr_SetRaisedException(exc2);
}

Py_ssize_t
Expand Down
4 changes: 4 additions & 0 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,11 @@ set_unhashable_type(PyObject *key)
PyErr_Format(PyExc_TypeError,
"cannot use '%T' as a set element (%S)",
key, exc);
PyObject *exc2 = PyErr_GetRaisedException();
PyException_SetCause(exc2, Py_NewRef(exc));
PyException_SetContext(exc2, Py_NewRef(exc));
Py_DECREF(exc);
PyErr_SetRaisedException(exc2);
}

int
Expand Down
Loading