Skip to content

gh-132413: Clear weakref to _datetime after modules are finalized #136152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
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
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy