Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] tools: make module import order consistent
A long time ago, in a commit far far away: 65a3751, we introduced a mechanism to support the "import emulator" feature[^1] of cPython. It's used by some C native modules to trigger an on-the-spot module import during a low-level function call. Some of the modules that do this are `datetime` and `time`: - https://github.com/python/cpython/blob/b6535ea7ecf40c51a9a13089d961c29abeaffa5e/Modules/timemodule.c#L892 - https://github.com/python/cpython/blob/b6535ea7ecf40c51a9a13089d961c29abeaffa5e/Modules/_datetimemodule.c#L1654 Those `PyImport_ImportModuleNoBlock()` calls were initially understood as real calls that needed to return the imported module. But the actual return value is never used by PyImport_Import. As long as the call succeeds, it is sufficient that the modules are imported and present in `sys.modules` afterwards: https://github.com/python/cpython/blob/b6535ea7ecf40c51a9a13089d961c29abeaffa5e/Python/import.c#L1825-L1834 In order to avoid randomizing the order of module imports, which can cause unpredictable behavior depending on the timing of those "emulated imports", it's simpler to provide a "no-op" import function, and ensure that all the allowed modules are imported in advance, when the server starts. This is what this commit does. For good measure, it will raise an error if the module being imported is not already imported, which means it was not one of the expected/allowed modules. In that case, it's sufficient to pre-import the module. [^1]: https://github.com/python/cpython/blob/b6535ea7ecf40c51a9a13089d961c29abeaffa5e/Python/import.c#L1756-L1766 opw-3721469
- Loading branch information