Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 79ba388

Browse files
committed
Improve verbose reporting of shutdown phase by using the "public" module name
1 parent 257cf2f commit 79ba388

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

Python/import.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,17 +345,17 @@ PyImport_Cleanup(void)
345345
for diagnosis messages (in verbose mode), while the weakref helps
346346
detect those modules which have been held alive. */
347347
weaklist = PyList_New(0);
348+
if (weaklist == NULL)
349+
PyErr_Clear();
348350

349-
#define STORE_MODULE_WEAKREF(mod) \
351+
#define STORE_MODULE_WEAKREF(name, mod) \
350352
if (weaklist != NULL) { \
351-
PyObject *name = PyModule_GetNameObject(mod); \
352353
PyObject *wr = PyWeakref_NewRef(mod, NULL); \
353354
if (name && wr) { \
354355
PyObject *tup = PyTuple_Pack(2, name, wr); \
355356
PyList_Append(weaklist, tup); \
356357
Py_XDECREF(tup); \
357358
} \
358-
Py_XDECREF(name); \
359359
Py_XDECREF(wr); \
360360
if (PyErr_Occurred()) \
361361
PyErr_Clear(); \
@@ -368,7 +368,7 @@ PyImport_Cleanup(void)
368368
if (PyModule_Check(value)) {
369369
if (Py_VerboseFlag && PyUnicode_Check(key))
370370
PySys_FormatStderr("# cleanup[2] removing %U\n", key, value);
371-
STORE_MODULE_WEAKREF(value);
371+
STORE_MODULE_WEAKREF(key, value);
372372
PyDict_SetItem(modules, key, Py_None);
373373
}
374374
}
@@ -394,14 +394,15 @@ PyImport_Cleanup(void)
394394
n = PyList_GET_SIZE(weaklist);
395395
for (i = 0; i < n; i++) {
396396
PyObject *tup = PyList_GET_ITEM(weaklist, i);
397+
PyObject *name = PyTuple_GET_ITEM(tup, 0);
397398
PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));
398399
if (mod == Py_None)
399400
continue;
400401
Py_INCREF(mod);
401402
assert(PyModule_Check(mod));
402-
if (Py_VerboseFlag)
403+
if (Py_VerboseFlag && PyUnicode_Check(name))
403404
PySys_FormatStderr("# cleanup[3] wiping %U\n",
404-
PyTuple_GET_ITEM(tup, 0), mod);
405+
name, mod);
405406
_PyModule_Clear(mod);
406407
Py_DECREF(mod);
407408
}

0 commit comments

Comments
 (0)