diff options
author | Brett Cannon <brett@python.org> | 2013-06-16 17:13:40 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2013-06-16 17:13:40 (GMT) |
commit | e4f41deccf94ccc798b1eb1f44657ade66669a60 (patch) | |
tree | 8f47ca73224c628f4afcebb9dd989cac73d0f8d1 /Lib/pkgutil.py | |
parent | 39295e7a55d03b9ef31c0d0dd27d129b1ad5a695 (diff) | |
download | cpython-e4f41deccf94ccc798b1eb1f44657ade66669a60.zip cpython-e4f41deccf94ccc798b1eb1f44657ade66669a60.tar.gz cpython-e4f41deccf94ccc798b1eb1f44657ade66669a60.tar.bz2 |
Issue #17177: The imp module is pending deprecation.
To make sure there is no issue with code that is both Python 2 and 3
compatible, there are no plans to remove the module any sooner than
Python 4 (unless the community moves to Python 3 solidly before then).
Diffstat (limited to 'Lib/pkgutil.py')
-rw-r--r-- | Lib/pkgutil.py | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py index d2a6f1e..4678bb8 100644 --- a/Lib/pkgutil.py +++ b/Lib/pkgutil.py @@ -1,13 +1,13 @@ """Utilities to support packages.""" from functools import singledispatch as simplegeneric -import imp import importlib +import importlib.util import os import os.path import sys from types import ModuleType -from warnings import warn +import warnings __all__ = [ 'get_importer', 'iter_importers', 'get_loader', 'find_loader', @@ -21,7 +21,7 @@ def read_code(stream): import marshal magic = stream.read(4) - if magic != imp.get_magic(): + if magic != importlib.util.MAGIC_NUMBER: return None stream.read(8) # Skip timestamp and size @@ -160,6 +160,13 @@ def _iter_file_finder_modules(importer, prefix=''): iter_importer_modules.register( importlib.machinery.FileFinder, _iter_file_finder_modules) + +def _import_imp(): + global imp + with warnings.catch_warnings(): + warnings.simplefilter('ignore', PendingDeprecationWarning) + imp = importlib.import_module('imp') + class ImpImporter: """PEP 302 Importer that wraps Python's "classic" import algorithm @@ -172,8 +179,10 @@ class ImpImporter: """ def __init__(self, path=None): - warn("This emulation is deprecated, use 'importlib' instead", + global imp + warnings.warn("This emulation is deprecated, use 'importlib' instead", DeprecationWarning) + _import_imp() self.path = path def find_module(self, fullname, path=None): @@ -238,8 +247,9 @@ class ImpLoader: code = source = None def __init__(self, fullname, file, filename, etc): - warn("This emulation is deprecated, use 'importlib' instead", - DeprecationWarning) + warnings.warn("This emulation is deprecated, use 'importlib' instead", + DeprecationWarning) + _import_imp() self.file = file self.filename = filename self.fullname = fullname |