Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../proc/self/root/lib64/python3....
File: py_compile.py
"""Routine to "compile" a .py file to a .pyc file.
[0] Fix | Delete
[1] Fix | Delete
This module has intimate knowledge of the format of .pyc files.
[2] Fix | Delete
"""
[3] Fix | Delete
[4] Fix | Delete
import importlib._bootstrap_external
[5] Fix | Delete
import importlib.machinery
[6] Fix | Delete
import importlib.util
[7] Fix | Delete
import os
[8] Fix | Delete
import os.path
[9] Fix | Delete
import sys
[10] Fix | Delete
import traceback
[11] Fix | Delete
[12] Fix | Delete
__all__ = ["compile", "main", "PyCompileError"]
[13] Fix | Delete
[14] Fix | Delete
[15] Fix | Delete
class PyCompileError(Exception):
[16] Fix | Delete
"""Exception raised when an error occurs while attempting to
[17] Fix | Delete
compile the file.
[18] Fix | Delete
[19] Fix | Delete
To raise this exception, use
[20] Fix | Delete
[21] Fix | Delete
raise PyCompileError(exc_type,exc_value,file[,msg])
[22] Fix | Delete
[23] Fix | Delete
where
[24] Fix | Delete
[25] Fix | Delete
exc_type: exception type to be used in error message
[26] Fix | Delete
type name can be accesses as class variable
[27] Fix | Delete
'exc_type_name'
[28] Fix | Delete
[29] Fix | Delete
exc_value: exception value to be used in error message
[30] Fix | Delete
can be accesses as class variable 'exc_value'
[31] Fix | Delete
[32] Fix | Delete
file: name of file being compiled to be used in error message
[33] Fix | Delete
can be accesses as class variable 'file'
[34] Fix | Delete
[35] Fix | Delete
msg: string message to be written as error message
[36] Fix | Delete
If no value is given, a default exception message will be
[37] Fix | Delete
given, consistent with 'standard' py_compile output.
[38] Fix | Delete
message (or default) can be accesses as class variable
[39] Fix | Delete
'msg'
[40] Fix | Delete
[41] Fix | Delete
"""
[42] Fix | Delete
[43] Fix | Delete
def __init__(self, exc_type, exc_value, file, msg=''):
[44] Fix | Delete
exc_type_name = exc_type.__name__
[45] Fix | Delete
if exc_type is SyntaxError:
[46] Fix | Delete
tbtext = ''.join(traceback.format_exception_only(
[47] Fix | Delete
exc_type, exc_value))
[48] Fix | Delete
errmsg = tbtext.replace('File "<string>"', 'File "%s"' % file)
[49] Fix | Delete
else:
[50] Fix | Delete
errmsg = "Sorry: %s: %s" % (exc_type_name,exc_value)
[51] Fix | Delete
[52] Fix | Delete
Exception.__init__(self,msg or errmsg,exc_type_name,exc_value,file)
[53] Fix | Delete
[54] Fix | Delete
self.exc_type_name = exc_type_name
[55] Fix | Delete
self.exc_value = exc_value
[56] Fix | Delete
self.file = file
[57] Fix | Delete
self.msg = msg or errmsg
[58] Fix | Delete
[59] Fix | Delete
def __str__(self):
[60] Fix | Delete
return self.msg
[61] Fix | Delete
[62] Fix | Delete
[63] Fix | Delete
def compile(file, cfile=None, dfile=None, doraise=False, optimize=-1):
[64] Fix | Delete
"""Byte-compile one Python source file to Python bytecode.
[65] Fix | Delete
[66] Fix | Delete
:param file: The source file name.
[67] Fix | Delete
:param cfile: The target byte compiled file name. When not given, this
[68] Fix | Delete
defaults to the PEP 3147/PEP 488 location.
[69] Fix | Delete
:param dfile: Purported file name, i.e. the file name that shows up in
[70] Fix | Delete
error messages. Defaults to the source file name.
[71] Fix | Delete
:param doraise: Flag indicating whether or not an exception should be
[72] Fix | Delete
raised when a compile error is found. If an exception occurs and this
[73] Fix | Delete
flag is set to False, a string indicating the nature of the exception
[74] Fix | Delete
will be printed, and the function will return to the caller. If an
[75] Fix | Delete
exception occurs and this flag is set to True, a PyCompileError
[76] Fix | Delete
exception will be raised.
[77] Fix | Delete
:param optimize: The optimization level for the compiler. Valid values
[78] Fix | Delete
are -1, 0, 1 and 2. A value of -1 means to use the optimization
[79] Fix | Delete
level of the current interpreter, as given by -O command line options.
[80] Fix | Delete
[81] Fix | Delete
:return: Path to the resulting byte compiled file.
[82] Fix | Delete
[83] Fix | Delete
Note that it isn't necessary to byte-compile Python modules for
[84] Fix | Delete
execution efficiency -- Python itself byte-compiles a module when
[85] Fix | Delete
it is loaded, and if it can, writes out the bytecode to the
[86] Fix | Delete
corresponding .pyc file.
[87] Fix | Delete
[88] Fix | Delete
However, if a Python installation is shared between users, it is a
[89] Fix | Delete
good idea to byte-compile all modules upon installation, since
[90] Fix | Delete
other users may not be able to write in the source directories,
[91] Fix | Delete
and thus they won't be able to write the .pyc file, and then
[92] Fix | Delete
they would be byte-compiling every module each time it is loaded.
[93] Fix | Delete
This can slow down program start-up considerably.
[94] Fix | Delete
[95] Fix | Delete
See compileall.py for a script/module that uses this module to
[96] Fix | Delete
byte-compile all installed files (or all files in selected
[97] Fix | Delete
directories).
[98] Fix | Delete
[99] Fix | Delete
Do note that FileExistsError is raised if cfile ends up pointing at a
[100] Fix | Delete
non-regular file or symlink. Because the compilation uses a file renaming,
[101] Fix | Delete
the resulting file would be regular and thus not the same type of file as
[102] Fix | Delete
it was previously.
[103] Fix | Delete
"""
[104] Fix | Delete
if cfile is None:
[105] Fix | Delete
if optimize >= 0:
[106] Fix | Delete
optimization = optimize if optimize >= 1 else ''
[107] Fix | Delete
cfile = importlib.util.cache_from_source(file,
[108] Fix | Delete
optimization=optimization)
[109] Fix | Delete
else:
[110] Fix | Delete
cfile = importlib.util.cache_from_source(file)
[111] Fix | Delete
if os.path.islink(cfile):
[112] Fix | Delete
msg = ('{} is a symlink and will be changed into a regular file if '
[113] Fix | Delete
'import writes a byte-compiled file to it')
[114] Fix | Delete
raise FileExistsError(msg.format(cfile))
[115] Fix | Delete
elif os.path.exists(cfile) and not os.path.isfile(cfile):
[116] Fix | Delete
msg = ('{} is a non-regular file and will be changed into a regular '
[117] Fix | Delete
'one if import writes a byte-compiled file to it')
[118] Fix | Delete
raise FileExistsError(msg.format(cfile))
[119] Fix | Delete
loader = importlib.machinery.SourceFileLoader('<py_compile>', file)
[120] Fix | Delete
source_bytes = loader.get_data(file)
[121] Fix | Delete
try:
[122] Fix | Delete
code = loader.source_to_code(source_bytes, dfile or file,
[123] Fix | Delete
_optimize=optimize)
[124] Fix | Delete
except Exception as err:
[125] Fix | Delete
py_exc = PyCompileError(err.__class__, err, dfile or file)
[126] Fix | Delete
if doraise:
[127] Fix | Delete
raise py_exc
[128] Fix | Delete
else:
[129] Fix | Delete
sys.stderr.write(py_exc.msg + '\n')
[130] Fix | Delete
return
[131] Fix | Delete
try:
[132] Fix | Delete
dirname = os.path.dirname(cfile)
[133] Fix | Delete
if dirname:
[134] Fix | Delete
os.makedirs(dirname)
[135] Fix | Delete
except FileExistsError:
[136] Fix | Delete
pass
[137] Fix | Delete
source_stats = loader.path_stats(file)
[138] Fix | Delete
bytecode = importlib._bootstrap_external._code_to_bytecode(
[139] Fix | Delete
code, source_stats['mtime'], source_stats['size'])
[140] Fix | Delete
mode = importlib._bootstrap_external._calc_mode(file)
[141] Fix | Delete
importlib._bootstrap_external._write_atomic(cfile, bytecode, mode)
[142] Fix | Delete
return cfile
[143] Fix | Delete
[144] Fix | Delete
[145] Fix | Delete
def main(args=None):
[146] Fix | Delete
"""Compile several source files.
[147] Fix | Delete
[148] Fix | Delete
The files named in 'args' (or on the command line, if 'args' is
[149] Fix | Delete
not specified) are compiled and the resulting bytecode is cached
[150] Fix | Delete
in the normal manner. This function does not search a directory
[151] Fix | Delete
structure to locate source files; it only compiles files named
[152] Fix | Delete
explicitly. If '-' is the only parameter in args, the list of
[153] Fix | Delete
files is taken from standard input.
[154] Fix | Delete
[155] Fix | Delete
"""
[156] Fix | Delete
if args is None:
[157] Fix | Delete
args = sys.argv[1:]
[158] Fix | Delete
rv = 0
[159] Fix | Delete
if args == ['-']:
[160] Fix | Delete
while True:
[161] Fix | Delete
filename = sys.stdin.readline()
[162] Fix | Delete
if not filename:
[163] Fix | Delete
break
[164] Fix | Delete
filename = filename.rstrip('\n')
[165] Fix | Delete
try:
[166] Fix | Delete
compile(filename, doraise=True)
[167] Fix | Delete
except PyCompileError as error:
[168] Fix | Delete
rv = 1
[169] Fix | Delete
sys.stderr.write("%s\n" % error.msg)
[170] Fix | Delete
except OSError as error:
[171] Fix | Delete
rv = 1
[172] Fix | Delete
sys.stderr.write("%s\n" % error)
[173] Fix | Delete
else:
[174] Fix | Delete
for filename in args:
[175] Fix | Delete
try:
[176] Fix | Delete
compile(filename, doraise=True)
[177] Fix | Delete
except PyCompileError as error:
[178] Fix | Delete
# return value to indicate at least one failure
[179] Fix | Delete
rv = 1
[180] Fix | Delete
sys.stderr.write("%s\n" % error.msg)
[181] Fix | Delete
return rv
[182] Fix | Delete
[183] Fix | Delete
if __name__ == "__main__":
[184] Fix | Delete
sys.exit(main())
[185] Fix | Delete
[186] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function