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