Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../lib64/python2....
File: runpy.py
"""runpy.py - locating and running Python code using the module namespace
[0] Fix | Delete
[1] Fix | Delete
Provides support for locating and running Python scripts using the Python
[2] Fix | Delete
module namespace instead of the native filesystem.
[3] Fix | Delete
[4] Fix | Delete
This allows Python code to play nicely with non-filesystem based PEP 302
[5] Fix | Delete
importers when locating support scripts as well as when importing modules.
[6] Fix | Delete
"""
[7] Fix | Delete
# Written by Nick Coghlan <ncoghlan at gmail.com>
[8] Fix | Delete
# to implement PEP 338 (Executing Modules as Scripts)
[9] Fix | Delete
[10] Fix | Delete
import sys
[11] Fix | Delete
import imp
[12] Fix | Delete
from pkgutil import read_code
[13] Fix | Delete
try:
[14] Fix | Delete
from imp import get_loader
[15] Fix | Delete
except ImportError:
[16] Fix | Delete
from pkgutil import get_loader
[17] Fix | Delete
[18] Fix | Delete
__all__ = [
[19] Fix | Delete
"run_module", "run_path",
[20] Fix | Delete
]
[21] Fix | Delete
[22] Fix | Delete
class _TempModule(object):
[23] Fix | Delete
"""Temporarily replace a module in sys.modules with an empty namespace"""
[24] Fix | Delete
def __init__(self, mod_name):
[25] Fix | Delete
self.mod_name = mod_name
[26] Fix | Delete
self.module = imp.new_module(mod_name)
[27] Fix | Delete
self._saved_module = []
[28] Fix | Delete
[29] Fix | Delete
def __enter__(self):
[30] Fix | Delete
mod_name = self.mod_name
[31] Fix | Delete
try:
[32] Fix | Delete
self._saved_module.append(sys.modules[mod_name])
[33] Fix | Delete
except KeyError:
[34] Fix | Delete
pass
[35] Fix | Delete
sys.modules[mod_name] = self.module
[36] Fix | Delete
return self
[37] Fix | Delete
[38] Fix | Delete
def __exit__(self, *args):
[39] Fix | Delete
if self._saved_module:
[40] Fix | Delete
sys.modules[self.mod_name] = self._saved_module[0]
[41] Fix | Delete
else:
[42] Fix | Delete
del sys.modules[self.mod_name]
[43] Fix | Delete
self._saved_module = []
[44] Fix | Delete
[45] Fix | Delete
class _ModifiedArgv0(object):
[46] Fix | Delete
def __init__(self, value):
[47] Fix | Delete
self.value = value
[48] Fix | Delete
self._saved_value = self._sentinel = object()
[49] Fix | Delete
[50] Fix | Delete
def __enter__(self):
[51] Fix | Delete
if self._saved_value is not self._sentinel:
[52] Fix | Delete
raise RuntimeError("Already preserving saved value")
[53] Fix | Delete
self._saved_value = sys.argv[0]
[54] Fix | Delete
sys.argv[0] = self.value
[55] Fix | Delete
[56] Fix | Delete
def __exit__(self, *args):
[57] Fix | Delete
self.value = self._sentinel
[58] Fix | Delete
sys.argv[0] = self._saved_value
[59] Fix | Delete
[60] Fix | Delete
def _run_code(code, run_globals, init_globals=None,
[61] Fix | Delete
mod_name=None, mod_fname=None,
[62] Fix | Delete
mod_loader=None, pkg_name=None):
[63] Fix | Delete
"""Helper to run code in nominated namespace"""
[64] Fix | Delete
if init_globals is not None:
[65] Fix | Delete
run_globals.update(init_globals)
[66] Fix | Delete
run_globals.update(__name__ = mod_name,
[67] Fix | Delete
__file__ = mod_fname,
[68] Fix | Delete
__loader__ = mod_loader,
[69] Fix | Delete
__package__ = pkg_name)
[70] Fix | Delete
exec code in run_globals
[71] Fix | Delete
return run_globals
[72] Fix | Delete
[73] Fix | Delete
def _run_module_code(code, init_globals=None,
[74] Fix | Delete
mod_name=None, mod_fname=None,
[75] Fix | Delete
mod_loader=None, pkg_name=None):
[76] Fix | Delete
"""Helper to run code in new namespace with sys modified"""
[77] Fix | Delete
with _TempModule(mod_name) as temp_module, _ModifiedArgv0(mod_fname):
[78] Fix | Delete
mod_globals = temp_module.module.__dict__
[79] Fix | Delete
_run_code(code, mod_globals, init_globals,
[80] Fix | Delete
mod_name, mod_fname, mod_loader, pkg_name)
[81] Fix | Delete
# Copy the globals of the temporary module, as they
[82] Fix | Delete
# may be cleared when the temporary module goes away
[83] Fix | Delete
return mod_globals.copy()
[84] Fix | Delete
[85] Fix | Delete
[86] Fix | Delete
# This helper is needed due to a missing component in the PEP 302
[87] Fix | Delete
# loader protocol (specifically, "get_filename" is non-standard)
[88] Fix | Delete
# Since we can't introduce new features in maintenance releases,
[89] Fix | Delete
# support was added to zipimporter under the name '_get_filename'
[90] Fix | Delete
def _get_filename(loader, mod_name):
[91] Fix | Delete
for attr in ("get_filename", "_get_filename"):
[92] Fix | Delete
meth = getattr(loader, attr, None)
[93] Fix | Delete
if meth is not None:
[94] Fix | Delete
return meth(mod_name)
[95] Fix | Delete
return None
[96] Fix | Delete
[97] Fix | Delete
# Helper to get the loader, code and filename for a module
[98] Fix | Delete
def _get_module_details(mod_name, error=ImportError):
[99] Fix | Delete
try:
[100] Fix | Delete
loader = get_loader(mod_name)
[101] Fix | Delete
if loader is None:
[102] Fix | Delete
raise error("No module named %s" % mod_name)
[103] Fix | Delete
ispkg = loader.is_package(mod_name)
[104] Fix | Delete
except ImportError as e:
[105] Fix | Delete
raise error(format(e))
[106] Fix | Delete
if ispkg:
[107] Fix | Delete
if mod_name == "__main__" or mod_name.endswith(".__main__"):
[108] Fix | Delete
raise error("Cannot use package as __main__ module")
[109] Fix | Delete
__import__(mod_name) # Do not catch exceptions initializing package
[110] Fix | Delete
try:
[111] Fix | Delete
pkg_main_name = mod_name + ".__main__"
[112] Fix | Delete
return _get_module_details(pkg_main_name)
[113] Fix | Delete
except ImportError, e:
[114] Fix | Delete
raise error(("%s; %r is a package and cannot " +
[115] Fix | Delete
"be directly executed") %(e, mod_name))
[116] Fix | Delete
try:
[117] Fix | Delete
code = loader.get_code(mod_name)
[118] Fix | Delete
except ImportError as e:
[119] Fix | Delete
raise error(format(e))
[120] Fix | Delete
if code is None:
[121] Fix | Delete
raise error("No code object available for %s" % mod_name)
[122] Fix | Delete
filename = _get_filename(loader, mod_name)
[123] Fix | Delete
return mod_name, loader, code, filename
[124] Fix | Delete
[125] Fix | Delete
[126] Fix | Delete
def _get_main_module_details(error=ImportError):
[127] Fix | Delete
# Helper that gives a nicer error message when attempting to
[128] Fix | Delete
# execute a zipfile or directory by invoking __main__.py
[129] Fix | Delete
main_name = "__main__"
[130] Fix | Delete
try:
[131] Fix | Delete
return _get_module_details(main_name)
[132] Fix | Delete
except ImportError as exc:
[133] Fix | Delete
if main_name in str(exc):
[134] Fix | Delete
raise error("can't find %r module in %r" %
[135] Fix | Delete
(main_name, sys.path[0]))
[136] Fix | Delete
raise
[137] Fix | Delete
[138] Fix | Delete
class _Error(Exception):
[139] Fix | Delete
"""Error that _run_module_as_main() should report without a traceback"""
[140] Fix | Delete
[141] Fix | Delete
# This function is the actual implementation of the -m switch and direct
[142] Fix | Delete
# execution of zipfiles and directories and is deliberately kept private.
[143] Fix | Delete
# This avoids a repeat of the situation where run_module() no longer met the
[144] Fix | Delete
# needs of mainmodule.c, but couldn't be changed because it was public
[145] Fix | Delete
def _run_module_as_main(mod_name, alter_argv=True):
[146] Fix | Delete
"""Runs the designated module in the __main__ namespace
[147] Fix | Delete
[148] Fix | Delete
Note that the executed module will have full access to the
[149] Fix | Delete
__main__ namespace. If this is not desirable, the run_module()
[150] Fix | Delete
function should be used to run the module code in a fresh namespace.
[151] Fix | Delete
[152] Fix | Delete
At the very least, these variables in __main__ will be overwritten:
[153] Fix | Delete
__name__
[154] Fix | Delete
__file__
[155] Fix | Delete
__loader__
[156] Fix | Delete
__package__
[157] Fix | Delete
"""
[158] Fix | Delete
try:
[159] Fix | Delete
if alter_argv or mod_name != "__main__": # i.e. -m switch
[160] Fix | Delete
mod_name, loader, code, fname = _get_module_details(
[161] Fix | Delete
mod_name, _Error)
[162] Fix | Delete
else: # i.e. directory or zipfile execution
[163] Fix | Delete
mod_name, loader, code, fname = _get_main_module_details(_Error)
[164] Fix | Delete
except _Error as exc:
[165] Fix | Delete
msg = "%s: %s" % (sys.executable, exc)
[166] Fix | Delete
sys.exit(msg)
[167] Fix | Delete
pkg_name = mod_name.rpartition('.')[0]
[168] Fix | Delete
main_globals = sys.modules["__main__"].__dict__
[169] Fix | Delete
if alter_argv:
[170] Fix | Delete
sys.argv[0] = fname
[171] Fix | Delete
return _run_code(code, main_globals, None,
[172] Fix | Delete
"__main__", fname, loader, pkg_name)
[173] Fix | Delete
[174] Fix | Delete
def run_module(mod_name, init_globals=None,
[175] Fix | Delete
run_name=None, alter_sys=False):
[176] Fix | Delete
"""Execute a module's code without importing it
[177] Fix | Delete
[178] Fix | Delete
Returns the resulting top level namespace dictionary
[179] Fix | Delete
"""
[180] Fix | Delete
mod_name, loader, code, fname = _get_module_details(mod_name)
[181] Fix | Delete
if run_name is None:
[182] Fix | Delete
run_name = mod_name
[183] Fix | Delete
pkg_name = mod_name.rpartition('.')[0]
[184] Fix | Delete
if alter_sys:
[185] Fix | Delete
return _run_module_code(code, init_globals, run_name,
[186] Fix | Delete
fname, loader, pkg_name)
[187] Fix | Delete
else:
[188] Fix | Delete
# Leave the sys module alone
[189] Fix | Delete
return _run_code(code, {}, init_globals, run_name,
[190] Fix | Delete
fname, loader, pkg_name)
[191] Fix | Delete
[192] Fix | Delete
[193] Fix | Delete
# XXX (ncoghlan): Perhaps expose the C API function
[194] Fix | Delete
# as imp.get_importer instead of reimplementing it in Python?
[195] Fix | Delete
def _get_importer(path_name):
[196] Fix | Delete
"""Python version of PyImport_GetImporter C API function"""
[197] Fix | Delete
cache = sys.path_importer_cache
[198] Fix | Delete
try:
[199] Fix | Delete
importer = cache[path_name]
[200] Fix | Delete
except KeyError:
[201] Fix | Delete
# Not yet cached. Flag as using the
[202] Fix | Delete
# standard machinery until we finish
[203] Fix | Delete
# checking the hooks
[204] Fix | Delete
cache[path_name] = None
[205] Fix | Delete
for hook in sys.path_hooks:
[206] Fix | Delete
try:
[207] Fix | Delete
importer = hook(path_name)
[208] Fix | Delete
break
[209] Fix | Delete
except ImportError:
[210] Fix | Delete
pass
[211] Fix | Delete
else:
[212] Fix | Delete
# The following check looks a bit odd. The trick is that
[213] Fix | Delete
# NullImporter raises ImportError if the supplied path is a
[214] Fix | Delete
# *valid* directory entry (and hence able to be handled
[215] Fix | Delete
# by the standard import machinery)
[216] Fix | Delete
try:
[217] Fix | Delete
importer = imp.NullImporter(path_name)
[218] Fix | Delete
except ImportError:
[219] Fix | Delete
return None
[220] Fix | Delete
cache[path_name] = importer
[221] Fix | Delete
return importer
[222] Fix | Delete
[223] Fix | Delete
def _get_code_from_file(fname):
[224] Fix | Delete
# Check for a compiled file first
[225] Fix | Delete
with open(fname, "rb") as f:
[226] Fix | Delete
code = read_code(f)
[227] Fix | Delete
if code is None:
[228] Fix | Delete
# That didn't work, so try it as normal source code
[229] Fix | Delete
with open(fname, "rU") as f:
[230] Fix | Delete
code = compile(f.read(), fname, 'exec')
[231] Fix | Delete
return code
[232] Fix | Delete
[233] Fix | Delete
def run_path(path_name, init_globals=None, run_name=None):
[234] Fix | Delete
"""Execute code located at the specified filesystem location
[235] Fix | Delete
[236] Fix | Delete
Returns the resulting top level namespace dictionary
[237] Fix | Delete
[238] Fix | Delete
The file path may refer directly to a Python script (i.e.
[239] Fix | Delete
one that could be directly executed with execfile) or else
[240] Fix | Delete
it may refer to a zipfile or directory containing a top
[241] Fix | Delete
level __main__.py script.
[242] Fix | Delete
"""
[243] Fix | Delete
if run_name is None:
[244] Fix | Delete
run_name = "<run_path>"
[245] Fix | Delete
importer = _get_importer(path_name)
[246] Fix | Delete
if isinstance(importer, imp.NullImporter):
[247] Fix | Delete
# Not a valid sys.path entry, so run the code directly
[248] Fix | Delete
# execfile() doesn't help as we want to allow compiled files
[249] Fix | Delete
code = _get_code_from_file(path_name)
[250] Fix | Delete
return _run_module_code(code, init_globals, run_name, path_name)
[251] Fix | Delete
else:
[252] Fix | Delete
# Importer is defined for path, so add it to
[253] Fix | Delete
# the start of sys.path
[254] Fix | Delete
sys.path.insert(0, path_name)
[255] Fix | Delete
try:
[256] Fix | Delete
# Here's where things are a little different from the run_module
[257] Fix | Delete
# case. There, we only had to replace the module in sys while the
[258] Fix | Delete
# code was running and doing so was somewhat optional. Here, we
[259] Fix | Delete
# have no choice and we have to remove it even while we read the
[260] Fix | Delete
# code. If we don't do this, a __loader__ attribute in the
[261] Fix | Delete
# existing __main__ module may prevent location of the new module.
[262] Fix | Delete
main_name = "__main__"
[263] Fix | Delete
saved_main = sys.modules[main_name]
[264] Fix | Delete
del sys.modules[main_name]
[265] Fix | Delete
try:
[266] Fix | Delete
mod_name, loader, code, fname = _get_main_module_details()
[267] Fix | Delete
finally:
[268] Fix | Delete
sys.modules[main_name] = saved_main
[269] Fix | Delete
pkg_name = ""
[270] Fix | Delete
with _TempModule(run_name) as temp_module, \
[271] Fix | Delete
_ModifiedArgv0(path_name):
[272] Fix | Delete
mod_globals = temp_module.module.__dict__
[273] Fix | Delete
return _run_code(code, mod_globals, init_globals,
[274] Fix | Delete
run_name, fname, loader, pkg_name).copy()
[275] Fix | Delete
finally:
[276] Fix | Delete
try:
[277] Fix | Delete
sys.path.remove(path_name)
[278] Fix | Delete
except ValueError:
[279] Fix | Delete
pass
[280] Fix | Delete
[281] Fix | Delete
[282] Fix | Delete
if __name__ == "__main__":
[283] Fix | Delete
# Run the module specified as the next command line argument
[284] Fix | Delete
if len(sys.argv) < 2:
[285] Fix | Delete
print >> sys.stderr, "No module specified for execution"
[286] Fix | Delete
else:
[287] Fix | Delete
del sys.argv[0] # Make the requested module sys.argv[0]
[288] Fix | Delete
_run_module_as_main(sys.argv[0])
[289] Fix | Delete
[290] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function