Message364930
> Code extremely simplified: (...)
Relationship between origin unmodified code and the extremely simplified code:
* Lib/test/test_importlib/__init__.py: load_tests() is called at each "test -R 3:3" iteration
* Lib/test/test_importlib/test_windows.py: at each iteartion:
* machinery = test.test_importlib.util.import_importlib('importlib.machinery') is called: it reloads the whole importlib package, with _frozen_importlib and _frozen_importlib_external blocked (force to reload pure Python importlib._bootstrap and importlib._bootstrap_external modules)
* support.import_module('winreg', required_on=['win']) is called with the fresh new importlib package: in short, it tries to import winreg which fails with ModuleNotFoundError
* Lib/importlib/__init__.py: each time the module is reloaded
* importlib._bootstrap._setup() is called: it copies '_thread', '_warnings' and '_weakref' from sys.modules into importlib._bootstrap namespace (module globals) using setattr(self_module, ...)
* importlib._bootstrap_external._setup() is called: it calls _bootstrap._builtin_from_name('_weakref')
When importlib._bootstrap_external._setup() calls _bootstrap._builtin_from_name('_weakref'), _init_module_attrs() copies the "spec" into module.__spec__. But the spec contains a reference to importlib._bootstrap namespace.
Before _weakref is converted to multiphase initialization:
$ ./python
>>> import _weakref
>>> id(_weakref)
140119879580176
>>> id(_weakref.__spec__.__init__.__globals__['_weakref'])
140119879580176
=> same module
After the change:
$ ./python
>>> import _weakref
>>> id(_weakref)
140312826159952
>>> id(_weakref.__spec__.__init__.__globals__['_weakref'])
140312826366288
=> two different objects
The problem is that module.__spec__ pulls the whole importlib package which contains tons of objects. |
|
Date |
User |
Action |
Args |
2020-03-24 13:52:23 | vstinner | set | recipients:
+ vstinner, shihai1991 |
2020-03-24 13:52:23 | vstinner | set | messageid: <1585057943.26.0.625012471894.issue40050@roundup.psfhosted.org> |
2020-03-24 13:52:23 | vstinner | link | issue40050 messages |
2020-03-24 13:52:23 | vstinner | create | |
|