-
Notifications
You must be signed in to change notification settings - Fork 0
/
explore.py
269 lines (237 loc) · 7.52 KB
/
explore.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import traceback
import sys
import inspect
import os
import re
try: input = raw_input
except NameError: pass
COMMANDS={
'go':'go:\tResume program execution',
'goend':'goend:\tResume execution and turn off current breakpoint',
'info':'info:\tPrint this help',
'exit':'exit:\tTry to sys.exit',
'gowhere ':'gowhere [condition]:\tStop at this breakpoint only when [condition] is true',
'up':'up:\tUp one level in stack',
'down':'down:\tDown one level in stack',
'stack':'stack:\tShow full stack',
'stack ':'stack LEVEL:\tGo to selected level in stack',
'save': 'save [var]:\tSave var to file',
'whereami': 'whereami [N]:\tPrint code of breakpoint [+ nearest N lines]',
}
WELCOME=True
def welcome():
global WELCOME
if WELCOME:
print ("Type 'info' for help")
WELCOME=False
LOC,GLOB=None,None
DISCART_POINTS=[]
CONDITION_POINTS={}
def completer(text,state):
global LOC,GLOB,COMMANDS
if LOC==None or GLOB==None: return None
#TODO find [+,-,/,*] in text and take end of string
m = re.match('^([^\n]*[*-+\/]{1})([^*+-\/]+$)',text)
begin=''
if m!=None:
print (m.groups())
begin=m.group(1)
text=m.group(2)
match=[]
if '.' in text:
#attrs
n=text.rfind('.')
objname=text[:n]
attrpath=text[n+1:]
try:
obj=eval(objname, LOC, GLOB)
except (SystemError) as e: raise
except:
return None
for attr in dir(obj):
if attr.startswith(attrpath):
match.append('%s%s.%s' % (begin,objname,attr))
else:
#vars
for varlist in (COMMANDS.keys(), LOC.keys(), GLOB.keys(), keyword.kwlist):
for var in varlist:
if not var in match and var.startswith(text):
match.append(begin+var)
match.sort()
try:
return match[state]
except IndexError:
return None
def stop():
frame = sys._getframe()
stack=[]
while frame:
stack.append(frame)
frame=frame.f_back
return navigate(stack[1:])
def handle_error(fn,*ar,**kw):
try:
fn(*ar,**kw)
except (SystemError, KeyboardInterrupt) as e: raise
except:
print (from_traceback())
#
def from_traceback():
print (traceback.format_exc())
tb=sys.exc_info()[2]
stack=[]
while tb:
stack.append(tb.tb_frame)
tb=tb.tb_next
stack.reverse()
# autoselect level
level = 0
for lev, frame in enumerate(stack):
filename = frame.f_code.co_filename
# TODO pandas name
if not 'site-packages' in filename and not 'pandas/' in filename:
level = lev
break
return navigate(stack, level=level)
def set_env(stack, level):
global LOC,GLOB,COMMANDS,DISCART_POINTS
frame=stack[level]
LOC= frame.f_locals
GLOB = frame.f_globals
point = '%s:%s' % (frame.f_code.co_filename,frame.f_lineno)
return frame, point
def navigate(stack, level=0):
global LOC,GLOB,COMMANDS,DISCART_POINTS
if not sys.stdout.isatty():
return
frame, point=set_env(stack, level)
if point in DISCART_POINTS:
return
if point in CONDITION_POINTS:
try:
r=eval(CONDITION_POINTS[point],frame.f_locals,frame.f_globals)
if not r: return
except (SystemError, KeyboardInterrupt) as e: raise
except:
print (traceback.format_exc())
#stop on error
CONDITION_POINTS.pop(point)
welcome()
while True:
line=input(point[-20:]+'>').strip()
if line in ('go','goend'):
if line=='goend': DISCART_POINTS.append(point)
LOC,GLOB=None,None
break
elif line == 'up':
if level>=len(stack):
print ('No up level' )
level+=1
frame,point=set_env(stack, level)
elif line == 'down':
if level<=0:
print ('No down level' )
level+=-1
frame,point=set_env(stack, level)
elif line =='stack':
st=[]
stack_len = len(stack)
for xlevel in range(stack_len):
xframe=stack[xlevel]
l='%s \t%s:%s' % (xlevel, xframe.f_code.co_filename,xframe.f_lineno)
if xlevel==level:
l='>'+l[1:]
st.append(l)
st.reverse()
print ('\n'.join(st))
elif re.match('stack [\d]+$', line):
lev = int(line[5:])
if lev >= 0 and lev < len(stack):
level = lev
frame, point = set_env(stack, level)
elif line=='info':
for i in COMMANDS:
print (COMMANDS[i])
elif line=='exit':
sys.exit(0)
elif re.match('whereami [\d]+$', line) or line == 'whereami':
window = 3
ar = line.split(' ')
if len(ar) > 1 and ar[1].isdigit():
window = int(ar[1])
fname = frame.f_code.co_filename
lineno = frame.f_lineno
if os.path.exists(fname):
try:
with open(fname) as fd:
for num, line in enumerate(fd, 1):
if abs(lineno - num) <= window:
cursor = '->' if lineno == num else ' '
print(f'{cursor}{num}\t{line[:-1]}')
continue
except OSError:
pass
print(':(')
elif line.startswith('gowhere '):
cond=line[len('gowhere '):]
try:
obj=eval(cond, LOC, GLOB)
CONDITION_POINTS[point]=cond
LOC,GLOB=None,None
break
except (SystemError, KeyboardInterrupt) as e: raise
except:
print (traceback.format_exc())
elif line.startswith('save '):
obj=line[len('save '):]
try:
varname = re.sub('[^\w]+', '', obj)
obj=eval(obj, LOC, GLOB)
if isinstance(obj,bytes):
obj = obj.decode()
elif isinstance(obj,str):
pass
else:
obj = str(obj)
open('out_{}.txt'.format(varname), 'w').write(obj)
LOC,GLOB=None,None
except (SystemError, KeyboardInterrupt) as e: raise
except:
print (traceback.format_exc())
elif len(line)>1 and line[-1]=='?':
#inspect object
inspect_obj(line[:-1])
elif line == '':
pass
else:
execute(line)
def inspect_obj(line):
try:
obj=eval(line, LOC, GLOB)
except (SystemError) as e: raise
except:
print ("Object '%s' not found" % line)
else:
try: print ('\t',obj.__class__)
except AttributeError: pass
try: print ('\t',inspect.getabsfile(obj))
except (NameError, TypeError): pass
if inspect.isfunction(obj):
print ('\tDefinition: ' + obj.__name__ + inspect.formatargspec(*inspect.getargspec(obj)))
print (' ',obj.__doc__)
def execute(line):
global LOC,GLOB
if not re.findall('[^\=]{1}[\=]{1}[^\=]{1}',line) and not (line.startswith('print') or line.startswith('import ') or line.startswith('from ')):
line = 'print (repr(' + line +'))'
try:
exec(line, LOC, GLOB)
except (SystemError) as e: raise
except:
print (traceback.format_exc())
try:
import readline
readline.parse_and_bind("tab: complete")
readline.set_completer(completer)
import keyword
except ImportError:
pass