Repository navigation
Expand file tree
/
Copy pathpatch_linecache.py
More file actions
181 lines (105 loc) · 5.3 KB
/
Copy pathpatch_linecache.py
File metadata and controls
181 lines (105 loc) · 5.3 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#THIS CODE IS FROM bpython's patch_linecache.py. Get it with pip install bpython.
#I adapted it for use in rp to provide better stack traces
#This module is what lets us do this:
# File <rp-input-4>, line 1, in <module>
# f()
# File <rp-input-3>, line 2, in f
# 1 def f():
# --> 2 g()
# File <rp-input-3>, line 4, in g
# 3 def g():
# --> 4 h()
# File <rp-input-3>, line 6, in h
# def h():
# ZeroDivisionError: division by zero
#Note how the stack trace includes lines of code in it, even though they're from user inputs. Previously I needed ipython to do this but I found that bpython also did it. Then I stole bypthon's code and put it in here. lol. Maybe I'll rewrite it some day....when I rewrite rp...
import linecache
class BPythonLinecache(dict):
"""Replaces the cache dict in the standard-library linecache module,
to also remember (in an unerasable way) rp console input."""
def __init__(self, *args, **kwargs):
super(BPythonLinecache, self).__init__(*args, **kwargs)
self.bpython_history = []
def is_bpython_filename(self, fname):
try:
return fname.startswith('<rp-input-')
except AttributeError:
# In case the key isn't a string
return False
def get_bpython_history(self, key):
"""Given a filename provided by remember_bpython_input,
returns the associated source string."""
try:
idx = int(key.split('-')[2][:-1])
return self.bpython_history[idx]
except (IndexError, ValueError):
raise KeyError
def remember_bpython_input(self, source):
"""Remembers a string of source code, and returns
a fake filename to use to retrieve it later."""
filename = '<rp-input-%s>' % len(self.bpython_history)
self.bpython_history.append((len(source), None,
source.splitlines(True), filename))
return filename
def __getitem__(self, key):
if self.is_bpython_filename(key):
return self.get_bpython_history(key)
return super(BPythonLinecache, self).__getitem__(key)
def __contains__(self, key):
if self.is_bpython_filename(key):
try:
self.get_bpython_history(key)
return True
except KeyError:
return False
return super(BPythonLinecache, self).__contains__(key)
def __delitem__(self, key):
if not self.is_bpython_filename(key):
return super(BPythonLinecache, self).__delitem__(key)
def _bpython_clear_linecache():
try:
bpython_history = linecache.cache.bpython_history
except AttributeError:
bpython_history = []
linecache.cache = BPythonLinecache()
linecache.cache.bpython_history = bpython_history
# Monkey-patch the linecache module so that we're able
# to hold our command history there and have it persist
linecache.cache = BPythonLinecache(linecache.cache)
linecache.clearcache = _bpython_clear_linecache
def filename_for_console_input(code_string):
"""Remembers a string of source code, and returns
a fake filename to use to retrieve it later."""
try:
return linecache.cache.remember_bpython_input(code_string)
except AttributeError:
# If someone else has patched linecache.cache, better for code to
# simply be unavailable to inspect.getsource() than to raise
# an exception.
return '<input>'
fc=filename_for_console_input
import code
#ans=code.InteractiveInterpreter().runsource('18/0',fc('18/0'),'single')
interpereter=code.InteractiveInterpreter()
# ans=interpereter.compile('148/0',fc('148/0'),'single')
# def ans():
# pass
# def run(code):
# exec(interpereter.compile(code,fc(code),'single'))
# ans=fc
def run_code(code,mode,namespace,exv):
import sys
old_tracer=sys.gettrace()#To make sure any debuggers launched in this exeval aren't carried through to pseudo-terminal internal code, while still letting you call pseudo-terminal from a debugger
try:
#THE FOLLOWING 4 LINES HAVE BEEN BOILED DOWN INTO 1 LINE TO MAKE EXITING THE DEBUGGER FASTER
#comp=interpereter.compile(code,fc(code),mode)
#if comp is None:#This means the code is incomplete; see help(interpereter.compile). Because interpereter.compile("(","<filename",'exec') returns None and not an error.
# #I want it to raise an error, though. We know it won't compile, so we might as well call exec.
# exec(code) #This shouldn't actually run anything; it should throw an EOF error with the appropriate text. Also, pseudo_terminal should hide the difference from the user.
#else:
# return exv(comp,namespace,namespace)
################################# EXITING THE DEBUGGER #######################################
return exv(interpereter.compile(code,fc(code),mode) or exec(code),namespace,namespace)
finally:#This should happen no matter what, but shouldn't squelch any errors we get when evaluating the code
#EXIT THE DEBUGGER: (Do this so we don't debug the next pseudo-terminal prompt as well)
sys.settrace(old_tracer)#Instead of just setting it to None, set it to what it was before. This allows us to run pseudo_terminal from a debugger without exiting the debugger.