STINNER Victor <vstin...@python.org> added the comment:

> 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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue40050>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to