Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python3....
File: modulefinder.py
"""Find modules used by a script, using introspection."""
[0] Fix | Delete
[1] Fix | Delete
import dis
[2] Fix | Delete
import importlib._bootstrap_external
[3] Fix | Delete
import importlib.machinery
[4] Fix | Delete
import marshal
[5] Fix | Delete
import os
[6] Fix | Delete
import io
[7] Fix | Delete
import sys
[8] Fix | Delete
[9] Fix | Delete
[10] Fix | Delete
LOAD_CONST = dis.opmap['LOAD_CONST']
[11] Fix | Delete
IMPORT_NAME = dis.opmap['IMPORT_NAME']
[12] Fix | Delete
STORE_NAME = dis.opmap['STORE_NAME']
[13] Fix | Delete
STORE_GLOBAL = dis.opmap['STORE_GLOBAL']
[14] Fix | Delete
STORE_OPS = STORE_NAME, STORE_GLOBAL
[15] Fix | Delete
EXTENDED_ARG = dis.EXTENDED_ARG
[16] Fix | Delete
[17] Fix | Delete
# Old imp constants:
[18] Fix | Delete
[19] Fix | Delete
_SEARCH_ERROR = 0
[20] Fix | Delete
_PY_SOURCE = 1
[21] Fix | Delete
_PY_COMPILED = 2
[22] Fix | Delete
_C_EXTENSION = 3
[23] Fix | Delete
_PKG_DIRECTORY = 5
[24] Fix | Delete
_C_BUILTIN = 6
[25] Fix | Delete
_PY_FROZEN = 7
[26] Fix | Delete
[27] Fix | Delete
# Modulefinder does a good job at simulating Python's, but it can not
[28] Fix | Delete
# handle __path__ modifications packages make at runtime. Therefore there
[29] Fix | Delete
# is a mechanism whereby you can register extra paths in this map for a
[30] Fix | Delete
# package, and it will be honored.
[31] Fix | Delete
[32] Fix | Delete
# Note this is a mapping is lists of paths.
[33] Fix | Delete
packagePathMap = {}
[34] Fix | Delete
[35] Fix | Delete
# A Public interface
[36] Fix | Delete
def AddPackagePath(packagename, path):
[37] Fix | Delete
packagePathMap.setdefault(packagename, []).append(path)
[38] Fix | Delete
[39] Fix | Delete
replacePackageMap = {}
[40] Fix | Delete
[41] Fix | Delete
# This ReplacePackage mechanism allows modulefinder to work around
[42] Fix | Delete
# situations in which a package injects itself under the name
[43] Fix | Delete
# of another package into sys.modules at runtime by calling
[44] Fix | Delete
# ReplacePackage("real_package_name", "faked_package_name")
[45] Fix | Delete
# before running ModuleFinder.
[46] Fix | Delete
[47] Fix | Delete
def ReplacePackage(oldname, newname):
[48] Fix | Delete
replacePackageMap[oldname] = newname
[49] Fix | Delete
[50] Fix | Delete
[51] Fix | Delete
def _find_module(name, path=None):
[52] Fix | Delete
"""An importlib reimplementation of imp.find_module (for our purposes)."""
[53] Fix | Delete
[54] Fix | Delete
# It's necessary to clear the caches for our Finder first, in case any
[55] Fix | Delete
# modules are being added/deleted/modified at runtime. In particular,
[56] Fix | Delete
# test_modulefinder.py changes file tree contents in a cache-breaking way:
[57] Fix | Delete
[58] Fix | Delete
importlib.machinery.PathFinder.invalidate_caches()
[59] Fix | Delete
[60] Fix | Delete
spec = importlib.machinery.PathFinder.find_spec(name, path)
[61] Fix | Delete
[62] Fix | Delete
if spec is None:
[63] Fix | Delete
raise ImportError("No module named {name!r}".format(name=name), name=name)
[64] Fix | Delete
[65] Fix | Delete
# Some special cases:
[66] Fix | Delete
[67] Fix | Delete
if spec.loader is importlib.machinery.BuiltinImporter:
[68] Fix | Delete
return None, None, ("", "", _C_BUILTIN)
[69] Fix | Delete
[70] Fix | Delete
if spec.loader is importlib.machinery.FrozenImporter:
[71] Fix | Delete
return None, None, ("", "", _PY_FROZEN)
[72] Fix | Delete
[73] Fix | Delete
file_path = spec.origin
[74] Fix | Delete
[75] Fix | Delete
if spec.loader.is_package(name):
[76] Fix | Delete
return None, os.path.dirname(file_path), ("", "", _PKG_DIRECTORY)
[77] Fix | Delete
[78] Fix | Delete
if isinstance(spec.loader, importlib.machinery.SourceFileLoader):
[79] Fix | Delete
kind = _PY_SOURCE
[80] Fix | Delete
[81] Fix | Delete
elif isinstance(spec.loader, importlib.machinery.ExtensionFileLoader):
[82] Fix | Delete
kind = _C_EXTENSION
[83] Fix | Delete
[84] Fix | Delete
elif isinstance(spec.loader, importlib.machinery.SourcelessFileLoader):
[85] Fix | Delete
kind = _PY_COMPILED
[86] Fix | Delete
[87] Fix | Delete
else: # Should never happen.
[88] Fix | Delete
return None, None, ("", "", _SEARCH_ERROR)
[89] Fix | Delete
[90] Fix | Delete
file = io.open_code(file_path)
[91] Fix | Delete
suffix = os.path.splitext(file_path)[-1]
[92] Fix | Delete
[93] Fix | Delete
return file, file_path, (suffix, "rb", kind)
[94] Fix | Delete
[95] Fix | Delete
[96] Fix | Delete
class Module:
[97] Fix | Delete
[98] Fix | Delete
def __init__(self, name, file=None, path=None):
[99] Fix | Delete
self.__name__ = name
[100] Fix | Delete
self.__file__ = file
[101] Fix | Delete
self.__path__ = path
[102] Fix | Delete
self.__code__ = None
[103] Fix | Delete
# The set of global names that are assigned to in the module.
[104] Fix | Delete
# This includes those names imported through starimports of
[105] Fix | Delete
# Python modules.
[106] Fix | Delete
self.globalnames = {}
[107] Fix | Delete
# The set of starimports this module did that could not be
[108] Fix | Delete
# resolved, ie. a starimport from a non-Python module.
[109] Fix | Delete
self.starimports = {}
[110] Fix | Delete
[111] Fix | Delete
def __repr__(self):
[112] Fix | Delete
s = "Module(%r" % (self.__name__,)
[113] Fix | Delete
if self.__file__ is not None:
[114] Fix | Delete
s = s + ", %r" % (self.__file__,)
[115] Fix | Delete
if self.__path__ is not None:
[116] Fix | Delete
s = s + ", %r" % (self.__path__,)
[117] Fix | Delete
s = s + ")"
[118] Fix | Delete
return s
[119] Fix | Delete
[120] Fix | Delete
class ModuleFinder:
[121] Fix | Delete
[122] Fix | Delete
def __init__(self, path=None, debug=0, excludes=None, replace_paths=None):
[123] Fix | Delete
if path is None:
[124] Fix | Delete
path = sys.path
[125] Fix | Delete
self.path = path
[126] Fix | Delete
self.modules = {}
[127] Fix | Delete
self.badmodules = {}
[128] Fix | Delete
self.debug = debug
[129] Fix | Delete
self.indent = 0
[130] Fix | Delete
self.excludes = excludes if excludes is not None else []
[131] Fix | Delete
self.replace_paths = replace_paths if replace_paths is not None else []
[132] Fix | Delete
self.processed_paths = [] # Used in debugging only
[133] Fix | Delete
[134] Fix | Delete
def msg(self, level, str, *args):
[135] Fix | Delete
if level <= self.debug:
[136] Fix | Delete
for i in range(self.indent):
[137] Fix | Delete
print(" ", end=' ')
[138] Fix | Delete
print(str, end=' ')
[139] Fix | Delete
for arg in args:
[140] Fix | Delete
print(repr(arg), end=' ')
[141] Fix | Delete
print()
[142] Fix | Delete
[143] Fix | Delete
def msgin(self, *args):
[144] Fix | Delete
level = args[0]
[145] Fix | Delete
if level <= self.debug:
[146] Fix | Delete
self.indent = self.indent + 1
[147] Fix | Delete
self.msg(*args)
[148] Fix | Delete
[149] Fix | Delete
def msgout(self, *args):
[150] Fix | Delete
level = args[0]
[151] Fix | Delete
if level <= self.debug:
[152] Fix | Delete
self.indent = self.indent - 1
[153] Fix | Delete
self.msg(*args)
[154] Fix | Delete
[155] Fix | Delete
def run_script(self, pathname):
[156] Fix | Delete
self.msg(2, "run_script", pathname)
[157] Fix | Delete
with io.open_code(pathname) as fp:
[158] Fix | Delete
stuff = ("", "rb", _PY_SOURCE)
[159] Fix | Delete
self.load_module('__main__', fp, pathname, stuff)
[160] Fix | Delete
[161] Fix | Delete
def load_file(self, pathname):
[162] Fix | Delete
dir, name = os.path.split(pathname)
[163] Fix | Delete
name, ext = os.path.splitext(name)
[164] Fix | Delete
with io.open_code(pathname) as fp:
[165] Fix | Delete
stuff = (ext, "rb", _PY_SOURCE)
[166] Fix | Delete
self.load_module(name, fp, pathname, stuff)
[167] Fix | Delete
[168] Fix | Delete
def import_hook(self, name, caller=None, fromlist=None, level=-1):
[169] Fix | Delete
self.msg(3, "import_hook", name, caller, fromlist, level)
[170] Fix | Delete
parent = self.determine_parent(caller, level=level)
[171] Fix | Delete
q, tail = self.find_head_package(parent, name)
[172] Fix | Delete
m = self.load_tail(q, tail)
[173] Fix | Delete
if not fromlist:
[174] Fix | Delete
return q
[175] Fix | Delete
if m.__path__:
[176] Fix | Delete
self.ensure_fromlist(m, fromlist)
[177] Fix | Delete
return None
[178] Fix | Delete
[179] Fix | Delete
def determine_parent(self, caller, level=-1):
[180] Fix | Delete
self.msgin(4, "determine_parent", caller, level)
[181] Fix | Delete
if not caller or level == 0:
[182] Fix | Delete
self.msgout(4, "determine_parent -> None")
[183] Fix | Delete
return None
[184] Fix | Delete
pname = caller.__name__
[185] Fix | Delete
if level >= 1: # relative import
[186] Fix | Delete
if caller.__path__:
[187] Fix | Delete
level -= 1
[188] Fix | Delete
if level == 0:
[189] Fix | Delete
parent = self.modules[pname]
[190] Fix | Delete
assert parent is caller
[191] Fix | Delete
self.msgout(4, "determine_parent ->", parent)
[192] Fix | Delete
return parent
[193] Fix | Delete
if pname.count(".") < level:
[194] Fix | Delete
raise ImportError("relative importpath too deep")
[195] Fix | Delete
pname = ".".join(pname.split(".")[:-level])
[196] Fix | Delete
parent = self.modules[pname]
[197] Fix | Delete
self.msgout(4, "determine_parent ->", parent)
[198] Fix | Delete
return parent
[199] Fix | Delete
if caller.__path__:
[200] Fix | Delete
parent = self.modules[pname]
[201] Fix | Delete
assert caller is parent
[202] Fix | Delete
self.msgout(4, "determine_parent ->", parent)
[203] Fix | Delete
return parent
[204] Fix | Delete
if '.' in pname:
[205] Fix | Delete
i = pname.rfind('.')
[206] Fix | Delete
pname = pname[:i]
[207] Fix | Delete
parent = self.modules[pname]
[208] Fix | Delete
assert parent.__name__ == pname
[209] Fix | Delete
self.msgout(4, "determine_parent ->", parent)
[210] Fix | Delete
return parent
[211] Fix | Delete
self.msgout(4, "determine_parent -> None")
[212] Fix | Delete
return None
[213] Fix | Delete
[214] Fix | Delete
def find_head_package(self, parent, name):
[215] Fix | Delete
self.msgin(4, "find_head_package", parent, name)
[216] Fix | Delete
if '.' in name:
[217] Fix | Delete
i = name.find('.')
[218] Fix | Delete
head = name[:i]
[219] Fix | Delete
tail = name[i+1:]
[220] Fix | Delete
else:
[221] Fix | Delete
head = name
[222] Fix | Delete
tail = ""
[223] Fix | Delete
if parent:
[224] Fix | Delete
qname = "%s.%s" % (parent.__name__, head)
[225] Fix | Delete
else:
[226] Fix | Delete
qname = head
[227] Fix | Delete
q = self.import_module(head, qname, parent)
[228] Fix | Delete
if q:
[229] Fix | Delete
self.msgout(4, "find_head_package ->", (q, tail))
[230] Fix | Delete
return q, tail
[231] Fix | Delete
if parent:
[232] Fix | Delete
qname = head
[233] Fix | Delete
parent = None
[234] Fix | Delete
q = self.import_module(head, qname, parent)
[235] Fix | Delete
if q:
[236] Fix | Delete
self.msgout(4, "find_head_package ->", (q, tail))
[237] Fix | Delete
return q, tail
[238] Fix | Delete
self.msgout(4, "raise ImportError: No module named", qname)
[239] Fix | Delete
raise ImportError("No module named " + qname)
[240] Fix | Delete
[241] Fix | Delete
def load_tail(self, q, tail):
[242] Fix | Delete
self.msgin(4, "load_tail", q, tail)
[243] Fix | Delete
m = q
[244] Fix | Delete
while tail:
[245] Fix | Delete
i = tail.find('.')
[246] Fix | Delete
if i < 0: i = len(tail)
[247] Fix | Delete
head, tail = tail[:i], tail[i+1:]
[248] Fix | Delete
mname = "%s.%s" % (m.__name__, head)
[249] Fix | Delete
m = self.import_module(head, mname, m)
[250] Fix | Delete
if not m:
[251] Fix | Delete
self.msgout(4, "raise ImportError: No module named", mname)
[252] Fix | Delete
raise ImportError("No module named " + mname)
[253] Fix | Delete
self.msgout(4, "load_tail ->", m)
[254] Fix | Delete
return m
[255] Fix | Delete
[256] Fix | Delete
def ensure_fromlist(self, m, fromlist, recursive=0):
[257] Fix | Delete
self.msg(4, "ensure_fromlist", m, fromlist, recursive)
[258] Fix | Delete
for sub in fromlist:
[259] Fix | Delete
if sub == "*":
[260] Fix | Delete
if not recursive:
[261] Fix | Delete
all = self.find_all_submodules(m)
[262] Fix | Delete
if all:
[263] Fix | Delete
self.ensure_fromlist(m, all, 1)
[264] Fix | Delete
elif not hasattr(m, sub):
[265] Fix | Delete
subname = "%s.%s" % (m.__name__, sub)
[266] Fix | Delete
submod = self.import_module(sub, subname, m)
[267] Fix | Delete
if not submod:
[268] Fix | Delete
raise ImportError("No module named " + subname)
[269] Fix | Delete
[270] Fix | Delete
def find_all_submodules(self, m):
[271] Fix | Delete
if not m.__path__:
[272] Fix | Delete
return
[273] Fix | Delete
modules = {}
[274] Fix | Delete
# 'suffixes' used to be a list hardcoded to [".py", ".pyc"].
[275] Fix | Delete
# But we must also collect Python extension modules - although
[276] Fix | Delete
# we cannot separate normal dlls from Python extensions.
[277] Fix | Delete
suffixes = []
[278] Fix | Delete
suffixes += importlib.machinery.EXTENSION_SUFFIXES[:]
[279] Fix | Delete
suffixes += importlib.machinery.SOURCE_SUFFIXES[:]
[280] Fix | Delete
suffixes += importlib.machinery.BYTECODE_SUFFIXES[:]
[281] Fix | Delete
for dir in m.__path__:
[282] Fix | Delete
try:
[283] Fix | Delete
names = os.listdir(dir)
[284] Fix | Delete
except OSError:
[285] Fix | Delete
self.msg(2, "can't list directory", dir)
[286] Fix | Delete
continue
[287] Fix | Delete
for name in names:
[288] Fix | Delete
mod = None
[289] Fix | Delete
for suff in suffixes:
[290] Fix | Delete
n = len(suff)
[291] Fix | Delete
if name[-n:] == suff:
[292] Fix | Delete
mod = name[:-n]
[293] Fix | Delete
break
[294] Fix | Delete
if mod and mod != "__init__":
[295] Fix | Delete
modules[mod] = mod
[296] Fix | Delete
return modules.keys()
[297] Fix | Delete
[298] Fix | Delete
def import_module(self, partname, fqname, parent):
[299] Fix | Delete
self.msgin(3, "import_module", partname, fqname, parent)
[300] Fix | Delete
try:
[301] Fix | Delete
m = self.modules[fqname]
[302] Fix | Delete
except KeyError:
[303] Fix | Delete
pass
[304] Fix | Delete
else:
[305] Fix | Delete
self.msgout(3, "import_module ->", m)
[306] Fix | Delete
return m
[307] Fix | Delete
if fqname in self.badmodules:
[308] Fix | Delete
self.msgout(3, "import_module -> None")
[309] Fix | Delete
return None
[310] Fix | Delete
if parent and parent.__path__ is None:
[311] Fix | Delete
self.msgout(3, "import_module -> None")
[312] Fix | Delete
return None
[313] Fix | Delete
try:
[314] Fix | Delete
fp, pathname, stuff = self.find_module(partname,
[315] Fix | Delete
parent and parent.__path__, parent)
[316] Fix | Delete
except ImportError:
[317] Fix | Delete
self.msgout(3, "import_module ->", None)
[318] Fix | Delete
return None
[319] Fix | Delete
[320] Fix | Delete
try:
[321] Fix | Delete
m = self.load_module(fqname, fp, pathname, stuff)
[322] Fix | Delete
finally:
[323] Fix | Delete
if fp:
[324] Fix | Delete
fp.close()
[325] Fix | Delete
if parent:
[326] Fix | Delete
setattr(parent, partname, m)
[327] Fix | Delete
self.msgout(3, "import_module ->", m)
[328] Fix | Delete
return m
[329] Fix | Delete
[330] Fix | Delete
def load_module(self, fqname, fp, pathname, file_info):
[331] Fix | Delete
suffix, mode, type = file_info
[332] Fix | Delete
self.msgin(2, "load_module", fqname, fp and "fp", pathname)
[333] Fix | Delete
if type == _PKG_DIRECTORY:
[334] Fix | Delete
m = self.load_package(fqname, pathname)
[335] Fix | Delete
self.msgout(2, "load_module ->", m)
[336] Fix | Delete
return m
[337] Fix | Delete
if type == _PY_SOURCE:
[338] Fix | Delete
co = compile(fp.read(), pathname, 'exec')
[339] Fix | Delete
elif type == _PY_COMPILED:
[340] Fix | Delete
try:
[341] Fix | Delete
data = fp.read()
[342] Fix | Delete
importlib._bootstrap_external._classify_pyc(data, fqname, {})
[343] Fix | Delete
except ImportError as exc:
[344] Fix | Delete
self.msgout(2, "raise ImportError: " + str(exc), pathname)
[345] Fix | Delete
raise
[346] Fix | Delete
co = marshal.loads(memoryview(data)[16:])
[347] Fix | Delete
else:
[348] Fix | Delete
co = None
[349] Fix | Delete
m = self.add_module(fqname)
[350] Fix | Delete
m.__file__ = pathname
[351] Fix | Delete
if co:
[352] Fix | Delete
if self.replace_paths:
[353] Fix | Delete
co = self.replace_paths_in_code(co)
[354] Fix | Delete
m.__code__ = co
[355] Fix | Delete
self.scan_code(co, m)
[356] Fix | Delete
self.msgout(2, "load_module ->", m)
[357] Fix | Delete
return m
[358] Fix | Delete
[359] Fix | Delete
def _add_badmodule(self, name, caller):
[360] Fix | Delete
if name not in self.badmodules:
[361] Fix | Delete
self.badmodules[name] = {}
[362] Fix | Delete
if caller:
[363] Fix | Delete
self.badmodules[name][caller.__name__] = 1
[364] Fix | Delete
else:
[365] Fix | Delete
self.badmodules[name]["-"] = 1
[366] Fix | Delete
[367] Fix | Delete
def _safe_import_hook(self, name, caller, fromlist, level=-1):
[368] Fix | Delete
# wrapper for self.import_hook() that won't raise ImportError
[369] Fix | Delete
if name in self.badmodules:
[370] Fix | Delete
self._add_badmodule(name, caller)
[371] Fix | Delete
return
[372] Fix | Delete
try:
[373] Fix | Delete
self.import_hook(name, caller, level=level)
[374] Fix | Delete
except ImportError as msg:
[375] Fix | Delete
self.msg(2, "ImportError:", str(msg))
[376] Fix | Delete
self._add_badmodule(name, caller)
[377] Fix | Delete
except SyntaxError as msg:
[378] Fix | Delete
self.msg(2, "SyntaxError:", str(msg))
[379] Fix | Delete
self._add_badmodule(name, caller)
[380] Fix | Delete
else:
[381] Fix | Delete
if fromlist:
[382] Fix | Delete
for sub in fromlist:
[383] Fix | Delete
fullname = name + "." + sub
[384] Fix | Delete
if fullname in self.badmodules:
[385] Fix | Delete
self._add_badmodule(fullname, caller)
[386] Fix | Delete
continue
[387] Fix | Delete
try:
[388] Fix | Delete
self.import_hook(name, caller, [sub], level=level)
[389] Fix | Delete
except ImportError as msg:
[390] Fix | Delete
self.msg(2, "ImportError:", str(msg))
[391] Fix | Delete
self._add_badmodule(fullname, caller)
[392] Fix | Delete
[393] Fix | Delete
def scan_opcodes(self, co):
[394] Fix | Delete
# Scan the code, and yield 'interesting' opcode combinations
[395] Fix | Delete
code = co.co_code
[396] Fix | Delete
names = co.co_names
[397] Fix | Delete
consts = co.co_consts
[398] Fix | Delete
opargs = [(op, arg) for _, op, arg in dis._unpack_opargs(code)
[399] Fix | Delete
if op != EXTENDED_ARG]
[400] Fix | Delete
for i, (op, oparg) in enumerate(opargs):
[401] Fix | Delete
if op in STORE_OPS:
[402] Fix | Delete
yield "store", (names[oparg],)
[403] Fix | Delete
continue
[404] Fix | Delete
if (op == IMPORT_NAME and i >= 2
[405] Fix | Delete
and opargs[i-1][0] == opargs[i-2][0] == LOAD_CONST):
[406] Fix | Delete
level = consts[opargs[i-2][1]]
[407] Fix | Delete
fromlist = consts[opargs[i-1][1]]
[408] Fix | Delete
if level == 0: # absolute import
[409] Fix | Delete
yield "absolute_import", (fromlist, names[oparg])
[410] Fix | Delete
else: # relative import
[411] Fix | Delete
yield "relative_import", (level, fromlist, names[oparg])
[412] Fix | Delete
continue
[413] Fix | Delete
[414] Fix | Delete
def scan_code(self, co, m):
[415] Fix | Delete
code = co.co_code
[416] Fix | Delete
scanner = self.scan_opcodes
[417] Fix | Delete
for what, args in scanner(co):
[418] Fix | Delete
if what == "store":
[419] Fix | Delete
name, = args
[420] Fix | Delete
m.globalnames[name] = 1
[421] Fix | Delete
elif what == "absolute_import":
[422] Fix | Delete
fromlist, name = args
[423] Fix | Delete
have_star = 0
[424] Fix | Delete
if fromlist is not None:
[425] Fix | Delete
if "*" in fromlist:
[426] Fix | Delete
have_star = 1
[427] Fix | Delete
fromlist = [f for f in fromlist if f != "*"]
[428] Fix | Delete
self._safe_import_hook(name, m, fromlist, level=0)
[429] Fix | Delete
if have_star:
[430] Fix | Delete
# We've encountered an "import *". If it is a Python module,
[431] Fix | Delete
# the code has already been parsed and we can suck out the
[432] Fix | Delete
# global names.
[433] Fix | Delete
mm = None
[434] Fix | Delete
if m.__path__:
[435] Fix | Delete
# At this point we don't know whether 'name' is a
[436] Fix | Delete
# submodule of 'm' or a global module. Let's just try
[437] Fix | Delete
# the full name first.
[438] Fix | Delete
mm = self.modules.get(m.__name__ + "." + name)
[439] Fix | Delete
if mm is None:
[440] Fix | Delete
mm = self.modules.get(name)
[441] Fix | Delete
if mm is not None:
[442] Fix | Delete
m.globalnames.update(mm.globalnames)
[443] Fix | Delete
m.starimports.update(mm.starimports)
[444] Fix | Delete
if mm.__code__ is None:
[445] Fix | Delete
m.starimports[name] = 1
[446] Fix | Delete
else:
[447] Fix | Delete
m.starimports[name] = 1
[448] Fix | Delete
elif what == "relative_import":
[449] Fix | Delete
level, fromlist, name = args
[450] Fix | Delete
if name:
[451] Fix | Delete
self._safe_import_hook(name, m, fromlist, level=level)
[452] Fix | Delete
else:
[453] Fix | Delete
parent = self.determine_parent(m, level=level)
[454] Fix | Delete
self._safe_import_hook(parent.__name__, None, fromlist, level=0)
[455] Fix | Delete
else:
[456] Fix | Delete
# We don't expect anything else from the generator.
[457] Fix | Delete
raise RuntimeError(what)
[458] Fix | Delete
[459] Fix | Delete
for c in co.co_consts:
[460] Fix | Delete
if isinstance(c, type(co)):
[461] Fix | Delete
self.scan_code(c, m)
[462] Fix | Delete
[463] Fix | Delete
def load_package(self, fqname, pathname):
[464] Fix | Delete
self.msgin(2, "load_package", fqname, pathname)
[465] Fix | Delete
newname = replacePackageMap.get(fqname)
[466] Fix | Delete
if newname:
[467] Fix | Delete
fqname = newname
[468] Fix | Delete
m = self.add_module(fqname)
[469] Fix | Delete
m.__file__ = pathname
[470] Fix | Delete
m.__path__ = [pathname]
[471] Fix | Delete
[472] Fix | Delete
# As per comment at top of file, simulate runtime __path__ additions.
[473] Fix | Delete
m.__path__ = m.__path__ + packagePathMap.get(fqname, [])
[474] Fix | Delete
[475] Fix | Delete
fp, buf, stuff = self.find_module("__init__", m.__path__)
[476] Fix | Delete
try:
[477] Fix | Delete
self.load_module(fqname, fp, buf, stuff)
[478] Fix | Delete
self.msgout(2, "load_package ->", m)
[479] Fix | Delete
return m
[480] Fix | Delete
finally:
[481] Fix | Delete
if fp:
[482] Fix | Delete
fp.close()
[483] Fix | Delete
[484] Fix | Delete
def add_module(self, fqname):
[485] Fix | Delete
if fqname in self.modules:
[486] Fix | Delete
return self.modules[fqname]
[487] Fix | Delete
self.modules[fqname] = m = Module(fqname)
[488] Fix | Delete
return m
[489] Fix | Delete
[490] Fix | Delete
def find_module(self, name, path, parent=None):
[491] Fix | Delete
if parent is not None:
[492] Fix | Delete
# assert path is not None
[493] Fix | Delete
fullname = parent.__name__+'.'+name
[494] Fix | Delete
else:
[495] Fix | Delete
fullname = name
[496] Fix | Delete
if fullname in self.excludes:
[497] Fix | Delete
self.msgout(3, "find_module -> Excluded", fullname)
[498] Fix | Delete
raise ImportError(name)
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function