Skip to content

Commit

Permalink
pythongh-105927: finalize_modules_clear_weaklist() uses _PyWeakref_GE…
Browse files Browse the repository at this point in the history
…T_REF() (python#105971)

finalize_modules_clear_weaklist() now holds a strong reference to the
module longer than before: replace PyWeakref_GET_OBJECT() with
_PyWeakref_GET_REF().
  • Loading branch information
vstinner authored and bentasker committed Jun 23, 2023
1 parent 237c7ff commit 62c676f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Include/internal/pycore_moduleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ PyModule_GetDict(PyObject *m)
PyErr_BadInternalCall();
return NULL;
}
return _PyModule_GetDict(m);
return _PyModule_GetDict(m); // borrowed reference
}

PyObject*
Expand Down
9 changes: 5 additions & 4 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 62c676f

Please sign in to comment.