Skip to content

Commit 5634116

Browse files
[3.13] gh-157621: Clear weakrefs for ParamSpec attributes (GH-157622) (#157655)
gh-157621: Clear weakrefs for ParamSpec attributes (GH-157622) (cherry picked from commit b215552) Co-authored-by: Samartha <samarthat9@gmail.com>
1 parent 0557ba8 commit 5634116

3 files changed

Lines changed: 14 additions & 0 deletions

File tree

‎Lib/test/test_typing.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9767,6 +9767,17 @@ def test_args_kwargs(self):
97679767
self.assertEqual(repr(P.args), "P.args")
97689768
self.assertEqual(repr(P.kwargs), "P.kwargs")
97699769

9770+
def test_args_kwargs_weakrefs(self):
9771+
P = ParamSpec('P')
9772+
for attr_name in ('args', 'kwargs'):
9773+
with self.subTest(attr_name=attr_name):
9774+
callback_fired = []
9775+
attr = getattr(P, attr_name)
9776+
ref = weakref.ref(attr, lambda _: callback_fired.append(True))
9777+
del attr
9778+
self.assertEqual(callback_fired, [True])
9779+
self.assertIsNone(ref())
9780+
97709781
def test_stringized(self):
97719782
P = ParamSpec('P')
97729783
class C(Generic[P]):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash when a weak-reference callback is attached to a
2+
:class:`typing.ParamSpecArgs` or :class:`typing.ParamSpecKwargs` instance.

‎Objects/typevarobject.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ paramspecattr_dealloc(PyObject *self)
692692
_PyObject_GC_UNTRACK(self);
693693

694694
Py_XDECREF(psa->__origin__);
695+
PyObject_ClearWeakRefs(self);
695696

696697
Py_TYPE(self)->tp_free(self);
697698
Py_DECREF(tp);

0 commit comments

Comments
 (0)