[Python-checkins] gh-105927: finalize_modules_clear_weaklist() uses _PyWeakref_GET_REF() (#105971)
vstinner
webhook-mailer at python.org
Wed Jun 21 15:50:24 EDT 2023
https://github.com/python/cpython/commit/4328dc646517f9251bdf6c827014f01c5229e8d9
commit: 4328dc646517f9251bdf6c827014f01c5229e8d9
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2023-06-21T21:50:20+02:00
summary:
gh-105927: finalize_modules_clear_weaklist() uses _PyWeakref_GET_REF() (#105971)
finalize_modules_clear_weaklist() now holds a strong reference to the
module longer than before: replace PyWeakref_GET_OBJECT() with
_PyWeakref_GET_REF().
files:
M Include/internal/pycore_moduleobject.h
M Objects/moduleobject.c
M Python/pylifecycle.c
diff --git a/Include/internal/pycore_moduleobject.h b/Include/internal/pycore_moduleobject.h
index 15a1bcb6ae516..31a31e724d0b2 100644
--- a/Include/internal/pycore_moduleobject.h
+++ b/Include/internal/pycore_moduleobject.h
@@ -33,7 +33,7 @@ static inline PyObject* _PyModule_GetDict(PyObject *mod) {
PyObject *dict = ((PyModuleObject *)mod) -> md_dict;
// _PyModule_GetDict(mod) must not be used after calling module_clear(mod)
assert(dict != NULL);
- return dict;
+ return dict; // borrowed reference
}
PyObject* _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress);
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index 985be58d02c78..bda25c881845c 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -504,7 +504,7 @@ PyModule_GetDict(PyObject *m)
PyErr_BadInternalCall();
return NULL;
}
- return _PyModule_GetDict(m);
+ return _PyModule_GetDict(m); // borrowed reference
}
PyObject*
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index ff9694886b6a4..5a5b14fbb0314 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -28,6 +28,7 @@
#include "pycore_typeobject.h" // _PyTypes_InitTypes()
#include "pycore_typevarobject.h" // _Py_clear_generic_types()
#include "pycore_unicodeobject.h" // _PyUnicode_InitTypes()
+#include "pycore_weakref.h" // _PyWeakref_GET_REF()
#include "opcode.h"
#include <locale.h> // setlocale()
@@ -1464,16 +1465,16 @@ finalize_modules_clear_weaklist(PyInterpreterState *interp,
for (Py_ssize_t i = PyList_GET_SIZE(weaklist) - 1; i >= 0; i--) {
PyObject *tup = PyList_GET_ITEM(weaklist, i);
PyObject *name = PyTuple_GET_ITEM(tup, 0);
- PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));
- if (mod == Py_None) {
+ PyObject *mod = _PyWeakref_GET_REF(PyTuple_GET_ITEM(tup, 1));
+ if (mod == NULL) {
continue;
}
assert(PyModule_Check(mod));
- PyObject *dict = PyModule_GetDict(mod);
+ PyObject *dict = _PyModule_GetDict(mod); // borrowed reference
if (dict == interp->builtins || dict == interp->sysdict) {
+ Py_DECREF(mod);
continue;
}
- Py_INCREF(mod);
if (verbose && PyUnicode_Check(name)) {
PySys_FormatStderr("# cleanup[3] wiping %U\n", name);
}
More information about the Python-checkins
mailing list