Visitar URL original
gh-132413: Clear weakref to _datetime after modules are finalized by neonene · Pull Request #136152 · python/cpython · GitHub
Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Sort weaklist
  • Loading branch information
neonene authored Jul 1, 2025
commit e33f704cde0c6eddf664fd2d32ad7d2ca255e3f4
24 changes: 18 additions & 6 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,10 @@ static PyObject*
finalize_remove_modules(PyObject *modules, int verbose)
{
PyObject *weaklist = PyList_New(0);
PyObject *weak_ext = PyList_New(0); // for extending the weaklist
if (weak_ext == NULL) {
Py_CLEAR(weaklist);
}
if (weaklist == NULL) {
PyErr_FormatUnraisable("Exception ignored while removing modules");
}
Expand All @@ -1554,17 +1558,19 @@ finalize_remove_modules(PyObject *modules, int verbose)
if (weaklist != NULL) { \
PyObject *wr = PyWeakref_NewRef(mod, NULL); \
if (wr) { \
if (Py_REFCNT(wr) > 1) { \
PyObject *list = Py_REFCNT(wr) <= 1 ? weaklist : weak_ext; \
PyObject *tup = PyTuple_Pack(2, name, wr); \
if (!tup || PyList_Append(list, tup) < 0) { \
PyErr_FormatUnraisable("Exception ignored while removing modules"); \
} \
if (list == weak_ext) { \
/* gh-132413: When the weakref is already used elsewhere,
* finalize_modules_clear_weaklist() rather than the GC
* should clear the referenced module since the GC tries
* to clear the wrakref first. */ \
* to clear the wrakref first. The weaklist requires the
* order in which such modules are cleared first. */ \
_PyObject_GC_UNTRACK(mod); \
} \
PyObject *tup = PyTuple_Pack(2, name, wr); \
if (!tup || PyList_Append(weaklist, tup) < 0) { \
PyErr_FormatUnraisable("Exception ignored while removing modules"); \
} \
Py_XDECREF(tup); \
Py_DECREF(wr); \
} \
Expand Down Expand Up @@ -1614,6 +1620,12 @@ finalize_remove_modules(PyObject *modules, int verbose)
Py_DECREF(iterator);
}
}

if (PyList_Extend(weaklist, weak_ext) < 0) {
PyErr_FormatUnraisable("Exception ignored while removing modules");
}
Py_DECREF(weak_ext);

#undef CLEAR_MODULE
#undef STORE_MODULE_WEAKREF

Expand Down