Add notes on imports and importlib
This commit is contained in:
parent
5b8f7dd1be
commit
60c4ae5599
|
@ -1357,6 +1357,16 @@ class Importer(object):
|
|||
fp.close()
|
||||
|
||||
def find_module(self, fullname, path=None):
|
||||
"""
|
||||
Return a loader (ourself) or None, for the module with fullname.
|
||||
|
||||
Implements importlib.abc.MetaPathFinder.find_module().
|
||||
Deprecrated in Python 3.4+, replaced by find_spec().
|
||||
Raises ImportWarning in Python 3.10+.
|
||||
|
||||
fullname A (fully qualified?) module name, e.g. "os.path".
|
||||
path __path__ of parent packge. None for a top level module.
|
||||
"""
|
||||
if hasattr(_tls, 'running'):
|
||||
return None
|
||||
|
||||
|
@ -1478,6 +1488,12 @@ class Importer(object):
|
|||
callback()
|
||||
|
||||
def load_module(self, fullname):
|
||||
"""
|
||||
Return the loaded module specified by fullname.
|
||||
|
||||
Implements importlib.abc.Loader.load_module().
|
||||
Deprecated in Python 3.4+, replaced by create_module() & exec_module().
|
||||
"""
|
||||
fullname = to_text(fullname)
|
||||
_v and self._log.debug('requesting %s', fullname)
|
||||
self._refuse_imports(fullname)
|
||||
|
|
|
@ -122,6 +122,13 @@ def is_stdlib_name(modname):
|
|||
"""
|
||||
Return :data:`True` if `modname` appears to come from the standard library.
|
||||
"""
|
||||
# `imp.is_builtin()` isn't a documented as part of Python's stdlib API.
|
||||
#
|
||||
# """
|
||||
# Main is a little special - imp.is_builtin("__main__") will return False,
|
||||
# but BuiltinImporter is still the most appropriate initial setting for
|
||||
# its __loader__ attribute.
|
||||
# """ -- comment in CPython pylifecycle.c:add_main_module()
|
||||
if imp.is_builtin(modname) != 0:
|
||||
return True
|
||||
|
||||
|
@ -512,6 +519,8 @@ class PkgutilMethod(FinderMethod):
|
|||
Find `fullname` using :func:`pkgutil.find_loader`.
|
||||
"""
|
||||
try:
|
||||
# If fullname refers to a submodule that's not already imported
|
||||
# then the containing package is imported.
|
||||
# Pre-'import spec' this returned None, in Python3.6 it raises
|
||||
# ImportError.
|
||||
loader = pkgutil.find_loader(fullname)
|
||||
|
|
Loading…
Reference in New Issue