File tree Expand file tree Collapse file tree 1 file changed +11
-1
lines changed
Expand file tree Collapse file tree 1 file changed +11
-1
lines changed Original file line number Diff line number Diff line change @@ -1554,7 +1554,17 @@ finalize_remove_modules(PyObject *modules, int verbose)
15541554 if (weaklist != NULL) { \
15551555 PyObject *wr = PyWeakref_NewRef(mod, NULL); \
15561556 if (wr) { \
1557- PyObject *tup = PyTuple_Pack(2, name, wr); \
1557+ 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+ } \
15581568 if (!tup || PyList_Append (weaklist , tup ) < 0 ) { \
15591569 PyErr_FormatUnraisable ("Exception ignored while removing modules" ); \
15601570 } \
You can’t perform that action at this time.
0 commit comments