Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.12] gh-108295: Fix crashes with TypeVar weakrefs (GH-108517) #108527

Merged
merged 1 commit into from
Aug 27, 2023
Merged
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
10 changes: 10 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,16 @@ def test_bad_var_substitution(self):
with self.assertRaises(TypeError):
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]]:
"""Renders templates with possible combinations of replacements.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crashes related to use of weakrefs on :data:`typing.TypeVar`.
3 changes: 3 additions & 0 deletions Objects/typevarobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ typevar_dealloc(PyObject *self)
Py_XDECREF(tv->constraints);
Py_XDECREF(tv->evaluate_constraints);
_PyObject_ClearManagedDict(self);
PyObject_ClearWeakRefs(self);

Py_TYPE(self)->tp_free(self);
Py_DECREF(tp);
Expand Down Expand Up @@ -743,6 +744,7 @@ paramspec_dealloc(PyObject *self)
Py_DECREF(ps->name);
Py_XDECREF(ps->bound);
_PyObject_ClearManagedDict(self);
PyObject_ClearWeakRefs(self);

Py_TYPE(self)->tp_free(self);
Py_DECREF(tp);
Expand Down Expand Up @@ -1022,6 +1024,7 @@ typevartuple_dealloc(PyObject *self)

Py_DECREF(tvt->name);
_PyObject_ClearManagedDict(self);
PyObject_ClearWeakRefs(self);

Py_TYPE(self)->tp_free(self);
Py_DECREF(tp);
Expand Down