Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: codeop.py
r"""Utilities to compile possibly incomplete Python source code.
[0] Fix | Delete
[1] Fix | Delete
This module provides two interfaces, broadly similar to the builtin
[2] Fix | Delete
function compile(), which take program text, a filename and a 'mode'
[3] Fix | Delete
and:
[4] Fix | Delete
[5] Fix | Delete
- Return code object if the command is complete and valid
[6] Fix | Delete
- Return None if the command is incomplete
[7] Fix | Delete
- Raise SyntaxError, ValueError or OverflowError if the command is a
[8] Fix | Delete
syntax error (OverflowError and ValueError can be produced by
[9] Fix | Delete
malformed literals).
[10] Fix | Delete
[11] Fix | Delete
Approach:
[12] Fix | Delete
[13] Fix | Delete
First, check if the source consists entirely of blank lines and
[14] Fix | Delete
comments; if so, replace it with 'pass', because the built-in
[15] Fix | Delete
parser doesn't always do the right thing for these.
[16] Fix | Delete
[17] Fix | Delete
Compile three times: as is, with \n, and with \n\n appended. If it
[18] Fix | Delete
compiles as is, it's complete. If it compiles with one \n appended,
[19] Fix | Delete
we expect more. If it doesn't compile either way, we compare the
[20] Fix | Delete
error we get when compiling with \n or \n\n appended. If the errors
[21] Fix | Delete
are the same, the code is broken. But if the errors are different, we
[22] Fix | Delete
expect more. Not intuitive; not even guaranteed to hold in future
[23] Fix | Delete
releases; but this matches the compiler's behavior from Python 1.4
[24] Fix | Delete
through 2.2, at least.
[25] Fix | Delete
[26] Fix | Delete
Caveat:
[27] Fix | Delete
[28] Fix | Delete
It is possible (but not likely) that the parser stops parsing with a
[29] Fix | Delete
successful outcome before reaching the end of the source; in this
[30] Fix | Delete
case, trailing symbols may be ignored instead of causing an error.
[31] Fix | Delete
For example, a backslash followed by two newlines may be followed by
[32] Fix | Delete
arbitrary garbage. This will be fixed once the API for the parser is
[33] Fix | Delete
better.
[34] Fix | Delete
[35] Fix | Delete
The two interfaces are:
[36] Fix | Delete
[37] Fix | Delete
compile_command(source, filename, symbol):
[38] Fix | Delete
[39] Fix | Delete
Compiles a single command in the manner described above.
[40] Fix | Delete
[41] Fix | Delete
CommandCompiler():
[42] Fix | Delete
[43] Fix | Delete
Instances of this class have __call__ methods identical in
[44] Fix | Delete
signature to compile_command; the difference is that if the
[45] Fix | Delete
instance compiles program text containing a __future__ statement,
[46] Fix | Delete
the instance 'remembers' and compiles all subsequent program texts
[47] Fix | Delete
with the statement in force.
[48] Fix | Delete
[49] Fix | Delete
The module also provides another class:
[50] Fix | Delete
[51] Fix | Delete
Compile():
[52] Fix | Delete
[53] Fix | Delete
Instances of this class act like the built-in function compile,
[54] Fix | Delete
but with 'memory' in the sense described above.
[55] Fix | Delete
"""
[56] Fix | Delete
[57] Fix | Delete
import __future__
[58] Fix | Delete
[59] Fix | Delete
_features = [getattr(__future__, fname)
[60] Fix | Delete
for fname in __future__.all_feature_names]
[61] Fix | Delete
[62] Fix | Delete
__all__ = ["compile_command", "Compile", "CommandCompiler"]
[63] Fix | Delete
[64] Fix | Delete
PyCF_DONT_IMPLY_DEDENT = 0x200 # Matches pythonrun.h
[65] Fix | Delete
[66] Fix | Delete
def _maybe_compile(compiler, source, filename, symbol):
[67] Fix | Delete
# Check for source consisting of only blank lines and comments
[68] Fix | Delete
for line in source.split("\n"):
[69] Fix | Delete
line = line.strip()
[70] Fix | Delete
if line and line[0] != '#':
[71] Fix | Delete
break # Leave it alone
[72] Fix | Delete
else:
[73] Fix | Delete
if symbol != "eval":
[74] Fix | Delete
source = "pass" # Replace it with a 'pass' statement
[75] Fix | Delete
[76] Fix | Delete
err = err1 = err2 = None
[77] Fix | Delete
code = code1 = code2 = None
[78] Fix | Delete
[79] Fix | Delete
try:
[80] Fix | Delete
code = compiler(source, filename, symbol)
[81] Fix | Delete
except SyntaxError as err:
[82] Fix | Delete
pass
[83] Fix | Delete
[84] Fix | Delete
try:
[85] Fix | Delete
code1 = compiler(source + "\n", filename, symbol)
[86] Fix | Delete
except SyntaxError as e:
[87] Fix | Delete
err1 = e
[88] Fix | Delete
[89] Fix | Delete
try:
[90] Fix | Delete
code2 = compiler(source + "\n\n", filename, symbol)
[91] Fix | Delete
except SyntaxError as e:
[92] Fix | Delete
err2 = e
[93] Fix | Delete
[94] Fix | Delete
if code:
[95] Fix | Delete
return code
[96] Fix | Delete
if not code1 and repr(err1) == repr(err2):
[97] Fix | Delete
raise err1
[98] Fix | Delete
[99] Fix | Delete
def _compile(source, filename, symbol):
[100] Fix | Delete
return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT)
[101] Fix | Delete
[102] Fix | Delete
def compile_command(source, filename="<input>", symbol="single"):
[103] Fix | Delete
r"""Compile a command and determine whether it is incomplete.
[104] Fix | Delete
[105] Fix | Delete
Arguments:
[106] Fix | Delete
[107] Fix | Delete
source -- the source string; may contain \n characters
[108] Fix | Delete
filename -- optional filename from which source was read; default
[109] Fix | Delete
"<input>"
[110] Fix | Delete
symbol -- optional grammar start symbol; "single" (default) or "eval"
[111] Fix | Delete
[112] Fix | Delete
Return value / exceptions raised:
[113] Fix | Delete
[114] Fix | Delete
- Return a code object if the command is complete and valid
[115] Fix | Delete
- Return None if the command is incomplete
[116] Fix | Delete
- Raise SyntaxError, ValueError or OverflowError if the command is a
[117] Fix | Delete
syntax error (OverflowError and ValueError can be produced by
[118] Fix | Delete
malformed literals).
[119] Fix | Delete
"""
[120] Fix | Delete
return _maybe_compile(_compile, source, filename, symbol)
[121] Fix | Delete
[122] Fix | Delete
class Compile:
[123] Fix | Delete
"""Instances of this class behave much like the built-in compile
[124] Fix | Delete
function, but if one is used to compile text containing a future
[125] Fix | Delete
statement, it "remembers" and compiles all subsequent program texts
[126] Fix | Delete
with the statement in force."""
[127] Fix | Delete
def __init__(self):
[128] Fix | Delete
self.flags = PyCF_DONT_IMPLY_DEDENT
[129] Fix | Delete
[130] Fix | Delete
def __call__(self, source, filename, symbol):
[131] Fix | Delete
codeob = compile(source, filename, symbol, self.flags, 1)
[132] Fix | Delete
for feature in _features:
[133] Fix | Delete
if codeob.co_flags & feature.compiler_flag:
[134] Fix | Delete
self.flags |= feature.compiler_flag
[135] Fix | Delete
return codeob
[136] Fix | Delete
[137] Fix | Delete
class CommandCompiler:
[138] Fix | Delete
"""Instances of this class have __call__ methods identical in
[139] Fix | Delete
signature to compile_command; the difference is that if the
[140] Fix | Delete
instance compiles program text containing a __future__ statement,
[141] Fix | Delete
the instance 'remembers' and compiles all subsequent program texts
[142] Fix | Delete
with the statement in force."""
[143] Fix | Delete
[144] Fix | Delete
def __init__(self,):
[145] Fix | Delete
self.compiler = Compile()
[146] Fix | Delete
[147] Fix | Delete
def __call__(self, source, filename="<input>", symbol="single"):
[148] Fix | Delete
r"""Compile a command and determine whether it is incomplete.
[149] Fix | Delete
[150] Fix | Delete
Arguments:
[151] Fix | Delete
[152] Fix | Delete
source -- the source string; may contain \n characters
[153] Fix | Delete
filename -- optional filename from which source was read;
[154] Fix | Delete
default "<input>"
[155] Fix | Delete
symbol -- optional grammar start symbol; "single" (default) or
[156] Fix | Delete
"eval"
[157] Fix | Delete
[158] Fix | Delete
Return value / exceptions raised:
[159] Fix | Delete
[160] Fix | Delete
- Return a code object if the command is complete and valid
[161] Fix | Delete
- Return None if the command is incomplete
[162] Fix | Delete
- Raise SyntaxError, ValueError or OverflowError if the command is a
[163] Fix | Delete
syntax error (OverflowError and ValueError can be produced by
[164] Fix | Delete
malformed literals).
[165] Fix | Delete
"""
[166] Fix | Delete
return _maybe_compile(self.compiler, source, filename, symbol)
[167] Fix | Delete
[168] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function