Skip to content

Commit f6cd6f8

Browse files
committed
pythongh-132713: Fix repr(classmethod) race condition
Hold a strong reference to the callable while calling repr(classmethod).
1 parent 22bc953 commit f6cd6f8

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix ``repr(classmethod)`` race condition: hold a strong reference to the
2+
callable while calling ``repr(classmethod)``. Patch by Victor Stinner.

Objects/funcobject.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,12 @@ static PyObject*
14881488
cm_repr(PyObject *self)
14891489
{
14901490
classmethod *cm = _PyClassMethod_CAST(self);
1491-
return PyUnicode_FromFormat("<classmethod(%R)>", cm->cm_callable);
1491+
// gh-132713: Hold a strong reference since cm_init() can be called
1492+
// in parallel
1493+
PyObject *callable = Py_NewRef(cm->cm_callable);
1494+
PyObject *repr = PyUnicode_FromFormat("<classmethod(%R)>", callable);
1495+
Py_DECREF(callable);
1496+
return repr;
14921497
}
14931498

14941499
PyDoc_STRVAR(classmethod_doc,

0 commit comments

Comments
 (0)