We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4f83cb0 commit 4e83b5bCopy full SHA for 4e83b5b
1 file changed
Python/pylifecycle.c
@@ -1554,7 +1554,17 @@ finalize_remove_modules(PyObject *modules, int verbose)
1554
if (weaklist != NULL) { \
1555
PyObject *wr = PyWeakref_NewRef(mod, NULL); \
1556
if (wr) { \
1557
- PyObject *tup = PyTuple_Pack(2, name, wr); \
+ PyObject *tup; \
1558
+ if (Py_REFCNT(wr) > 1) { \
1559
+ /* gh-132413: When the weakref is already used
1560
+ * elsewhere, keep the referenced module alive
1561
+ * until finalize_modules_clear_weaklist() finishes.
1562
+ */ \
1563
+ tup = PyTuple_Pack(3, name, wr, mod); \
1564
+ } \
1565
+ else { \
1566
+ tup = PyTuple_Pack(2, name, wr); \
1567
1568
if (!tup || PyList_Append(weaklist, tup) < 0) { \
1569
PyErr_FormatUnraisable("Exception ignored while removing modules"); \
1570
} \
0 commit comments