Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: code.py
"""Utilities needed to emulate Python's interactive interpreter.
[0] Fix | Delete
[1] Fix | Delete
"""
[2] Fix | Delete
[3] Fix | Delete
# Inspired by similar code by Jeff Epler and Fredrik Lundh.
[4] Fix | Delete
[5] Fix | Delete
[6] Fix | Delete
import sys
[7] Fix | Delete
import traceback
[8] Fix | Delete
import argparse
[9] Fix | Delete
from codeop import CommandCompiler, compile_command
[10] Fix | Delete
[11] Fix | Delete
__all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact",
[12] Fix | Delete
"compile_command"]
[13] Fix | Delete
[14] Fix | Delete
class InteractiveInterpreter:
[15] Fix | Delete
"""Base class for InteractiveConsole.
[16] Fix | Delete
[17] Fix | Delete
This class deals with parsing and interpreter state (the user's
[18] Fix | Delete
namespace); it doesn't deal with input buffering or prompting or
[19] Fix | Delete
input file naming (the filename is always passed in explicitly).
[20] Fix | Delete
[21] Fix | Delete
"""
[22] Fix | Delete
[23] Fix | Delete
def __init__(self, locals=None):
[24] Fix | Delete
"""Constructor.
[25] Fix | Delete
[26] Fix | Delete
The optional 'locals' argument specifies the dictionary in
[27] Fix | Delete
which code will be executed; it defaults to a newly created
[28] Fix | Delete
dictionary with key "__name__" set to "__console__" and key
[29] Fix | Delete
"__doc__" set to None.
[30] Fix | Delete
[31] Fix | Delete
"""
[32] Fix | Delete
if locals is None:
[33] Fix | Delete
locals = {"__name__": "__console__", "__doc__": None}
[34] Fix | Delete
self.locals = locals
[35] Fix | Delete
self.compile = CommandCompiler()
[36] Fix | Delete
[37] Fix | Delete
def runsource(self, source, filename="<input>", symbol="single"):
[38] Fix | Delete
"""Compile and run some source in the interpreter.
[39] Fix | Delete
[40] Fix | Delete
Arguments are as for compile_command().
[41] Fix | Delete
[42] Fix | Delete
One several things can happen:
[43] Fix | Delete
[44] Fix | Delete
1) The input is incorrect; compile_command() raised an
[45] Fix | Delete
exception (SyntaxError or OverflowError). A syntax traceback
[46] Fix | Delete
will be printed by calling the showsyntaxerror() method.
[47] Fix | Delete
[48] Fix | Delete
2) The input is incomplete, and more input is required;
[49] Fix | Delete
compile_command() returned None. Nothing happens.
[50] Fix | Delete
[51] Fix | Delete
3) The input is complete; compile_command() returned a code
[52] Fix | Delete
object. The code is executed by calling self.runcode() (which
[53] Fix | Delete
also handles run-time exceptions, except for SystemExit).
[54] Fix | Delete
[55] Fix | Delete
The return value is True in case 2, False in the other cases (unless
[56] Fix | Delete
an exception is raised). The return value can be used to
[57] Fix | Delete
decide whether to use sys.ps1 or sys.ps2 to prompt the next
[58] Fix | Delete
line.
[59] Fix | Delete
[60] Fix | Delete
"""
[61] Fix | Delete
try:
[62] Fix | Delete
code = self.compile(source, filename, symbol)
[63] Fix | Delete
except (OverflowError, SyntaxError, ValueError):
[64] Fix | Delete
# Case 1
[65] Fix | Delete
self.showsyntaxerror(filename)
[66] Fix | Delete
return False
[67] Fix | Delete
[68] Fix | Delete
if code is None:
[69] Fix | Delete
# Case 2
[70] Fix | Delete
return True
[71] Fix | Delete
[72] Fix | Delete
# Case 3
[73] Fix | Delete
self.runcode(code)
[74] Fix | Delete
return False
[75] Fix | Delete
[76] Fix | Delete
def runcode(self, code):
[77] Fix | Delete
"""Execute a code object.
[78] Fix | Delete
[79] Fix | Delete
When an exception occurs, self.showtraceback() is called to
[80] Fix | Delete
display a traceback. All exceptions are caught except
[81] Fix | Delete
SystemExit, which is reraised.
[82] Fix | Delete
[83] Fix | Delete
A note about KeyboardInterrupt: this exception may occur
[84] Fix | Delete
elsewhere in this code, and may not always be caught. The
[85] Fix | Delete
caller should be prepared to deal with it.
[86] Fix | Delete
[87] Fix | Delete
"""
[88] Fix | Delete
try:
[89] Fix | Delete
exec(code, self.locals)
[90] Fix | Delete
except SystemExit:
[91] Fix | Delete
raise
[92] Fix | Delete
except:
[93] Fix | Delete
self.showtraceback()
[94] Fix | Delete
[95] Fix | Delete
def showsyntaxerror(self, filename=None):
[96] Fix | Delete
"""Display the syntax error that just occurred.
[97] Fix | Delete
[98] Fix | Delete
This doesn't display a stack trace because there isn't one.
[99] Fix | Delete
[100] Fix | Delete
If a filename is given, it is stuffed in the exception instead
[101] Fix | Delete
of what was there before (because Python's parser always uses
[102] Fix | Delete
"<string>" when reading from a string).
[103] Fix | Delete
[104] Fix | Delete
The output is written by self.write(), below.
[105] Fix | Delete
[106] Fix | Delete
"""
[107] Fix | Delete
type, value, tb = sys.exc_info()
[108] Fix | Delete
sys.last_type = type
[109] Fix | Delete
sys.last_value = value
[110] Fix | Delete
sys.last_traceback = tb
[111] Fix | Delete
if filename and type is SyntaxError:
[112] Fix | Delete
# Work hard to stuff the correct filename in the exception
[113] Fix | Delete
try:
[114] Fix | Delete
msg, (dummy_filename, lineno, offset, line) = value.args
[115] Fix | Delete
except ValueError:
[116] Fix | Delete
# Not the format we expect; leave it alone
[117] Fix | Delete
pass
[118] Fix | Delete
else:
[119] Fix | Delete
# Stuff in the right filename
[120] Fix | Delete
value = SyntaxError(msg, (filename, lineno, offset, line))
[121] Fix | Delete
sys.last_value = value
[122] Fix | Delete
if sys.excepthook is sys.__excepthook__:
[123] Fix | Delete
lines = traceback.format_exception_only(type, value)
[124] Fix | Delete
self.write(''.join(lines))
[125] Fix | Delete
else:
[126] Fix | Delete
# If someone has set sys.excepthook, we let that take precedence
[127] Fix | Delete
# over self.write
[128] Fix | Delete
sys.excepthook(type, value, tb)
[129] Fix | Delete
[130] Fix | Delete
def showtraceback(self):
[131] Fix | Delete
"""Display the exception that just occurred.
[132] Fix | Delete
[133] Fix | Delete
We remove the first stack item because it is our own code.
[134] Fix | Delete
[135] Fix | Delete
The output is written by self.write(), below.
[136] Fix | Delete
[137] Fix | Delete
"""
[138] Fix | Delete
sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
[139] Fix | Delete
sys.last_traceback = last_tb
[140] Fix | Delete
try:
[141] Fix | Delete
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next)
[142] Fix | Delete
if sys.excepthook is sys.__excepthook__:
[143] Fix | Delete
self.write(''.join(lines))
[144] Fix | Delete
else:
[145] Fix | Delete
# If someone has set sys.excepthook, we let that take precedence
[146] Fix | Delete
# over self.write
[147] Fix | Delete
sys.excepthook(ei[0], ei[1], last_tb)
[148] Fix | Delete
finally:
[149] Fix | Delete
last_tb = ei = None
[150] Fix | Delete
[151] Fix | Delete
def write(self, data):
[152] Fix | Delete
"""Write a string.
[153] Fix | Delete
[154] Fix | Delete
The base implementation writes to sys.stderr; a subclass may
[155] Fix | Delete
replace this with a different implementation.
[156] Fix | Delete
[157] Fix | Delete
"""
[158] Fix | Delete
sys.stderr.write(data)
[159] Fix | Delete
[160] Fix | Delete
[161] Fix | Delete
class InteractiveConsole(InteractiveInterpreter):
[162] Fix | Delete
"""Closely emulate the behavior of the interactive Python interpreter.
[163] Fix | Delete
[164] Fix | Delete
This class builds on InteractiveInterpreter and adds prompting
[165] Fix | Delete
using the familiar sys.ps1 and sys.ps2, and input buffering.
[166] Fix | Delete
[167] Fix | Delete
"""
[168] Fix | Delete
[169] Fix | Delete
def __init__(self, locals=None, filename="<console>"):
[170] Fix | Delete
"""Constructor.
[171] Fix | Delete
[172] Fix | Delete
The optional locals argument will be passed to the
[173] Fix | Delete
InteractiveInterpreter base class.
[174] Fix | Delete
[175] Fix | Delete
The optional filename argument should specify the (file)name
[176] Fix | Delete
of the input stream; it will show up in tracebacks.
[177] Fix | Delete
[178] Fix | Delete
"""
[179] Fix | Delete
InteractiveInterpreter.__init__(self, locals)
[180] Fix | Delete
self.filename = filename
[181] Fix | Delete
self.resetbuffer()
[182] Fix | Delete
[183] Fix | Delete
def resetbuffer(self):
[184] Fix | Delete
"""Reset the input buffer."""
[185] Fix | Delete
self.buffer = []
[186] Fix | Delete
[187] Fix | Delete
def interact(self, banner=None, exitmsg=None):
[188] Fix | Delete
"""Closely emulate the interactive Python console.
[189] Fix | Delete
[190] Fix | Delete
The optional banner argument specifies the banner to print
[191] Fix | Delete
before the first interaction; by default it prints a banner
[192] Fix | Delete
similar to the one printed by the real Python interpreter,
[193] Fix | Delete
followed by the current class name in parentheses (so as not
[194] Fix | Delete
to confuse this with the real interpreter -- since it's so
[195] Fix | Delete
close!).
[196] Fix | Delete
[197] Fix | Delete
The optional exitmsg argument specifies the exit message
[198] Fix | Delete
printed when exiting. Pass the empty string to suppress
[199] Fix | Delete
printing an exit message. If exitmsg is not given or None,
[200] Fix | Delete
a default message is printed.
[201] Fix | Delete
[202] Fix | Delete
"""
[203] Fix | Delete
try:
[204] Fix | Delete
sys.ps1
[205] Fix | Delete
except AttributeError:
[206] Fix | Delete
sys.ps1 = ">>> "
[207] Fix | Delete
try:
[208] Fix | Delete
sys.ps2
[209] Fix | Delete
except AttributeError:
[210] Fix | Delete
sys.ps2 = "... "
[211] Fix | Delete
cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
[212] Fix | Delete
if banner is None:
[213] Fix | Delete
self.write("Python %s on %s\n%s\n(%s)\n" %
[214] Fix | Delete
(sys.version, sys.platform, cprt,
[215] Fix | Delete
self.__class__.__name__))
[216] Fix | Delete
elif banner:
[217] Fix | Delete
self.write("%s\n" % str(banner))
[218] Fix | Delete
more = 0
[219] Fix | Delete
while 1:
[220] Fix | Delete
try:
[221] Fix | Delete
if more:
[222] Fix | Delete
prompt = sys.ps2
[223] Fix | Delete
else:
[224] Fix | Delete
prompt = sys.ps1
[225] Fix | Delete
try:
[226] Fix | Delete
line = self.raw_input(prompt)
[227] Fix | Delete
except EOFError:
[228] Fix | Delete
self.write("\n")
[229] Fix | Delete
break
[230] Fix | Delete
else:
[231] Fix | Delete
more = self.push(line)
[232] Fix | Delete
except KeyboardInterrupt:
[233] Fix | Delete
self.write("\nKeyboardInterrupt\n")
[234] Fix | Delete
self.resetbuffer()
[235] Fix | Delete
more = 0
[236] Fix | Delete
if exitmsg is None:
[237] Fix | Delete
self.write('now exiting %s...\n' % self.__class__.__name__)
[238] Fix | Delete
elif exitmsg != '':
[239] Fix | Delete
self.write('%s\n' % exitmsg)
[240] Fix | Delete
[241] Fix | Delete
def push(self, line):
[242] Fix | Delete
"""Push a line to the interpreter.
[243] Fix | Delete
[244] Fix | Delete
The line should not have a trailing newline; it may have
[245] Fix | Delete
internal newlines. The line is appended to a buffer and the
[246] Fix | Delete
interpreter's runsource() method is called with the
[247] Fix | Delete
concatenated contents of the buffer as source. If this
[248] Fix | Delete
indicates that the command was executed or invalid, the buffer
[249] Fix | Delete
is reset; otherwise, the command is incomplete, and the buffer
[250] Fix | Delete
is left as it was after the line was appended. The return
[251] Fix | Delete
value is 1 if more input is required, 0 if the line was dealt
[252] Fix | Delete
with in some way (this is the same as runsource()).
[253] Fix | Delete
[254] Fix | Delete
"""
[255] Fix | Delete
self.buffer.append(line)
[256] Fix | Delete
source = "\n".join(self.buffer)
[257] Fix | Delete
more = self.runsource(source, self.filename)
[258] Fix | Delete
if not more:
[259] Fix | Delete
self.resetbuffer()
[260] Fix | Delete
return more
[261] Fix | Delete
[262] Fix | Delete
def raw_input(self, prompt=""):
[263] Fix | Delete
"""Write a prompt and read a line.
[264] Fix | Delete
[265] Fix | Delete
The returned line does not include the trailing newline.
[266] Fix | Delete
When the user enters the EOF key sequence, EOFError is raised.
[267] Fix | Delete
[268] Fix | Delete
The base implementation uses the built-in function
[269] Fix | Delete
input(); a subclass may replace this with a different
[270] Fix | Delete
implementation.
[271] Fix | Delete
[272] Fix | Delete
"""
[273] Fix | Delete
return input(prompt)
[274] Fix | Delete
[275] Fix | Delete
[276] Fix | Delete
[277] Fix | Delete
def interact(banner=None, readfunc=None, local=None, exitmsg=None):
[278] Fix | Delete
"""Closely emulate the interactive Python interpreter.
[279] Fix | Delete
[280] Fix | Delete
This is a backwards compatible interface to the InteractiveConsole
[281] Fix | Delete
class. When readfunc is not specified, it attempts to import the
[282] Fix | Delete
readline module to enable GNU readline if it is available.
[283] Fix | Delete
[284] Fix | Delete
Arguments (all optional, all default to None):
[285] Fix | Delete
[286] Fix | Delete
banner -- passed to InteractiveConsole.interact()
[287] Fix | Delete
readfunc -- if not None, replaces InteractiveConsole.raw_input()
[288] Fix | Delete
local -- passed to InteractiveInterpreter.__init__()
[289] Fix | Delete
exitmsg -- passed to InteractiveConsole.interact()
[290] Fix | Delete
[291] Fix | Delete
"""
[292] Fix | Delete
console = InteractiveConsole(local)
[293] Fix | Delete
if readfunc is not None:
[294] Fix | Delete
console.raw_input = readfunc
[295] Fix | Delete
else:
[296] Fix | Delete
try:
[297] Fix | Delete
import readline
[298] Fix | Delete
except ImportError:
[299] Fix | Delete
pass
[300] Fix | Delete
console.interact(banner, exitmsg)
[301] Fix | Delete
[302] Fix | Delete
[303] Fix | Delete
if __name__ == "__main__":
[304] Fix | Delete
parser = argparse.ArgumentParser()
[305] Fix | Delete
parser.add_argument('-q', action='store_true',
[306] Fix | Delete
help="don't print version and copyright messages")
[307] Fix | Delete
args = parser.parse_args()
[308] Fix | Delete
if args.q or sys.flags.quiet:
[309] Fix | Delete
banner = ''
[310] Fix | Delete
else:
[311] Fix | Delete
banner = None
[312] Fix | Delete
interact(banner)
[313] Fix | Delete
[314] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function