Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions Lib/test/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ def test_context_new_1(self):
contextvars.Context(a=1)
contextvars.Context(**{})

def test_context_new_unhashable_str_subclass(self):
# gh-132002: it used to crash on unhashable str subtypes.
class weird_str(str):
def __eq__(self, other):
pass

with self.assertRaisesRegex(TypeError, 'unhashable type'):
contextvars.ContextVar(weird_str())

def test_context_typerrors_1(self):
ctx = contextvars.Context()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash when deallocating :class:`contextvars.ContextVar` with weird
unahashable string names.
2 changes: 1 addition & 1 deletion Python/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ contextvar_new(PyObject *name, PyObject *def)

var->var_hash = contextvar_generate_hash(var, name);
if (var->var_hash == -1) {
Py_DECREF(var);
PyObject_GC_Del(var);
return NULL;
}

Expand Down
Loading