Skip to content

Commit e71fa3a

Browse files
[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 b215552) Co-authored-by: Samartha <samarthat9@gmail.com>
1 parent 68c47df commit e71fa3a

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
@@ -10285,6 +10285,17 @@ def test_args_kwargs(self):
1028510285
self.assertEqual(repr(P.args), "P.args")
1028610286
self.assertEqual(repr(P.kwargs), "P.kwargs")
1028710287

10288+
def test_args_kwargs_weakrefs(self):
10289+
P = ParamSpec('P')
10290+
for attr_name in ('args', 'kwargs'):
10291+
with self.subTest(attr_name=attr_name):
10292+
callback_fired = []
10293+
attr = getattr(P, attr_name)
10294+
ref = weakref.ref(attr, lambda _: callback_fired.append(True))
10295+
del attr
10296+
self.assertEqual(callback_fired, [True])
10297+
self.assertIsNone(ref())
10298+
1028810299
def test_stringized(self):
1028910300
P = ParamSpec('P')
1029010301
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)