From 58c43da11beda2ebda5ffe571602f69e56e4cdfd Mon Sep 17 00:00:00 2001 From: GruffElixir <136691411+gruffelixir@users.noreply.github.com> Date: Wed, 16 Sep 2026 19:30:27 +0530 Subject: [PATCH 1/3] gh-157621: Clear ParamSpec attribute weakrefs --- Lib/test/test_typing.py | 11 +++++++++++ Objects/typevarobject.c | 1 + 2 files changed, 12 insertions(+) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index f35f864dce21e86..b0d468b34092b25 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -10299,6 +10299,17 @@ def test_args_kwargs(self): self.assertEqual(repr(P.args), "P.args") self.assertEqual(repr(P.kwargs), "P.kwargs") + def test_args_kwargs_weakrefs(self): + P = ParamSpec('P') + for attr_name in ('args', 'kwargs'): + with self.subTest(attr_name=attr_name): + callback_fired = [] + attr = getattr(P, attr_name) + ref = weakref.ref(attr, lambda _: callback_fired.append(True)) + del attr + self.assertEqual(callback_fired, [True]) + self.assertIsNone(ref()) + def test_stringized(self): P = ParamSpec('P') class C(Generic[P]): diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index 53c350838969616..eed1457cbb426d9 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -958,6 +958,7 @@ paramspecattr_dealloc(PyObject *self) _PyObject_GC_UNTRACK(self); Py_XDECREF(psa->__origin__); + PyObject_ClearWeakRefs(self); Py_TYPE(self)->tp_free(self); Py_DECREF(tp); From 785678849b43c675e797cb173251c173a8a8d0df Mon Sep 17 00:00:00 2001 From: GruffElixir <136691411+gruffelixir@users.noreply.github.com> Date: Wed, 16 Sep 2026 19:35:03 +0530 Subject: [PATCH 2/3] gh-157621: Add NEWS entry --- .../NEWS.d/next/Library/2026-09-16-14-04-38.gh-issue-157621.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-09-16-14-04-38.gh-issue-157621.rst diff --git a/Misc/NEWS.d/next/Library/2026-09-16-14-04-38.gh-issue-157621.rst b/Misc/NEWS.d/next/Library/2026-09-16-14-04-38.gh-issue-157621.rst new file mode 100644 index 000000000000000..b8342da0faebb91 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-16-14-04-38.gh-issue-157621.rst @@ -0,0 +1,2 @@ +Fix a crash when a weak-reference callback is attached to a +:class:`typing.ParamSpecArgs` or :class:`typing.ParamSpecKwargs` instance. From 2ba5aca2a9d785a2b5030708d02cfa828dde0452 Mon Sep 17 00:00:00 2001 From: GruffElixir <136691411+gruffelixir@users.noreply.github.com> Date: Wed, 16 Sep 2026 19:43:22 +0530 Subject: [PATCH 3/3] gh-157621: Fix NEWS fragment filename --- ...-157621.rst => 2026-09-16-14-04-38.gh-issue-157621.R6v2S8.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/Library/{2026-09-16-14-04-38.gh-issue-157621.rst => 2026-09-16-14-04-38.gh-issue-157621.R6v2S8.rst} (100%) diff --git a/Misc/NEWS.d/next/Library/2026-09-16-14-04-38.gh-issue-157621.rst b/Misc/NEWS.d/next/Library/2026-09-16-14-04-38.gh-issue-157621.R6v2S8.rst similarity index 100% rename from Misc/NEWS.d/next/Library/2026-09-16-14-04-38.gh-issue-157621.rst rename to Misc/NEWS.d/next/Library/2026-09-16-14-04-38.gh-issue-157621.R6v2S8.rst