-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathconsole.py
More file actions
152 lines (130 loc) · 4.81 KB
/
console.py
File metadata and controls
152 lines (130 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
See documentation for the class Console defined here.
"""
import code
import sys
import traceback
from wx.py.pseudo import PseudoFileIn, PseudoFileOut, PseudoFileErr
class Console(code.InteractiveConsole):
"""
Console based on code.InteractiveConsole. You are supposed to run this
console in a process you create with the `multiprocessing` package.
It requires a parameter `queue_pack`, which you should create with
`make_queue_pack()` in this package. You are supposed to feed the same
queue pack into the Shell you will create for the two to be connected
to each other.
"""
def __init__(self, queue_pack, *args, **kwargs):
code.InteractiveConsole.__init__(self, *args, **kwargs)
(self.input_queue,
self.output_queue,
self.runcode_finished_queue,
self.runsource_return_queue) = queue_pack
self.readfunc = self.input_queue.get
self.writefunc = self.output_queue.put
self.stdin = PseudoFileIn(self.readfunc)
self.stdout = PseudoFileOut(self.writefunc)
self.stderr = PseudoFileErr(self.writefunc)
def raw_input(self, prompt=None):
if prompt:
self.write(prompt)
return self.readfunc()
def write(self, output):
# self.log(output)
return self.writefunc(output)
def log(self, output):
print(output)
sys.stdout.flush()
def push(self, command):
more = self.runsource(command, self.filename)
return more
def showsyntaxerror(self, filename=None):
ex_type, value, sys.last_traceback = sys.exc_info()
sys.last_type = ex_type
sys.last_value = value
if filename and ex_type is SyntaxError:
# Work hard to stuff the correct filename in the exception
try:
msg, (dummy_filename, lineno, offset, line) = value
except ValueError:
# Not the format we expect; leave it alone
pass
else:
# Stuff in the right filename
value = SyntaxError(msg, (filename, lineno, offset, line))
sys.last_value = value
ex_list = traceback.format_exception_only(ex_type, value)
map(self.write, ex_list)
def runsource(self, source, filename="<input>", symbol="single"):
try:
code = self.compile(source, filename, symbol)
except (OverflowError, SyntaxError, ValueError):
# Case 1
self.showsyntaxerror(filename)
self.runsource_return_queue.put(False)
self.runcode_finished_queue.put(None)
return False
if code is None:
# Case 2
self.runsource_return_queue.put(True)
self.runcode_finished_queue.put(None)
return True
# Case 3
self.runsource_return_queue.put(False)
stdin, stdout, stderr = \
sys.stdin, sys.stdout, sys.stderr
sys.stdin, sys.stdout, sys.stderr = \
self.stdin, self.stdout, self.stderr
try:
self.runcode(code)
finally:
if sys.stdin == self.stdin:
sys.stdin = stdin
if sys.stdout == self.stdout:
sys.stdout = stdout
if sys.stderr == self.stderr:
sys.stderr = stderr
self.runcode_finished_queue.put(None)
return False
def interact(self, banner=None):
try:
sys.ps1
except AttributeError:
sys.ps1 = ">>> "
try:
sys.ps2
except AttributeError:
sys.ps2 = "... "
"""
cprt = 'Type "help", "copyright", "credits" or "license" ' \
'for more information.'
if banner is None:
self.write("Python %s on %s\n%s\n(%s)\n" %
(sys.version, sys.platform, cprt,
self.__class__.__name__))
else:
self.write("%s\n" % str(banner))
"""
more = 0
while True:
try:
if more:
pass # prompt = sys.ps2
else:
pass # prompt = sys.ps1
try:
line = self.raw_input() # prompt)
# Can be None if sys.stdin was redefined
encoding = getattr(sys.stdin, "encoding", None)
if encoding and not isinstance(line, unicode):
line = line.decode(encoding)
except EOFError:
self.write("\n")
break
else:
# self.log(line.__repr__())
more = self.push(line)
except KeyboardInterrupt:
self.write("\nKeyboardInterrupt\n")
self.resetbuffer()
more = 0