summaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Lib/pkgutil.py
diff options
context:
space:
mode:
authorAlexSm <[email protected]>2024-02-16 11:51:30 +0100
committerGitHub <[email protected]>2024-02-16 11:51:30 +0100
commit506ecaee93b52cc12c2e2f97c3d42e3ca2a7f59e (patch)
treed096fb9eb988fbb0ca1ba970041773207ce3aa70 /contrib/tools/python3/src/Lib/pkgutil.py
parent4749b9e5d260714490997e6f5ee1ee8c1c8fc46c (diff)
parentf200f72c9d7a89c1018e3dc6b46c49fe2ecf84fb (diff)
Merge pull request #1940 from dcherednik/importlib
Library import 14
Diffstat (limited to 'contrib/tools/python3/src/Lib/pkgutil.py')
-rw-r--r--contrib/tools/python3/src/Lib/pkgutil.py205
1 files changed, 9 insertions, 196 deletions
diff --git a/contrib/tools/python3/src/Lib/pkgutil.py b/contrib/tools/python3/src/Lib/pkgutil.py
index 56731de64af..dccbec52aa7 100644
--- a/contrib/tools/python3/src/Lib/pkgutil.py
+++ b/contrib/tools/python3/src/Lib/pkgutil.py
@@ -14,7 +14,7 @@ import warnings
__all__ = [
'get_importer', 'iter_importers', 'get_loader', 'find_loader',
'walk_packages', 'iter_modules', 'get_data',
- 'ImpImporter', 'ImpLoader', 'read_code', 'extend_path',
+ 'read_code', 'extend_path',
'ModuleInfo',
]
@@ -23,20 +23,6 @@ ModuleInfo = namedtuple('ModuleInfo', 'module_finder name ispkg')
ModuleInfo.__doc__ = 'A namedtuple with minimal info about a module.'
-def _get_spec(finder, name):
- """Return the finder-specific module spec."""
- # Works with legacy finders.
- try:
- find_spec = finder.find_spec
- except AttributeError:
- loader = finder.find_module(name)
- if loader is None:
- return None
- return importlib.util.spec_from_loader(name, loader)
- else:
- return find_spec(name)
-
-
def read_code(stream):
# This helper is needed in order for the PEP 302 emulation to
# correctly handle compiled files
@@ -185,187 +171,6 @@ iter_importer_modules.register(
importlib.machinery.FileFinder, _iter_file_finder_modules)
-def _import_imp():
- global imp
- with warnings.catch_warnings():
- warnings.simplefilter('ignore', DeprecationWarning)
- imp = importlib.import_module('imp')
-
-class ImpImporter:
- """PEP 302 Finder that wraps Python's "classic" import algorithm
-
- 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
- on sys.meta_path.
- """
-
- def __init__(self, path=None):
- global imp
- warnings.warn("This emulation is deprecated and slated for removal "
- "in Python 3.12; use 'importlib' instead",
- DeprecationWarning)
- _import_imp()
- self.path = path
-
- def find_module(self, fullname, path=None):
- # Note: we ignore 'path' argument since it is only used via meta_path
- subname = fullname.split(".")[-1]
- if subname != fullname and self.path is None:
- return None
- if self.path is None:
- path = None
- else:
- path = [os.path.realpath(self.path)]
- try:
- file, filename, etc = imp.find_module(subname, path)
- except ImportError:
- return None
- return ImpLoader(fullname, file, filename, etc)
-
- def iter_modules(self, prefix=''):
- if self.path is None or not os.path.isdir(self.path):
- return
-
- yielded = {}
- import inspect
- try:
- filenames = os.listdir(self.path)
- except OSError:
- # ignore unreadable directories like import does
- filenames = []
- filenames.sort() # handle packages before same-named modules
-
- for fn in filenames:
- modname = inspect.getmodulename(fn)
- if modname=='__init__' or modname in yielded:
- continue
-
- path = os.path.join(self.path, fn)
- ispkg = False
-
- if not modname and os.path.isdir(path) and '.' not in fn:
- modname = fn
- try:
- dircontents = os.listdir(path)
- except OSError:
- # ignore unreadable directories like import does
- dircontents = []
- for fn in dircontents:
- subname = inspect.getmodulename(fn)
- if subname=='__init__':
- ispkg = True
- break
- else:
- continue # not a package
-
- if modname and '.' not in modname:
- yielded[modname] = 1
- yield prefix + modname, ispkg
-
-
-class ImpLoader:
- """PEP 302 Loader that wraps Python's "classic" import algorithm
- """
- code = source = None
-
- def __init__(self, fullname, file, filename, etc):
- warnings.warn("This emulation is deprecated and slated for removal in "
- "Python 3.12; use 'importlib' instead",
- DeprecationWarning)
- _import_imp()
- self.file = file
- self.filename = filename
- self.fullname = fullname
- self.etc = etc
-
- def load_module(self, fullname):
- self._reopen()
- try:
- mod = imp.load_module(fullname, self.file, self.filename, self.etc)
- finally:
- if self.file:
- self.file.close()
- # Note: we don't set __loader__ because we want the module to look
- # normal; i.e. this is just a wrapper for standard import machinery
- return mod
-
- def get_data(self, pathname):
- with open(pathname, "rb") as file:
- return file.read()
-
- def _reopen(self):
- if self.file and self.file.closed:
- mod_type = self.etc[2]
- if mod_type==imp.PY_SOURCE:
- self.file = open(self.filename, 'r')
- elif mod_type in (imp.PY_COMPILED, imp.C_EXTENSION):
- self.file = open(self.filename, 'rb')
-
- def _fix_name(self, fullname):
- if fullname is None:
- fullname = self.fullname
- elif fullname != self.fullname:
- raise ImportError("Loader for module %s cannot handle "
- "module %s" % (self.fullname, fullname))
- return fullname
-
- def is_package(self, fullname):
- fullname = self._fix_name(fullname)
- return self.etc[2]==imp.PKG_DIRECTORY
-
- def get_code(self, fullname=None):
- fullname = self._fix_name(fullname)
- if self.code is None:
- mod_type = self.etc[2]
- if mod_type==imp.PY_SOURCE:
- source = self.get_source(fullname)
- self.code = compile(source, self.filename, 'exec')
- elif mod_type==imp.PY_COMPILED:
- self._reopen()
- try:
- self.code = read_code(self.file)
- finally:
- self.file.close()
- elif mod_type==imp.PKG_DIRECTORY:
- self.code = self._get_delegate().get_code()
- return self.code
-
- def get_source(self, fullname=None):
- fullname = self._fix_name(fullname)
- if self.source is None:
- mod_type = self.etc[2]
- if mod_type==imp.PY_SOURCE:
- self._reopen()
- try:
- self.source = self.file.read()
- finally:
- self.file.close()
- elif mod_type==imp.PY_COMPILED:
- if os.path.exists(self.filename[:-1]):
- with open(self.filename[:-1], 'r') as f:
- self.source = f.read()
- elif mod_type==imp.PKG_DIRECTORY:
- self.source = self._get_delegate().get_source()
- return self.source
-
- def _get_delegate(self):
- finder = ImpImporter(self.filename)
- spec = _get_spec(finder, '__init__')
- return spec.loader
-
- def get_filename(self, fullname=None):
- fullname = self._fix_name(fullname)
- mod_type = self.etc[2]
- if mod_type==imp.PKG_DIRECTORY:
- return self._get_delegate().get_filename()
- elif mod_type in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION):
- return self.filename
- return None
-
-
try:
import zipimport
from zipimport import zipimporter
@@ -465,6 +270,10 @@ def get_loader(module_or_name):
If the named module is not already imported, its containing package
(if any) is imported, in order to establish the package __path__.
"""
+ warnings._deprecated("pkgutil.get_loader",
+ f"{warnings._DEPRECATED_MSG}; "
+ "use importlib.util.find_spec() instead",
+ remove=(3, 14))
if module_or_name in sys.modules:
module_or_name = sys.modules[module_or_name]
if module_or_name is None:
@@ -489,6 +298,10 @@ def find_loader(fullname):
importlib.util.find_spec that converts most failures to ImportError
and only returns the loader rather than the full spec
"""
+ warnings._deprecated("pkgutil.find_loader",
+ f"{warnings._DEPRECATED_MSG}; "
+ "use importlib.util.find_spec() instead",
+ remove=(3, 14))
if fullname.startswith('.'):
msg = "Relative module name {!r} not supported".format(fullname)
raise ImportError(msg)