[issue18812] PyImport_Import redundant calls to find module

Brett Cannon report at bugs.python.org
Fri Aug 23 16:56:07 CEST 2013


Brett Cannon added the comment:

If I were to change this code in any way it would be to stop passing in a dummy value for fromlist, not stop pulling from sys.modules. The official idiom for directly calling __import__() is::

  __import__(module_name)
  return sys.modules[module_name]

It just so happens PyImport_Import() is old enough to be using both the old idiom of a bogus fromlist item *and* been updated at some point to use the new idiom.

I would also strongly discourage you from replacing __import__. More and more code is using importlib and various importers to do things and so it might have unforeseen consequences. importlib.import_module() also relies on importlib.__import__, not builtins.__import__ so as more code switches to that function for dynamic importing it will lead to more imports not using an overridden __import__.

----------

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


More information about the Python-bugs-list mailing list