From fd4b97fe35020d57cfb804e36832c15c926f9ae9 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 26 Aug 2023 17:13:33 -0700 Subject: [PATCH] gh-108295: Fix crashes with TypeVar weakrefs (GH-108517) (cherry picked from commit 482fad7f01567447b7259ebf58d62999fcdc5964) Co-authored-by: Jelle Zijlstra --- Lib/test/test_typing.py | 10 ++++++++++ .../2023-08-26-08-38-57.gh-issue-108295.Pn0QRM.rst | 1 + Objects/typevarobject.c | 3 +++ 3 files changed, 14 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-08-26-08-38-57.gh-issue-108295.Pn0QRM.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index c88152de7725fb..6c9f168b63f96d 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -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. diff --git a/Misc/NEWS.d/next/Library/2023-08-26-08-38-57.gh-issue-108295.Pn0QRM.rst b/Misc/NEWS.d/next/Library/2023-08-26-08-38-57.gh-issue-108295.Pn0QRM.rst new file mode 100644 index 00000000000000..7e61ed4de75ce6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-08-26-08-38-57.gh-issue-108295.Pn0QRM.rst @@ -0,0 +1 @@ +Fix crashes related to use of weakrefs on :data:`typing.TypeVar`. diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index 97f39136996fcc..069e1d98ea4978 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -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); @@ -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); @@ -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); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy