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