[issue19944] Make importlib.find_spec load packages as needed

Eric Snow report at bugs.python.org
Fri Jan 3 07:31:15 CET 2014


Eric Snow added the comment:

Good points, Nick.  I was trying to limit touches on _bootstrap.py for 3.4, but that "simple" patch is mostly just a hacky band-aid.  Here's a patch that hopefully stands on its own and still limits touches.  (The patch is the bare-bones changes only.)

There are 2 things in particular with this patch that I'd like to be sure others are comfortable with:

* an import statement (for resolve_name) inside find_spec()
* the use of builtins.__import__() instead of importlib.import_module()

Regarding the "nested" import, I didn't want to import the submodule at the top level due to possible future circular imports (there aren't any now with importlib.util, but still...).  At the same time, to me using something out of a submodule in the parent module in this way is a bit of a bad code smell.  I'd be more inclined to move the resolve_name() implementation into _bootstrap.py to resolve it, but even better may be to move it to __init__.py as a private name and then import it into importlib.util.  As it stands, the patch simply uses the "nested" import.

As to using builtins.__import__() when importing the parent module, that seems like it would result in less surprising results in the (uncommon) case that someone is using a custom __import__() that would yield a different result than importlib.import_module().  In the case of find_spec() that makes more sense to me.

----------
Added file: http://bugs.python.org/file33299/issue19944-find-spec-mirror-import-module-direct.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19944>
_______________________________________


More information about the Python-bugs-list mailing list