cpython: a8b30fd6ee4f (original) (raw)
Mercurial > cpython
changeset 89787:a8b30fd6ee4f 3.4
Close #20839: pkgutil.find_loader now uses importlib.util.find_spec [#20839]
Nick Coghlan ncoghlan@gmail.com | |
---|---|
date | Tue, 04 Mar 2014 20:39:42 +1000 |
parents | 2c5a5fa0692c |
children | c97d80985b71 |
files | Doc/library/pkgutil.rst Lib/pkgutil.py Lib/test/test_pkgutil.py Misc/NEWS |
diffstat | 4 files changed, 41 insertions(+), 21 deletions(-)[+] [-] Doc/library/pkgutil.rst 17 Lib/pkgutil.py 17 Lib/test/test_pkgutil.py 19 Misc/NEWS 9 |
line wrap: on
line diff
--- a/Doc/library/pkgutil.rst
+++ b/Doc/library/pkgutil.rst
@@ -74,15 +74,17 @@ support.
Retrieve a :pep:302
module loader for the given fullname.
- This is a convenience wrapper around :func:
importlib.find_loader
that - sets the path argument correctly when searching for submodules, and
- also ensures parent packages (if any) are imported before searching for
- submodules.
- This is a backwards compatibility wrapper around
- :func:
importlib.util.find_spec
that converts most failures to - :exc:
ImportError
and only returns the loader rather than the full - :class:
ModuleSpec
. .. versionchanged:: 3.3 Updated to be based directly on :mod:importlib
rather than relying on the package internal PEP 302 import emulation. - .. versionchanged:: 3.4
Updated to be based on :pep:`451`[](#l1.21)
.. function:: get_importer(path_item)
@@ -109,14 +111,13 @@ support.
not already imported, its containing package (if any) is imported, in order
to establish the package __path__
.
- This function uses :func:
iter_importers
, and is thus subject to the same - limitations regarding platform-specific special import locations such as the
- Windows registry.
-
.. versionchanged:: 3.3
Updated to be based directly on :mod:
importlib
rather than relying on the package internal PEP 302 import emulation.
+ .. function:: iter_importers(fullname='')
--- a/Lib/pkgutil.py +++ b/Lib/pkgutil.py @@ -470,29 +470,22 @@ def get_loader(module_or_name): def find_loader(fullname): """Find a PEP 302 "loader" object for fullname
- This is s convenience wrapper around :func:
importlib.find_loader
that - sets the path argument correctly when searching for submodules, and
- also ensures parent packages (if any) are imported before searching for
- submodules.
- This is a backwards compatibility wrapper around
- importlib.util.find_spec that converts most failures to ImportError
- and only returns the loader rather than the full spec """ if fullname.startswith('.'): msg = "Relative module name {!r} not supported".format(fullname) raise ImportError(msg)
- path = None
- pkg_name = fullname.rpartition(".")[0]
- if pkg_name:
pkg = importlib.import_module(pkg_name)[](#l2.21)
path = getattr(pkg, "__path__", None)[](#l2.22)
if path is None:[](#l2.23)
try:return None[](#l2.24)
return importlib.find_loader(fullname, path)[](#l2.26)
except (ImportError, AttributeError, TypeError, ValueError) as ex: # This hack fixes an impedance mismatch between pkgutil and # importlib, where the latter raises other errors for cases where # pkgutil previously raised ImportError msg = "Error while finding loader for {!r} ({}: {})" raise ImportError(msg.format(fullname, type(ex), ex)) from exspec = importlib.util.find_spec(fullname)[](#l2.27)
- return spec.loader
--- a/Lib/test/test_pkgutil.py +++ b/Lib/test/test_pkgutil.py @@ -334,6 +334,25 @@ class ImportlibMigrationTests(unittest.T self.assertIsNotNone(pkgutil.get_loader("test.support")) self.assertEqual(len(w.warnings), 0)
- def test_get_loader_handles_missing_loader_attribute(self):
global __loader__[](#l3.8)
this_loader = __loader__[](#l3.9)
del __loader__[](#l3.10)
try:[](#l3.11)
with check_warnings() as w:[](#l3.12)
self.assertIsNotNone(pkgutil.get_loader(__name__))[](#l3.13)
self.assertEqual(len(w.warnings), 0)[](#l3.14)
finally:[](#l3.15)
__loader__ = this_loader[](#l3.16)
- def test_find_loader_avoids_emulation(self):
with check_warnings() as w:[](#l3.20)
self.assertIsNotNone(pkgutil.find_loader("sys"))[](#l3.21)
self.assertIsNotNone(pkgutil.find_loader("os"))[](#l3.22)
self.assertIsNotNone(pkgutil.find_loader("test.support"))[](#l3.23)
self.assertEqual(len(w.warnings), 0)[](#l3.24)
+ def test_get_importer_avoids_emulation(self): # We use an illegal path so none of the path hooks should fire with check_warnings() as w:
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,13 @@ Core and Builtins
- Issue #20786: Fix signatures for dict.delitem and property.delete builtins. +Library +------- + +- Issue #20839: Don't trigger a DeprecationWarning in the still supported
- pkgutil.get_loader() API when loader isn't set on a module (nor
- when pkgutil.find_loader() is called directly). + Build ----- @@ -27,7 +34,7 @@ Build uninstalling pip (rather than failing) if the user has updated pip to a different version from the one bundled with ensurepip. -- Issue #20465: Update OS X and Windows installer builds to use +- Issue #20465: Update OS X and Windows installer builds to use SQLite 3.8.3.1.