[issue39430] tarfile.open(mode="r") race condition when importing lzma

Serhiy Storchaka report at bugs.python.org
Fri Jan 24 09:08:24 EST 2020


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

It is intended to support circular imports. Let foo.py contains "import bar" and bar.py contains "import foo". When you execute "import foo", the import machinery first creates an empty module foo, adds it to sys.modules, reads foo.py and executes it in the namespace of module foo. When the interpreter encounters "import bar" in foo.py, the import machinery creates an empty module bar, adds it to sys.modules, reads bar.py and executes it in the namespace of module bar. When the interpreter encounters "import foo" in bar.py, the import machinery takes the module foo from sys.modules. So you break an infinite cycle and can import modules with cyclic dependencies.

You can argue that cyclic import does not look as a good practice, but actually it is pretty common case when you import a submodule in a package. If foo/__init__.py contains "from .bar import Bar", the foo module must be imported before you import foo.bar, but is not completely initialized at that time yet.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39430>
_______________________________________


More information about the Python-bugs-list mailing list