mirror of https://github.com/python/cpython
gh-108295: Fix crashes with TypeVar weakrefs (#108517)
This commit is contained in:
parent
2b15536fa9
commit
482fad7f01
|
@ -544,6 +544,16 @@ class TypeVarTests(BaseTestCase):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
list[T][arg]
|
list[T][arg]
|
||||||
|
|
||||||
|
def test_many_weakrefs(self):
|
||||||
|
# gh-108295: this used to segfault
|
||||||
|
for cls in (ParamSpec, TypeVarTuple, TypeVar):
|
||||||
|
with self.subTest(cls=cls):
|
||||||
|
vals = weakref.WeakValueDictionary()
|
||||||
|
|
||||||
|
for x in range(100000):
|
||||||
|
vals[x] = cls(str(x))
|
||||||
|
del vals
|
||||||
|
|
||||||
|
|
||||||
def template_replace(templates: list[str], replacements: dict[str, list[str]]) -> list[tuple[str]]:
|
def template_replace(templates: list[str], replacements: dict[str, list[str]]) -> list[tuple[str]]:
|
||||||
"""Renders templates with possible combinations of replacements.
|
"""Renders templates with possible combinations of replacements.
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix crashes related to use of weakrefs on :data:`typing.TypeVar`.
|
|
@ -201,6 +201,7 @@ typevar_dealloc(PyObject *self)
|
||||||
Py_XDECREF(tv->constraints);
|
Py_XDECREF(tv->constraints);
|
||||||
Py_XDECREF(tv->evaluate_constraints);
|
Py_XDECREF(tv->evaluate_constraints);
|
||||||
_PyObject_ClearManagedDict(self);
|
_PyObject_ClearManagedDict(self);
|
||||||
|
PyObject_ClearWeakRefs(self);
|
||||||
|
|
||||||
Py_TYPE(self)->tp_free(self);
|
Py_TYPE(self)->tp_free(self);
|
||||||
Py_DECREF(tp);
|
Py_DECREF(tp);
|
||||||
|
@ -743,6 +744,7 @@ paramspec_dealloc(PyObject *self)
|
||||||
Py_DECREF(ps->name);
|
Py_DECREF(ps->name);
|
||||||
Py_XDECREF(ps->bound);
|
Py_XDECREF(ps->bound);
|
||||||
_PyObject_ClearManagedDict(self);
|
_PyObject_ClearManagedDict(self);
|
||||||
|
PyObject_ClearWeakRefs(self);
|
||||||
|
|
||||||
Py_TYPE(self)->tp_free(self);
|
Py_TYPE(self)->tp_free(self);
|
||||||
Py_DECREF(tp);
|
Py_DECREF(tp);
|
||||||
|
@ -1022,6 +1024,7 @@ typevartuple_dealloc(PyObject *self)
|
||||||
|
|
||||||
Py_DECREF(tvt->name);
|
Py_DECREF(tvt->name);
|
||||||
_PyObject_ClearManagedDict(self);
|
_PyObject_ClearManagedDict(self);
|
||||||
|
PyObject_ClearWeakRefs(self);
|
||||||
|
|
||||||
Py_TYPE(self)->tp_free(self);
|
Py_TYPE(self)->tp_free(self);
|
||||||
Py_DECREF(tp);
|
Py_DECREF(tp);
|
||||||
|
|
Loading…
Reference in New Issue