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

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
API cleanup
  • Loading branch information
eduardo-elizondo committed Feb 21, 2022
commit 56ef9144c6cee0cc747b517fbbc5a0ad6ff4e48d
3 changes: 2 additions & 1 deletion Include/moduleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ PyAPI_FUNC(const char *) PyModule_GetName(PyObject *);
Py_DEPRECATED(3.2) PyAPI_FUNC(const char *) PyModule_GetFilename(PyObject *);
PyAPI_FUNC(PyObject *) PyModule_GetFilenameObject(PyObject *);
#ifndef Py_LIMITED_API
PyAPI_FUNC(void) _PyModule_Clear(PyObject *, int phase);
PyAPI_FUNC(void) _PyModule_Clear(PyObject *);
PyAPI_FUNC(void) _PyModule_PhasedClear(PyObject *, int phase);
PyAPI_FUNC(void) _PyModule_ClearDict(PyObject *);
PyAPI_FUNC(int) _PyModuleSpec_IsInitializing(PyObject *);
#endif
Expand Down
40 changes: 22 additions & 18 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ PyModule_GetState(PyObject* m)
}

void
_PyModule_ClearPhaseOne(PyObject *d)
module_dict_clear_phase_one(PyObject *d)
{
/* Phase one only clears names starting with a single underscore
* while also excluding modules from being deleted
Expand All @@ -584,13 +584,13 @@ _PyModule_ClearPhaseOne(PyObject *d)
int verbose = _Py_GetConfig()->verbose;
while (PyDict_Next(d, &pos, &key, &value)) {
if (value == Py_None || PyModule_Check(value) || !PyUnicode_Check(key)) {
continue;
continue;
}
if (PyUnicode_READ_CHAR(key, 0) != '_') {
continue;
continue;
}
if (PyUnicode_READ_CHAR(key, 1) == '_') {
continue;
continue;
}
if (verbose > 1) {
const char *s = PyUnicode_AsUTF8(key);
Expand All @@ -599,15 +599,14 @@ _PyModule_ClearPhaseOne(PyObject *d)
else
PyErr_Clear();
}
// printf("# Phase 1 - Clearing: %s\n", PyUnicode_AsUTF8(PyObject_Repr(key)));
if (PyDict_SetItem(d, key, Py_None) != 0) {
PyErr_WriteUnraisable(NULL);
}
}
}

void
_PyModule_ClearPhaseTwo(PyObject *d)
module_dict_clear_phase_two(PyObject *d)
{
/* Phase two, clears all names except for __builtins__ and modules */
Py_ssize_t pos;
Expand All @@ -619,12 +618,12 @@ _PyModule_ClearPhaseTwo(PyObject *d)
int verbose = _Py_GetConfig()->verbose;
while (PyDict_Next(d, &pos, &key, &value)) {
if (value == Py_None || PyModule_Check(value) || !PyUnicode_Check(key)) {
continue;
continue;
}
if (PyUnicode_READ_CHAR(key, 0) == '_' ||
_PyUnicode_EqualToASCIIString(key, "__builtins__"))
{
continue;
continue;
}
if (verbose > 1) {
const char *s = PyUnicode_AsUTF8(key);
Expand All @@ -633,15 +632,14 @@ _PyModule_ClearPhaseTwo(PyObject *d)
else
PyErr_Clear();
}
// printf("# Phase 2 - Clearing: %s\n", PyUnicode_AsUTF8(PyObject_Repr(key)));
if (PyDict_SetItem(d, key, Py_None) != 0) {
PyErr_WriteUnraisable(NULL);
}
}
}

void
_PyModule_ClearPhaseThree(PyObject *d)
module_dict_clear_phase_three(PyObject *d)
{
/* Phase three, clears all modules except __builtins__*/
Py_ssize_t pos;
Expand All @@ -653,10 +651,10 @@ _PyModule_ClearPhaseThree(PyObject *d)
int verbose = _Py_GetConfig()->verbose;
while (PyDict_Next(d, &pos, &key, &value)) {
if (value == Py_None || !PyModule_Check(value) || !PyUnicode_Check(key)) {
continue;
continue;
}
if (_PyUnicode_EqualToASCIIString(key, "__builtins__")) {
continue;
continue;
}
if (verbose > 1) {
const char *s = PyUnicode_AsUTF8(key);
Expand All @@ -665,29 +663,35 @@ _PyModule_ClearPhaseThree(PyObject *d)
else
PyErr_Clear();
}
// printf("# Phase 3 - Clearing: %s\n", PyUnicode_AsUTF8(PyObject_Repr(key)));
if (PyDict_SetItem(d, key, Py_None) != 0) {
PyErr_WriteUnraisable(NULL);
}
}
}

void
_PyModule_Clear(PyObject *m, int phase)
_PyModule_Clear(PyObject *m)
{
PyObject *d = ((PyModuleObject *)m)->md_dict;
if (d != NULL)
_PyModule_ClearDict(d);
}

void
_PyModule_PhasedClear(PyObject *m, int phase)
{
PyObject *d = ((PyModuleObject *)m)->md_dict;
if (d == NULL) {
return;
}
// printf("# Module: %s\n", PyUnicode_AsUTF8(PyObject_Repr(((PyModuleObject *)m)->md_name)));
if (phase == 1) {
_PyModule_ClearPhaseOne(d);
module_dict_clear_phase_one(d);
}
if (phase == 2) {
_PyModule_ClearPhaseTwo(d);
module_dict_clear_phase_two(d);
}
if (phase == 3) {
_PyModule_ClearPhaseThree(d);
module_dict_clear_phase_three(d);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,7 @@ finalize_modules_clear_weaklist(PyThreadState *tstate,
if (verbose && PyUnicode_Check(name)) {
PySys_FormatStderr("# cleanup[3] wiping %U\n", name);
}
_PyModule_Clear(mod, phase);
_PyModule_PhasedClear(mod, phase);
if (max_module_phase) {
Py_DECREF(mod);
}
Expand Down