-
Notifications
You must be signed in to change notification settings - Fork 0
/
interact.py
221 lines (187 loc) · 6.46 KB
/
interact.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# David Creswick and Richard Darst, 2005
"""Improved code.interact()
Can take globals argument. Can automatically find locals and globals
based on the stack - by default gets them from one stack frame up.
Suggested uses:
In Python, interact now:
import fitz.interact ; interact.interact()
In Python, interact now. This only works once, since the module is
only imported once:
import fitz.interactnow
In Python, go into interact mode at a traceback.
import fitz.interacttb
Run from command line and drop into PDB upon each traceback:
python -m fitz.pdbtb
... or enable it from python:
import fitz.pdbtb ; pdbtb.enable()
"""
import code
import inspect
import os
import sys
# Set up readline. Perhaps this will interact with other things badly
# (i.e., if readline shouldn't be here), but I'll deal with that when
# it comes.
import readline
import rlcompleter
readline.parse_and_bind("tab: complete")
scrollback = 20
line_continuers = (",", "\\")
tab = " "
def interact(banner="", local=None, stackLevel=1, frame=None,
mode='interact', history=False):
"""Interact using the current global and local scope and history.
arguments:
banner -- print this text before interacting.
local -- ignored
history: bool or int
if non-false, this many lines of history are found from the
file being interacted with and inserted into the readline
buffer
"""
if len(banner) > 0:
print banner
# Get data from calling frame
#calling_data = inspect.stack()[stackLevel]
#filename = calling_data[1]
#lineno = calling_data[2]
#frame = calling_data[0]
#local = frame.f_locals
#global_ = frame.f_globals
if frame is None:
frame = inspect.stack()[stackLevel][0]
filename = frame.f_code.co_filename
lineno = frame.f_lineno
local = frame.f_locals
global_ = frame.f_globals
if history:
new_history = [ ]
if os.access(filename, os.R_OK):
new_history.extend(get_history(filename, lineno))
# add lines to history, removing proper amount of whitespace
for line in new_history:
readline.add_history(line)
# add an exit() function under the name __exit. Hopefully this
# won't cause namespace collisions!
local["__exit"] = sys.exit
if mode == 'pdb':
import pdb
p = pdb.Pdb()
def do_quit(self, arg):
sys.exit()
p.do_quit = type(p.do_quit)(do_quit, p, pdb.Pdb)
p.do_q = p.do_quit
p.reset()
p.interaction(frame, None)
#from fitz import interactnow
#pdb.post_mortem(frame)
else:
interact2(local=local, global_=global_, banner="")
def pdb(*args, **kwargs):
return interact(*args, mode='pdb',
stackLevel=kwargs.pop('stackLevel', 1)+1,
**kwargs)
def get_history(filename, lineno):
# get lines
f = file(filename)
lines = f.readlines()
f.close()
#get amount of leading whitespace
current_line = lines[lineno-1]
whitespace = len(current_line) - len(current_line.lstrip())
# Get just the lines we want
if lineno-scrollback >= 0:
lines = lines[lineno-scrollback:lineno-1]
else:
lines = lines[0:lineno-1]
# Reconstruct list of lines, taking into account wrapping
newlines = [ ]
curline = ""
continuation = False
for line in lines:
line = line.replace("\t", tab)
line = line.rstrip()
if line == "":
continue
if continuation:
line = line.lstrip()
# Determine if we have a complete command. If it is, append
# it to newlines, and set continuation = False. If it isn't,e
# append it to curline and set continuation = True
if len(line) > 0 and line[-1] == "\\":
curline = "".join((curline, line[:-1]+" "))
continuation = True
elif len(line) > 0 and line[-1] == ",":
curline = "".join((curline, line+" "))
continuation = True
else:
newlines.append("".join((curline, line)))
curline = ""
continuation = False
# Strip leading whitespace
for i, line in enumerate(newlines):
if len(line[:whitespace].strip()) == 0:
# if it starts with just whitespace
newlines[i] = line.rstrip()[whitespace:]
else:
# we need less whitespace here
newlines[i] = line.strip()
return newlines
def null_function(a, b):
pass
# Hack up an interactive interperter. I just do the minimal
# modifications to what is in code.py. Note, that I do use some
# Python distribution code here. It is GPL compatible.
# The usage of the variable names `local` and `locals`, etc, is
# unfortunante. We see both of them. I try to be consistent with
# what already exists, but `global` is a statement!
class InteractiveConsole(code.InteractiveConsole):
def __init__(self, locals=None,
globals=None,
filename="<console>"):
"""Documentation comes from `code.InteractiveConsole`"""
# We use the previous initialization method, but now we also
# set self.globals to what we passed.
code.InteractiveConsole.__init__(self, locals)
self.globals = globals
def runcode(self, code_):
# This comes straight from code.py
try:
#exec code in self.locals
exec code_ in self.globals, self.locals
except SystemExit:
raise
except:
self.showtraceback()
else:
if code.softspace(sys.stdout, 0):
print
def interact2(banner=None, readfunc=None, local=None, global_=None):
"""Closely emulate `code.interact`
This function is exactly like `code.interact` (see it's
documentation), but has the additional argument:
globals -- mapping to use for global namespace namespace.
"""
console = InteractiveConsole(locals=local,
globals=global_)
if readfunc is not None:
console.raw_input = readfunc
else: # this is redundant since we imported readline above.
try:
import readline
except ImportError:
pass
console.interact(banner)
if __name__ == "__main__":
a = 12
# print vars()
if True:
pass
null_function(1, \
2)
null_function(1, 2 +
2)
null_function(1,
2)
interact()
print vars()