Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python3....
File: pkgutil.py
"""Utilities to support packages."""
[0] Fix | Delete
[1] Fix | Delete
from collections import namedtuple
[2] Fix | Delete
from functools import singledispatch as simplegeneric
[3] Fix | Delete
import importlib
[4] Fix | Delete
import importlib.util
[5] Fix | Delete
import importlib.machinery
[6] Fix | Delete
import os
[7] Fix | Delete
import os.path
[8] Fix | Delete
import sys
[9] Fix | Delete
from types import ModuleType
[10] Fix | Delete
import warnings
[11] Fix | Delete
[12] Fix | Delete
__all__ = [
[13] Fix | Delete
'get_importer', 'iter_importers', 'get_loader', 'find_loader',
[14] Fix | Delete
'walk_packages', 'iter_modules', 'get_data',
[15] Fix | Delete
'ImpImporter', 'ImpLoader', 'read_code', 'extend_path',
[16] Fix | Delete
'ModuleInfo',
[17] Fix | Delete
]
[18] Fix | Delete
[19] Fix | Delete
[20] Fix | Delete
ModuleInfo = namedtuple('ModuleInfo', 'module_finder name ispkg')
[21] Fix | Delete
ModuleInfo.__doc__ = 'A namedtuple with minimal info about a module.'
[22] Fix | Delete
[23] Fix | Delete
[24] Fix | Delete
def _get_spec(finder, name):
[25] Fix | Delete
"""Return the finder-specific module spec."""
[26] Fix | Delete
# Works with legacy finders.
[27] Fix | Delete
try:
[28] Fix | Delete
find_spec = finder.find_spec
[29] Fix | Delete
except AttributeError:
[30] Fix | Delete
loader = finder.find_module(name)
[31] Fix | Delete
if loader is None:
[32] Fix | Delete
return None
[33] Fix | Delete
return importlib.util.spec_from_loader(name, loader)
[34] Fix | Delete
else:
[35] Fix | Delete
return find_spec(name)
[36] Fix | Delete
[37] Fix | Delete
[38] Fix | Delete
def read_code(stream):
[39] Fix | Delete
# This helper is needed in order for the PEP 302 emulation to
[40] Fix | Delete
# correctly handle compiled files
[41] Fix | Delete
import marshal
[42] Fix | Delete
[43] Fix | Delete
magic = stream.read(4)
[44] Fix | Delete
if magic != importlib.util.MAGIC_NUMBER:
[45] Fix | Delete
return None
[46] Fix | Delete
[47] Fix | Delete
stream.read(12) # Skip rest of the header
[48] Fix | Delete
return marshal.load(stream)
[49] Fix | Delete
[50] Fix | Delete
[51] Fix | Delete
def walk_packages(path=None, prefix='', onerror=None):
[52] Fix | Delete
"""Yields ModuleInfo for all modules recursively
[53] Fix | Delete
on path, or, if path is None, all accessible modules.
[54] Fix | Delete
[55] Fix | Delete
'path' should be either None or a list of paths to look for
[56] Fix | Delete
modules in.
[57] Fix | Delete
[58] Fix | Delete
'prefix' is a string to output on the front of every module name
[59] Fix | Delete
on output.
[60] Fix | Delete
[61] Fix | Delete
Note that this function must import all *packages* (NOT all
[62] Fix | Delete
modules!) on the given path, in order to access the __path__
[63] Fix | Delete
attribute to find submodules.
[64] Fix | Delete
[65] Fix | Delete
'onerror' is a function which gets called with one argument (the
[66] Fix | Delete
name of the package which was being imported) if any exception
[67] Fix | Delete
occurs while trying to import a package. If no onerror function is
[68] Fix | Delete
supplied, ImportErrors are caught and ignored, while all other
[69] Fix | Delete
exceptions are propagated, terminating the search.
[70] Fix | Delete
[71] Fix | Delete
Examples:
[72] Fix | Delete
[73] Fix | Delete
# list all modules python can access
[74] Fix | Delete
walk_packages()
[75] Fix | Delete
[76] Fix | Delete
# list all submodules of ctypes
[77] Fix | Delete
walk_packages(ctypes.__path__, ctypes.__name__+'.')
[78] Fix | Delete
"""
[79] Fix | Delete
[80] Fix | Delete
def seen(p, m={}):
[81] Fix | Delete
if p in m:
[82] Fix | Delete
return True
[83] Fix | Delete
m[p] = True
[84] Fix | Delete
[85] Fix | Delete
for info in iter_modules(path, prefix):
[86] Fix | Delete
yield info
[87] Fix | Delete
[88] Fix | Delete
if info.ispkg:
[89] Fix | Delete
try:
[90] Fix | Delete
__import__(info.name)
[91] Fix | Delete
except ImportError:
[92] Fix | Delete
if onerror is not None:
[93] Fix | Delete
onerror(info.name)
[94] Fix | Delete
except Exception:
[95] Fix | Delete
if onerror is not None:
[96] Fix | Delete
onerror(info.name)
[97] Fix | Delete
else:
[98] Fix | Delete
raise
[99] Fix | Delete
else:
[100] Fix | Delete
path = getattr(sys.modules[info.name], '__path__', None) or []
[101] Fix | Delete
[102] Fix | Delete
# don't traverse path items we've seen before
[103] Fix | Delete
path = [p for p in path if not seen(p)]
[104] Fix | Delete
[105] Fix | Delete
yield from walk_packages(path, info.name+'.', onerror)
[106] Fix | Delete
[107] Fix | Delete
[108] Fix | Delete
def iter_modules(path=None, prefix=''):
[109] Fix | Delete
"""Yields ModuleInfo for all submodules on path,
[110] Fix | Delete
or, if path is None, all top-level modules on sys.path.
[111] Fix | Delete
[112] Fix | Delete
'path' should be either None or a list of paths to look for
[113] Fix | Delete
modules in.
[114] Fix | Delete
[115] Fix | Delete
'prefix' is a string to output on the front of every module name
[116] Fix | Delete
on output.
[117] Fix | Delete
"""
[118] Fix | Delete
if path is None:
[119] Fix | Delete
importers = iter_importers()
[120] Fix | Delete
elif isinstance(path, str):
[121] Fix | Delete
raise ValueError("path must be None or list of paths to look for "
[122] Fix | Delete
"modules in")
[123] Fix | Delete
else:
[124] Fix | Delete
importers = map(get_importer, path)
[125] Fix | Delete
[126] Fix | Delete
yielded = {}
[127] Fix | Delete
for i in importers:
[128] Fix | Delete
for name, ispkg in iter_importer_modules(i, prefix):
[129] Fix | Delete
if name not in yielded:
[130] Fix | Delete
yielded[name] = 1
[131] Fix | Delete
yield ModuleInfo(i, name, ispkg)
[132] Fix | Delete
[133] Fix | Delete
[134] Fix | Delete
@simplegeneric
[135] Fix | Delete
def iter_importer_modules(importer, prefix=''):
[136] Fix | Delete
if not hasattr(importer, 'iter_modules'):
[137] Fix | Delete
return []
[138] Fix | Delete
return importer.iter_modules(prefix)
[139] Fix | Delete
[140] Fix | Delete
[141] Fix | Delete
# Implement a file walker for the normal importlib path hook
[142] Fix | Delete
def _iter_file_finder_modules(importer, prefix=''):
[143] Fix | Delete
if importer.path is None or not os.path.isdir(importer.path):
[144] Fix | Delete
return
[145] Fix | Delete
[146] Fix | Delete
yielded = {}
[147] Fix | Delete
import inspect
[148] Fix | Delete
try:
[149] Fix | Delete
filenames = os.listdir(importer.path)
[150] Fix | Delete
except OSError:
[151] Fix | Delete
# ignore unreadable directories like import does
[152] Fix | Delete
filenames = []
[153] Fix | Delete
filenames.sort() # handle packages before same-named modules
[154] Fix | Delete
[155] Fix | Delete
for fn in filenames:
[156] Fix | Delete
modname = inspect.getmodulename(fn)
[157] Fix | Delete
if modname=='__init__' or modname in yielded:
[158] Fix | Delete
continue
[159] Fix | Delete
[160] Fix | Delete
path = os.path.join(importer.path, fn)
[161] Fix | Delete
ispkg = False
[162] Fix | Delete
[163] Fix | Delete
if not modname and os.path.isdir(path) and '.' not in fn:
[164] Fix | Delete
modname = fn
[165] Fix | Delete
try:
[166] Fix | Delete
dircontents = os.listdir(path)
[167] Fix | Delete
except OSError:
[168] Fix | Delete
# ignore unreadable directories like import does
[169] Fix | Delete
dircontents = []
[170] Fix | Delete
for fn in dircontents:
[171] Fix | Delete
subname = inspect.getmodulename(fn)
[172] Fix | Delete
if subname=='__init__':
[173] Fix | Delete
ispkg = True
[174] Fix | Delete
break
[175] Fix | Delete
else:
[176] Fix | Delete
continue # not a package
[177] Fix | Delete
[178] Fix | Delete
if modname and '.' not in modname:
[179] Fix | Delete
yielded[modname] = 1
[180] Fix | Delete
yield prefix + modname, ispkg
[181] Fix | Delete
[182] Fix | Delete
iter_importer_modules.register(
[183] Fix | Delete
importlib.machinery.FileFinder, _iter_file_finder_modules)
[184] Fix | Delete
[185] Fix | Delete
[186] Fix | Delete
def _import_imp():
[187] Fix | Delete
global imp
[188] Fix | Delete
with warnings.catch_warnings():
[189] Fix | Delete
warnings.simplefilter('ignore', DeprecationWarning)
[190] Fix | Delete
imp = importlib.import_module('imp')
[191] Fix | Delete
[192] Fix | Delete
class ImpImporter:
[193] Fix | Delete
"""PEP 302 Finder that wraps Python's "classic" import algorithm
[194] Fix | Delete
[195] Fix | Delete
ImpImporter(dirname) produces a PEP 302 finder that searches that
[196] Fix | Delete
directory. ImpImporter(None) produces a PEP 302 finder that searches
[197] Fix | Delete
the current sys.path, plus any modules that are frozen or built-in.
[198] Fix | Delete
[199] Fix | Delete
Note that ImpImporter does not currently support being used by placement
[200] Fix | Delete
on sys.meta_path.
[201] Fix | Delete
"""
[202] Fix | Delete
[203] Fix | Delete
def __init__(self, path=None):
[204] Fix | Delete
global imp
[205] Fix | Delete
warnings.warn("This emulation is deprecated, use 'importlib' instead",
[206] Fix | Delete
DeprecationWarning)
[207] Fix | Delete
_import_imp()
[208] Fix | Delete
self.path = path
[209] Fix | Delete
[210] Fix | Delete
def find_module(self, fullname, path=None):
[211] Fix | Delete
# Note: we ignore 'path' argument since it is only used via meta_path
[212] Fix | Delete
subname = fullname.split(".")[-1]
[213] Fix | Delete
if subname != fullname and self.path is None:
[214] Fix | Delete
return None
[215] Fix | Delete
if self.path is None:
[216] Fix | Delete
path = None
[217] Fix | Delete
else:
[218] Fix | Delete
path = [os.path.realpath(self.path)]
[219] Fix | Delete
try:
[220] Fix | Delete
file, filename, etc = imp.find_module(subname, path)
[221] Fix | Delete
except ImportError:
[222] Fix | Delete
return None
[223] Fix | Delete
return ImpLoader(fullname, file, filename, etc)
[224] Fix | Delete
[225] Fix | Delete
def iter_modules(self, prefix=''):
[226] Fix | Delete
if self.path is None or not os.path.isdir(self.path):
[227] Fix | Delete
return
[228] Fix | Delete
[229] Fix | Delete
yielded = {}
[230] Fix | Delete
import inspect
[231] Fix | Delete
try:
[232] Fix | Delete
filenames = os.listdir(self.path)
[233] Fix | Delete
except OSError:
[234] Fix | Delete
# ignore unreadable directories like import does
[235] Fix | Delete
filenames = []
[236] Fix | Delete
filenames.sort() # handle packages before same-named modules
[237] Fix | Delete
[238] Fix | Delete
for fn in filenames:
[239] Fix | Delete
modname = inspect.getmodulename(fn)
[240] Fix | Delete
if modname=='__init__' or modname in yielded:
[241] Fix | Delete
continue
[242] Fix | Delete
[243] Fix | Delete
path = os.path.join(self.path, fn)
[244] Fix | Delete
ispkg = False
[245] Fix | Delete
[246] Fix | Delete
if not modname and os.path.isdir(path) and '.' not in fn:
[247] Fix | Delete
modname = fn
[248] Fix | Delete
try:
[249] Fix | Delete
dircontents = os.listdir(path)
[250] Fix | Delete
except OSError:
[251] Fix | Delete
# ignore unreadable directories like import does
[252] Fix | Delete
dircontents = []
[253] Fix | Delete
for fn in dircontents:
[254] Fix | Delete
subname = inspect.getmodulename(fn)
[255] Fix | Delete
if subname=='__init__':
[256] Fix | Delete
ispkg = True
[257] Fix | Delete
break
[258] Fix | Delete
else:
[259] Fix | Delete
continue # not a package
[260] Fix | Delete
[261] Fix | Delete
if modname and '.' not in modname:
[262] Fix | Delete
yielded[modname] = 1
[263] Fix | Delete
yield prefix + modname, ispkg
[264] Fix | Delete
[265] Fix | Delete
[266] Fix | Delete
class ImpLoader:
[267] Fix | Delete
"""PEP 302 Loader that wraps Python's "classic" import algorithm
[268] Fix | Delete
"""
[269] Fix | Delete
code = source = None
[270] Fix | Delete
[271] Fix | Delete
def __init__(self, fullname, file, filename, etc):
[272] Fix | Delete
warnings.warn("This emulation is deprecated, use 'importlib' instead",
[273] Fix | Delete
DeprecationWarning)
[274] Fix | Delete
_import_imp()
[275] Fix | Delete
self.file = file
[276] Fix | Delete
self.filename = filename
[277] Fix | Delete
self.fullname = fullname
[278] Fix | Delete
self.etc = etc
[279] Fix | Delete
[280] Fix | Delete
def load_module(self, fullname):
[281] Fix | Delete
self._reopen()
[282] Fix | Delete
try:
[283] Fix | Delete
mod = imp.load_module(fullname, self.file, self.filename, self.etc)
[284] Fix | Delete
finally:
[285] Fix | Delete
if self.file:
[286] Fix | Delete
self.file.close()
[287] Fix | Delete
# Note: we don't set __loader__ because we want the module to look
[288] Fix | Delete
# normal; i.e. this is just a wrapper for standard import machinery
[289] Fix | Delete
return mod
[290] Fix | Delete
[291] Fix | Delete
def get_data(self, pathname):
[292] Fix | Delete
with open(pathname, "rb") as file:
[293] Fix | Delete
return file.read()
[294] Fix | Delete
[295] Fix | Delete
def _reopen(self):
[296] Fix | Delete
if self.file and self.file.closed:
[297] Fix | Delete
mod_type = self.etc[2]
[298] Fix | Delete
if mod_type==imp.PY_SOURCE:
[299] Fix | Delete
self.file = open(self.filename, 'r')
[300] Fix | Delete
elif mod_type in (imp.PY_COMPILED, imp.C_EXTENSION):
[301] Fix | Delete
self.file = open(self.filename, 'rb')
[302] Fix | Delete
[303] Fix | Delete
def _fix_name(self, fullname):
[304] Fix | Delete
if fullname is None:
[305] Fix | Delete
fullname = self.fullname
[306] Fix | Delete
elif fullname != self.fullname:
[307] Fix | Delete
raise ImportError("Loader for module %s cannot handle "
[308] Fix | Delete
"module %s" % (self.fullname, fullname))
[309] Fix | Delete
return fullname
[310] Fix | Delete
[311] Fix | Delete
def is_package(self, fullname):
[312] Fix | Delete
fullname = self._fix_name(fullname)
[313] Fix | Delete
return self.etc[2]==imp.PKG_DIRECTORY
[314] Fix | Delete
[315] Fix | Delete
def get_code(self, fullname=None):
[316] Fix | Delete
fullname = self._fix_name(fullname)
[317] Fix | Delete
if self.code is None:
[318] Fix | Delete
mod_type = self.etc[2]
[319] Fix | Delete
if mod_type==imp.PY_SOURCE:
[320] Fix | Delete
source = self.get_source(fullname)
[321] Fix | Delete
self.code = compile(source, self.filename, 'exec')
[322] Fix | Delete
elif mod_type==imp.PY_COMPILED:
[323] Fix | Delete
self._reopen()
[324] Fix | Delete
try:
[325] Fix | Delete
self.code = read_code(self.file)
[326] Fix | Delete
finally:
[327] Fix | Delete
self.file.close()
[328] Fix | Delete
elif mod_type==imp.PKG_DIRECTORY:
[329] Fix | Delete
self.code = self._get_delegate().get_code()
[330] Fix | Delete
return self.code
[331] Fix | Delete
[332] Fix | Delete
def get_source(self, fullname=None):
[333] Fix | Delete
fullname = self._fix_name(fullname)
[334] Fix | Delete
if self.source is None:
[335] Fix | Delete
mod_type = self.etc[2]
[336] Fix | Delete
if mod_type==imp.PY_SOURCE:
[337] Fix | Delete
self._reopen()
[338] Fix | Delete
try:
[339] Fix | Delete
self.source = self.file.read()
[340] Fix | Delete
finally:
[341] Fix | Delete
self.file.close()
[342] Fix | Delete
elif mod_type==imp.PY_COMPILED:
[343] Fix | Delete
if os.path.exists(self.filename[:-1]):
[344] Fix | Delete
with open(self.filename[:-1], 'r') as f:
[345] Fix | Delete
self.source = f.read()
[346] Fix | Delete
elif mod_type==imp.PKG_DIRECTORY:
[347] Fix | Delete
self.source = self._get_delegate().get_source()
[348] Fix | Delete
return self.source
[349] Fix | Delete
[350] Fix | Delete
def _get_delegate(self):
[351] Fix | Delete
finder = ImpImporter(self.filename)
[352] Fix | Delete
spec = _get_spec(finder, '__init__')
[353] Fix | Delete
return spec.loader
[354] Fix | Delete
[355] Fix | Delete
def get_filename(self, fullname=None):
[356] Fix | Delete
fullname = self._fix_name(fullname)
[357] Fix | Delete
mod_type = self.etc[2]
[358] Fix | Delete
if mod_type==imp.PKG_DIRECTORY:
[359] Fix | Delete
return self._get_delegate().get_filename()
[360] Fix | Delete
elif mod_type in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION):
[361] Fix | Delete
return self.filename
[362] Fix | Delete
return None
[363] Fix | Delete
[364] Fix | Delete
[365] Fix | Delete
try:
[366] Fix | Delete
import zipimport
[367] Fix | Delete
from zipimport import zipimporter
[368] Fix | Delete
[369] Fix | Delete
def iter_zipimport_modules(importer, prefix=''):
[370] Fix | Delete
dirlist = sorted(zipimport._zip_directory_cache[importer.archive])
[371] Fix | Delete
_prefix = importer.prefix
[372] Fix | Delete
plen = len(_prefix)
[373] Fix | Delete
yielded = {}
[374] Fix | Delete
import inspect
[375] Fix | Delete
for fn in dirlist:
[376] Fix | Delete
if not fn.startswith(_prefix):
[377] Fix | Delete
continue
[378] Fix | Delete
[379] Fix | Delete
fn = fn[plen:].split(os.sep)
[380] Fix | Delete
[381] Fix | Delete
if len(fn)==2 and fn[1].startswith('__init__.py'):
[382] Fix | Delete
if fn[0] not in yielded:
[383] Fix | Delete
yielded[fn[0]] = 1
[384] Fix | Delete
yield prefix + fn[0], True
[385] Fix | Delete
[386] Fix | Delete
if len(fn)!=1:
[387] Fix | Delete
continue
[388] Fix | Delete
[389] Fix | Delete
modname = inspect.getmodulename(fn[0])
[390] Fix | Delete
if modname=='__init__':
[391] Fix | Delete
continue
[392] Fix | Delete
[393] Fix | Delete
if modname and '.' not in modname and modname not in yielded:
[394] Fix | Delete
yielded[modname] = 1
[395] Fix | Delete
yield prefix + modname, False
[396] Fix | Delete
[397] Fix | Delete
iter_importer_modules.register(zipimporter, iter_zipimport_modules)
[398] Fix | Delete
[399] Fix | Delete
except ImportError:
[400] Fix | Delete
pass
[401] Fix | Delete
[402] Fix | Delete
[403] Fix | Delete
def get_importer(path_item):
[404] Fix | Delete
"""Retrieve a finder for the given path item
[405] Fix | Delete
[406] Fix | Delete
The returned finder is cached in sys.path_importer_cache
[407] Fix | Delete
if it was newly created by a path hook.
[408] Fix | Delete
[409] Fix | Delete
The cache (or part of it) can be cleared manually if a
[410] Fix | Delete
rescan of sys.path_hooks is necessary.
[411] Fix | Delete
"""
[412] Fix | Delete
path_item = os.fsdecode(path_item)
[413] Fix | Delete
try:
[414] Fix | Delete
importer = sys.path_importer_cache[path_item]
[415] Fix | Delete
except KeyError:
[416] Fix | Delete
for path_hook in sys.path_hooks:
[417] Fix | Delete
try:
[418] Fix | Delete
importer = path_hook(path_item)
[419] Fix | Delete
sys.path_importer_cache.setdefault(path_item, importer)
[420] Fix | Delete
break
[421] Fix | Delete
except ImportError:
[422] Fix | Delete
pass
[423] Fix | Delete
else:
[424] Fix | Delete
importer = None
[425] Fix | Delete
return importer
[426] Fix | Delete
[427] Fix | Delete
[428] Fix | Delete
def iter_importers(fullname=""):
[429] Fix | Delete
"""Yield finders for the given module name
[430] Fix | Delete
[431] Fix | Delete
If fullname contains a '.', the finders will be for the package
[432] Fix | Delete
containing fullname, otherwise they will be all registered top level
[433] Fix | Delete
finders (i.e. those on both sys.meta_path and sys.path_hooks).
[434] Fix | Delete
[435] Fix | Delete
If the named module is in a package, that package is imported as a side
[436] Fix | Delete
effect of invoking this function.
[437] Fix | Delete
[438] Fix | Delete
If no module name is specified, all top level finders are produced.
[439] Fix | Delete
"""
[440] Fix | Delete
if fullname.startswith('.'):
[441] Fix | Delete
msg = "Relative module name {!r} not supported".format(fullname)
[442] Fix | Delete
raise ImportError(msg)
[443] Fix | Delete
if '.' in fullname:
[444] Fix | Delete
# Get the containing package's __path__
[445] Fix | Delete
pkg_name = fullname.rpartition(".")[0]
[446] Fix | Delete
pkg = importlib.import_module(pkg_name)
[447] Fix | Delete
path = getattr(pkg, '__path__', None)
[448] Fix | Delete
if path is None:
[449] Fix | Delete
return
[450] Fix | Delete
else:
[451] Fix | Delete
yield from sys.meta_path
[452] Fix | Delete
path = sys.path
[453] Fix | Delete
for item in path:
[454] Fix | Delete
yield get_importer(item)
[455] Fix | Delete
[456] Fix | Delete
[457] Fix | Delete
def get_loader(module_or_name):
[458] Fix | Delete
"""Get a "loader" object for module_or_name
[459] Fix | Delete
[460] Fix | Delete
Returns None if the module cannot be found or imported.
[461] Fix | Delete
If the named module is not already imported, its containing package
[462] Fix | Delete
(if any) is imported, in order to establish the package __path__.
[463] Fix | Delete
"""
[464] Fix | Delete
if module_or_name in sys.modules:
[465] Fix | Delete
module_or_name = sys.modules[module_or_name]
[466] Fix | Delete
if module_or_name is None:
[467] Fix | Delete
return None
[468] Fix | Delete
if isinstance(module_or_name, ModuleType):
[469] Fix | Delete
module = module_or_name
[470] Fix | Delete
loader = getattr(module, '__loader__', None)
[471] Fix | Delete
if loader is not None:
[472] Fix | Delete
return loader
[473] Fix | Delete
if getattr(module, '__spec__', None) is None:
[474] Fix | Delete
return None
[475] Fix | Delete
fullname = module.__name__
[476] Fix | Delete
else:
[477] Fix | Delete
fullname = module_or_name
[478] Fix | Delete
return find_loader(fullname)
[479] Fix | Delete
[480] Fix | Delete
[481] Fix | Delete
def find_loader(fullname):
[482] Fix | Delete
"""Find a "loader" object for fullname
[483] Fix | Delete
[484] Fix | Delete
This is a backwards compatibility wrapper around
[485] Fix | Delete
importlib.util.find_spec that converts most failures to ImportError
[486] Fix | Delete
and only returns the loader rather than the full spec
[487] Fix | Delete
"""
[488] Fix | Delete
if fullname.startswith('.'):
[489] Fix | Delete
msg = "Relative module name {!r} not supported".format(fullname)
[490] Fix | Delete
raise ImportError(msg)
[491] Fix | Delete
try:
[492] Fix | Delete
spec = importlib.util.find_spec(fullname)
[493] Fix | Delete
except (ImportError, AttributeError, TypeError, ValueError) as ex:
[494] Fix | Delete
# This hack fixes an impedance mismatch between pkgutil and
[495] Fix | Delete
# importlib, where the latter raises other errors for cases where
[496] Fix | Delete
# pkgutil previously raised ImportError
[497] Fix | Delete
msg = "Error while finding loader for {!r} ({}: {})"
[498] Fix | Delete
raise ImportError(msg.format(fullname, type(ex), ex)) from ex
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function