mirror of
https://github.com/python/cpython.git
synced 2024-11-24 02:15:30 +08:00
Issue #26896: Disambiguate uses of "importer" with "finder".
Thanks to Oren Milman for the patch.
This commit is contained in:
parent
15552c39e0
commit
fdcdd9ed80
@ -207,13 +207,13 @@ Importing Modules
|
||||
|
||||
.. c:function:: PyObject* PyImport_GetImporter(PyObject *path)
|
||||
|
||||
Return an importer object for a :data:`sys.path`/:attr:`pkg.__path__` item
|
||||
Return a finder object for a :data:`sys.path`/:attr:`pkg.__path__` item
|
||||
*path*, possibly by fetching it from the :data:`sys.path_importer_cache`
|
||||
dict. If it wasn't yet cached, traverse :data:`sys.path_hooks` until a hook
|
||||
is found that can handle the path item. Return ``None`` if no hook could;
|
||||
this tells our caller it should fall back to the built-in import mechanism.
|
||||
Cache the result in :data:`sys.path_importer_cache`. Return a new reference
|
||||
to the importer object.
|
||||
this tells our caller that the :term:`path based finder` could not find a
|
||||
finder for this path item. Cache the result in :data:`sys.path_importer_cache`.
|
||||
Return a new reference to the finder object.
|
||||
|
||||
|
||||
.. c:function:: void _PyImport_Init()
|
||||
|
@ -46,10 +46,10 @@ support.
|
||||
|
||||
.. class:: ImpImporter(dirname=None)
|
||||
|
||||
:pep:`302` Importer that wraps Python's "classic" import algorithm.
|
||||
:pep:`302` Finder that wraps Python's "classic" import algorithm.
|
||||
|
||||
If *dirname* is a string, a :pep:`302` importer is created that searches that
|
||||
directory. If *dirname* is ``None``, a :pep:`302` importer is created that
|
||||
If *dirname* is a string, a :pep:`302` finder is created that searches that
|
||||
directory. If *dirname* is ``None``, a :pep:`302` finder is created that
|
||||
searches the current :data:`sys.path`, plus any modules that are frozen or
|
||||
built-in.
|
||||
|
||||
@ -88,9 +88,9 @@ support.
|
||||
|
||||
.. function:: get_importer(path_item)
|
||||
|
||||
Retrieve a :pep:`302` importer for the given *path_item*.
|
||||
Retrieve a :pep:`302` finder for the given *path_item*.
|
||||
|
||||
The returned importer is cached in :data:`sys.path_importer_cache` if it was
|
||||
The returned finder is cached in :data:`sys.path_importer_cache` if it was
|
||||
newly created by a path hook.
|
||||
|
||||
The cache (or part of it) can be cleared manually if a rescan of
|
||||
@ -121,16 +121,16 @@ support.
|
||||
|
||||
.. function:: iter_importers(fullname='')
|
||||
|
||||
Yield :pep:`302` importers for the given module name.
|
||||
Yield :pep:`302` finders for the given module name.
|
||||
|
||||
If fullname contains a '.', the importers will be for the package
|
||||
If fullname contains a '.', the finders will be for the package
|
||||
containing fullname, otherwise they will be all registered top level
|
||||
importers (i.e. those on both sys.meta_path and sys.path_hooks).
|
||||
finders (i.e. those on both sys.meta_path and sys.path_hooks).
|
||||
|
||||
If the named module is in a package, that package is imported as a side
|
||||
effect of invoking this function.
|
||||
|
||||
If no module name is specified, all top level importers are produced.
|
||||
If no module name is specified, all top level finders are produced.
|
||||
|
||||
.. versionchanged:: 3.3
|
||||
Updated to be based directly on :mod:`importlib` rather than relying
|
||||
|
@ -45,7 +45,7 @@ def read_code(stream):
|
||||
|
||||
|
||||
def walk_packages(path=None, prefix='', onerror=None):
|
||||
"""Yields (module_loader, name, ispkg) for all modules recursively
|
||||
"""Yields (module_finder, name, ispkg) for all modules recursively
|
||||
on path, or, if path is None, all accessible modules.
|
||||
|
||||
'path' should be either None or a list of paths to look for
|
||||
@ -102,7 +102,7 @@ def walk_packages(path=None, prefix='', onerror=None):
|
||||
|
||||
|
||||
def iter_modules(path=None, prefix=''):
|
||||
"""Yields (module_loader, name, ispkg) for all submodules on path,
|
||||
"""Yields (module_finder, name, ispkg) for all submodules on path,
|
||||
or, if path is None, all top-level modules on sys.path.
|
||||
|
||||
'path' should be either None or a list of paths to look for
|
||||
@ -184,10 +184,10 @@ def _import_imp():
|
||||
imp = importlib.import_module('imp')
|
||||
|
||||
class ImpImporter:
|
||||
"""PEP 302 Importer that wraps Python's "classic" import algorithm
|
||||
"""PEP 302 Finder that wraps Python's "classic" import algorithm
|
||||
|
||||
ImpImporter(dirname) produces a PEP 302 importer that searches that
|
||||
directory. ImpImporter(None) produces a PEP 302 importer that searches
|
||||
ImpImporter(dirname) produces a PEP 302 finder that searches that
|
||||
directory. ImpImporter(None) produces a PEP 302 finder that searches
|
||||
the current sys.path, plus any modules that are frozen or built-in.
|
||||
|
||||
Note that ImpImporter does not currently support being used by placement
|
||||
@ -395,9 +395,9 @@ except ImportError:
|
||||
|
||||
|
||||
def get_importer(path_item):
|
||||
"""Retrieve a PEP 302 importer for the given path item
|
||||
"""Retrieve a PEP 302 finder for the given path item
|
||||
|
||||
The returned importer is cached in sys.path_importer_cache
|
||||
The returned finder is cached in sys.path_importer_cache
|
||||
if it was newly created by a path hook.
|
||||
|
||||
The cache (or part of it) can be cleared manually if a
|
||||
@ -419,16 +419,16 @@ def get_importer(path_item):
|
||||
|
||||
|
||||
def iter_importers(fullname=""):
|
||||
"""Yield PEP 302 importers for the given module name
|
||||
"""Yield PEP 302 finders for the given module name
|
||||
|
||||
If fullname contains a '.', the importers will be for the package
|
||||
If fullname contains a '.', the finders will be for the package
|
||||
containing fullname, otherwise they will be all registered top level
|
||||
importers (i.e. those on both sys.meta_path and sys.path_hooks).
|
||||
finders (i.e. those on both sys.meta_path and sys.path_hooks).
|
||||
|
||||
If the named module is in a package, that package is imported as a side
|
||||
effect of invoking this function.
|
||||
|
||||
If no module name is specified, all top level importers are produced.
|
||||
If no module name is specified, all top level finders are produced.
|
||||
"""
|
||||
if fullname.startswith('.'):
|
||||
msg = "Relative module name {!r} not supported".format(fullname)
|
||||
|
@ -98,7 +98,7 @@ def _run_module_code(code, init_globals=None,
|
||||
# may be cleared when the temporary module goes away
|
||||
return mod_globals.copy()
|
||||
|
||||
# Helper to get the loader, code and filename for a module
|
||||
# Helper to get the full name, spec and code for a module
|
||||
def _get_module_details(mod_name, error=ImportError):
|
||||
if mod_name.startswith("."):
|
||||
raise error("Relative module names not supported")
|
||||
@ -253,7 +253,7 @@ def run_path(path_name, init_globals=None, run_name=None):
|
||||
return _run_module_code(code, init_globals, run_name,
|
||||
pkg_name=pkg_name, script_name=fname)
|
||||
else:
|
||||
# Importer is defined for path, so add it to
|
||||
# Finder is defined for path, so add it to
|
||||
# the start of sys.path
|
||||
sys.path.insert(0, path_name)
|
||||
try:
|
||||
|
@ -76,7 +76,6 @@ class CallSignature:
|
||||
self.__import__(mod_name)
|
||||
assert len(log) == 1
|
||||
args = log[0][0]
|
||||
kwargs = log[0][1]
|
||||
# Assuming all arguments are positional.
|
||||
self.assertEqual(args[0], mod_name)
|
||||
self.assertIsNone(args[1])
|
||||
|
@ -266,7 +266,6 @@ class mock_spec(_ImporterMock):
|
||||
module = self.modules[fullname]
|
||||
except KeyError:
|
||||
return None
|
||||
is_package = hasattr(module, '__path__')
|
||||
spec = util.spec_from_file_location(
|
||||
fullname, module.__file__, loader=self,
|
||||
submodule_search_locations=getattr(module, '__path__', None))
|
||||
|
@ -205,7 +205,7 @@ class PkgutilPEP302Tests(unittest.TestCase):
|
||||
del sys.meta_path[0]
|
||||
|
||||
def test_getdata_pep302(self):
|
||||
# Use a dummy importer/loader
|
||||
# Use a dummy finder/loader
|
||||
self.assertEqual(pkgutil.get_data('foo', 'dummy'), "Hello, world!")
|
||||
del sys.modules['foo']
|
||||
|
||||
|
@ -998,6 +998,7 @@ Damien Miller
|
||||
Jason V. Miller
|
||||
Jay T. Miller
|
||||
Katie Miller
|
||||
Oren Milman
|
||||
Roman Milner
|
||||
Julien Miotte
|
||||
Andrii V. Mishkovskyi
|
||||
|
@ -950,12 +950,13 @@ is_builtin(PyObject *name)
|
||||
}
|
||||
|
||||
|
||||
/* Return an importer object for a sys.path/pkg.__path__ item 'p',
|
||||
/* Return a finder object for a sys.path/pkg.__path__ item 'p',
|
||||
possibly by fetching it from the path_importer_cache dict. If it
|
||||
wasn't yet cached, traverse path_hooks until a hook is found
|
||||
that can handle the path item. Return None if no hook could;
|
||||
this tells our caller it should fall back to the builtin
|
||||
import mechanism. Cache the result in path_importer_cache.
|
||||
this tells our caller that the path based finder could not find
|
||||
a finder for this path item. Cache the result in
|
||||
path_importer_cache.
|
||||
Returns a borrowed reference. */
|
||||
|
||||
static PyObject *
|
||||
|
Loading…
Reference in New Issue
Block a user