-
-
Notifications
You must be signed in to change notification settings - Fork 35k
[WIP] bpo-42671: Make Python finalization deterministic #23826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| Make the Python finalization more deterministic: | ||
|
|
||
| * First, clear the ``__main__`` module. | ||
| * Then, clear modules from the most recently imported to the least | ||
| recently imported: ``reversed(sys.modules)``. | ||
| * :mod:`builtins` and :mod:`sys` modules are always cleared last. | ||
| * Module attributes are set to None from the most recently defined to the | ||
| least recently defined: ``reversed(module.__dict__)``. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -559,65 +559,88 @@ PyModule_GetState(PyObject* m) | |
| void | ||
| _PyModule_Clear(PyObject *m) | ||
| { | ||
| PyObject *d = ((PyModuleObject *)m)->md_dict; | ||
| if (d != NULL) | ||
| _PyModule_ClearDict(d); | ||
| PyModuleObject *module = (PyModuleObject *)m; | ||
| _PyModule_ClearDict(module->md_name, module->md_dict); | ||
| } | ||
|
|
||
| void | ||
| _PyModule_ClearDict(PyObject *d) | ||
| _PyModule_ClearDict(PyObject *module_name, PyObject *dict) | ||
| { | ||
| int verbose = _Py_GetConfig()->verbose; | ||
| if (verbose) { | ||
| PySys_FormatStderr("# cleanup[3] wiping %S module\n", module_name); | ||
| } | ||
|
|
||
| // If the module has no dict: there is nothing to do. | ||
| if (dict == NULL) { | ||
| return; | ||
| } | ||
|
|
||
| /* To make the execution order of destructors for global | ||
| objects a bit more predictable, we first zap all objects | ||
| whose name starts with a single underscore, before we clear | ||
| the entire dictionary. We zap them by replacing them with | ||
| None, rather than deleting them from the dictionary, to | ||
| avoid rehashing the dictionary (to some extent). */ | ||
|
|
||
| Py_ssize_t pos; | ||
| PyObject *key, *value; | ||
|
|
||
| int verbose = _Py_GetConfig()->verbose; | ||
| for (int step=1; step <= 2; step++) { | ||
| PyObject *reversed = PyObject_CallOneArg((PyObject*)&PyReversed_Type, dict); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In future we can also add
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PyDict_Keys() returns keys in their creation order, no? I would like to get them in the reverse order. Do you suggest to iterate on the list backwards? |
||
| if (reversed == NULL) { | ||
| PyErr_WriteUnraisable(NULL); | ||
| return; | ||
| } | ||
| PyObject *keys = PyObject_CallOneArg((PyObject*)&PyList_Type, reversed); | ||
| Py_DECREF(reversed); | ||
| if (keys == NULL) { | ||
| PyErr_WriteUnraisable(NULL); | ||
| return; | ||
| } | ||
| PyObject *iter = PyObject_GetIter(keys); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is easier to iterate list by index.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, you're right. My first attempt avoided the creation of a list, but then I get annoying RuntimeError when the dict was modified while we iterate on it. Deleting a variable can have many side effects, it's way safer to copy the list of keys. |
||
| Py_DECREF(keys); | ||
| if (iter == NULL) { | ||
| PyErr_WriteUnraisable(NULL); | ||
| return; | ||
| } | ||
|
|
||
| /* First, clear only names starting with a single underscore */ | ||
| pos = 0; | ||
| while (PyDict_Next(d, &pos, &key, &value)) { | ||
| if (value != Py_None && PyUnicode_Check(key)) { | ||
| if (PyUnicode_READ_CHAR(key, 0) == '_' && | ||
| PyUnicode_READ_CHAR(key, 1) != '_') { | ||
| if (verbose > 1) { | ||
| const char *s = PyUnicode_AsUTF8(key); | ||
| if (s != NULL) | ||
| PySys_WriteStderr("# clear[1] %s\n", s); | ||
| else | ||
| PyErr_Clear(); | ||
| } | ||
| if (PyDict_SetItem(d, key, Py_None) != 0) { | ||
| PyErr_WriteUnraisable(NULL); | ||
| } | ||
| /* First, clear only names starting with a single underscore */ | ||
| PyObject *key; | ||
| while ((key = PyIter_Next(iter))) { | ||
| assert(!PyErr_Occurred()); | ||
| PyObject *value = PyObject_GetItem(dict, key); | ||
| Py_XDECREF(value); // only the value pointer is useful | ||
| if (value == Py_None) { | ||
| continue; | ||
| } | ||
| if (value == NULL) { | ||
| // ignore error | ||
| PyErr_Clear(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /* Next, clear all names except for __builtins__ */ | ||
| pos = 0; | ||
| while (PyDict_Next(d, &pos, &key, &value)) { | ||
| if (value != Py_None && PyUnicode_Check(key)) { | ||
| if (PyUnicode_READ_CHAR(key, 0) != '_' || | ||
| !_PyUnicode_EqualToASCIIString(key, "__builtins__")) | ||
| { | ||
| if (verbose > 1) { | ||
| const char *s = PyUnicode_AsUTF8(key); | ||
| if (s != NULL) | ||
| PySys_WriteStderr("# clear[2] %s\n", s); | ||
| else | ||
| PyErr_Clear(); | ||
| if (PyUnicode_Check(key)) { | ||
| if (step == 1) { | ||
| if (PyUnicode_READ_CHAR(key, 0) != '_' || | ||
| PyUnicode_READ_CHAR(key, 1) == '_') { | ||
| continue; | ||
| } | ||
| } | ||
| if (PyDict_SetItem(d, key, Py_None) != 0) { | ||
| PyErr_WriteUnraisable(NULL); | ||
| else { | ||
| /* Step 2: clear all names except for __builtins__ */ | ||
| if (_PyUnicode_EqualToASCIIString(key, "__builtins__")) { | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
| if (verbose > 1) { | ||
| PySys_FormatStderr("# clear[%i] %S.%S\n", | ||
| step, module_name, key); | ||
| } | ||
| assert(!PyErr_Occurred()); | ||
| if (PyDict_SetItem(dict, key, Py_None) != 0) { | ||
| PyErr_WriteUnraisable(NULL); | ||
| } | ||
| Py_DECREF(key); | ||
| } | ||
| Py_DECREF(iter); | ||
| } | ||
|
|
||
| /* Note: we leave __builtins__ in place, so that destructors | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that it may be better now to clear all objects in the same order, without prioritizing underscored names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh. I didn't know, so I chose the conservative option: keep the existing "heuristic". I'm fine with making the cleanup simpler :-)