https://github.com/python/cpython/commit/e71fa3aa53dbfb3e9b369b97cef8ac19f81c976f
commit: e71fa3aa53dbfb3e9b369b97cef8ac19f81c976f
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-09T12:13:45-07:00
summary:

[3.15] gh-157621: Clear weakrefs for ParamSpec attributes (GH-157622) (#157653)

gh-157621: Clear weakrefs for ParamSpec attributes (GH-157622)
(cherry picked from commit b215552a32666d38db5e85e1a6da91374610dd73)

Co-authored-by: Samartha <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-09-16-14-04-38.gh-issue-157621.R6v2S8.rst
M Lib/test/test_typing.py
M Objects/typevarobject.c

diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index f99e8f12e94156..be67a540e98904 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -10285,6 +10285,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/Misc/NEWS.d/next/Library/2026-09-16-14-04-38.gh-issue-157621.R6v2S8.rst 
b/Misc/NEWS.d/next/Library/2026-09-16-14-04-38.gh-issue-157621.R6v2S8.rst
new file mode 100644
index 00000000000000..b8342da0faebb9
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-09-16-14-04-38.gh-issue-157621.R6v2S8.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.
diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c
index 53c35083896961..eed1457cbb426d 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);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to