Skip to content

Commit 4328dc6

Browse files
authored
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().
1 parent fc32522 commit 4328dc6

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

Include/internal/pycore_moduleobject.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static inline PyObject* _PyModule_GetDict(PyObject *mod) {
3333
PyObject *dict = ((PyModuleObject *)mod) -> md_dict;
3434
// _PyModule_GetDict(mod) must not be used after calling module_clear(mod)
3535
assert(dict != NULL);
36-
return dict;
36+
return dict; // borrowed reference
3737
}
3838

3939
PyObject* _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress);

Objects/moduleobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ PyModule_GetDict(PyObject *m)
504504
PyErr_BadInternalCall();
505505
return NULL;
506506
}
507-
return _PyModule_GetDict(m);
507+
return _PyModule_GetDict(m); // borrowed reference
508508
}
509509

510510
PyObject*

Python/pylifecycle.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "pycore_typeobject.h" // _PyTypes_InitTypes()
2929
#include "pycore_typevarobject.h" // _Py_clear_generic_types()
3030
#include "pycore_unicodeobject.h" // _PyUnicode_InitTypes()
31+
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
3132
#include "opcode.h"
3233

3334
#include <locale.h> // setlocale()
@@ -1464,16 +1465,16 @@ finalize_modules_clear_weaklist(PyInterpreterState *interp,
14641465
for (Py_ssize_t i = PyList_GET_SIZE(weaklist) - 1; i >= 0; i--) {
14651466
PyObject *tup = PyList_GET_ITEM(weaklist, i);
14661467
PyObject *name = PyTuple_GET_ITEM(tup, 0);
1467-
PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));
1468-
if (mod == Py_None) {
1468+
PyObject *mod = _PyWeakref_GET_REF(PyTuple_GET_ITEM(tup, 1));
1469+
if (mod == NULL) {
14691470
continue;
14701471
}
14711472
assert(PyModule_Check(mod));
1472-
PyObject *dict = PyModule_GetDict(mod);
1473+
PyObject *dict = _PyModule_GetDict(mod); // borrowed reference
14731474
if (dict == interp->builtins || dict == interp->sysdict) {
1475+
Py_DECREF(mod);
14741476
continue;
14751477
}
1476-
Py_INCREF(mod);
14771478
if (verbose && PyUnicode_Check(name)) {
14781479
PySys_FormatStderr("# cleanup[3] wiping %U\n", name);
14791480
}

0 commit comments

Comments
 (0)