gh-132002: Fix crash of ContextVar
on unhashable str
subtype (#132003)
This commit is contained in:
parent
87d9983994
commit
ab2a3dda1d
@ -92,6 +92,15 @@ class ContextTest(unittest.TestCase):
|
|||||||
contextvars.Context(a=1)
|
contextvars.Context(a=1)
|
||||||
contextvars.Context(**{})
|
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):
|
def test_context_typerrors_1(self):
|
||||||
ctx = contextvars.Context()
|
ctx = contextvars.Context()
|
||||||
|
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
Fix crash when deallocating :class:`contextvars.ContextVar` with weird
|
||||||
|
unahashable string names.
|
@ -878,14 +878,7 @@ contextvar_new(PyObject *name, PyObject *def)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
var->var_hash = contextvar_generate_hash(var, name);
|
|
||||||
if (var->var_hash == -1) {
|
|
||||||
Py_DECREF(var);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
var->var_name = Py_NewRef(name);
|
var->var_name = Py_NewRef(name);
|
||||||
|
|
||||||
var->var_default = Py_XNewRef(def);
|
var->var_default = Py_XNewRef(def);
|
||||||
|
|
||||||
#ifndef Py_GIL_DISABLED
|
#ifndef Py_GIL_DISABLED
|
||||||
@ -894,6 +887,12 @@ contextvar_new(PyObject *name, PyObject *def)
|
|||||||
var->var_cached_tsver = 0;
|
var->var_cached_tsver = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
var->var_hash = contextvar_generate_hash(var, name);
|
||||||
|
if (var->var_hash == -1) {
|
||||||
|
Py_DECREF(var);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (_PyObject_GC_MAY_BE_TRACKED(name) ||
|
if (_PyObject_GC_MAY_BE_TRACKED(name) ||
|
||||||
(def != NULL && _PyObject_GC_MAY_BE_TRACKED(def)))
|
(def != NULL && _PyObject_GC_MAY_BE_TRACKED(def)))
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user