Skip to content

Commit b215552

Browse files
authored
gh-157621: Clear weakrefs for ParamSpec attributes (#157622)
1 parent b42dcf6 commit b215552

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
@@ -10299,6 +10299,17 @@ def test_args_kwargs(self):
1029910299
self.assertEqual(repr(P.args), "P.args")
1030010300
self.assertEqual(repr(P.kwargs), "P.kwargs")
1030110301

10302+
def test_args_kwargs_weakrefs(self):
10303+
P = ParamSpec('P')
10304+
for attr_name in ('args', 'kwargs'):
10305+
with self.subTest(attr_name=attr_name):
10306+
callback_fired = []
10307+
attr = getattr(P, attr_name)
10308+
ref = weakref.ref(attr, lambda _: callback_fired.append(True))
10309+
del attr
10310+
self.assertEqual(callback_fired, [True])
10311+
self.assertIsNone(ref())
10312+
1030210313
def test_stringized(self):
1030310314
P = ParamSpec('P')
1030410315
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
@@ -958,6 +958,7 @@ paramspecattr_dealloc(PyObject *self)
958958
_PyObject_GC_UNTRACK(self);
959959

960960
Py_XDECREF(psa->__origin__);
961+
PyObject_ClearWeakRefs(self);
961962

962963
Py_TYPE(self)->tp_free(self);
963964
Py_DECREF(tp);

0 commit comments

Comments
 (0)