Skip to content

Commit 505d46c

Browse files
committed
bah
1 parent 5a4d72a commit 505d46c

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

v3/opt-ipy.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
# TODO: support incremental pushes to the OPT frontend for efficiency
3636
# and better "snappiness"
3737

38-
# TODO: keep global variables in the right order so that elements don't
39-
# "jump" around
40-
4138
# TODO: support line number adjustments for function definitions/calls
4239
# (right now opt-ipy doesn't jump into function calls at all)
4340

@@ -83,7 +80,7 @@ def get_full_trace(self):
8380

8481

8582
def run_str(self, stmt_str, user_globals):
86-
opt_trace = pg_logger.exec_str_with_user_ns(stmt_str, user_globals, get_trace)
83+
opt_trace = pg_logger.exec_str_with_user_ns(stmt_str, user_globals, lambda cod, trace: trace)
8784

8885
# 'clean up' the trace a bit:
8986
if len(self.output_traces):
@@ -92,6 +89,26 @@ def run_str(self, stmt_str, user_globals):
9289
end_of_last_trace = self.output_traces[-1].pop()
9390
#print 'END:', end_of_last_trace
9491
#print 'CUR:', opt_trace[0]
92+
last_ordered_globals = list(end_of_last_trace['ordered_globals']) # copy just to be paranoid
93+
94+
# patch up ordered_globals with last_ordered_globals to
95+
# maintain continuity, i.e., prevent variable display from "jumping"
96+
for t in opt_trace:
97+
og = t['ordered_globals']
98+
og_set = set(og)
99+
100+
# reorder og to use last_ordered_globals as a prefix to
101+
# maintain order
102+
new_og = [e for e in last_ordered_globals if e in og_set]
103+
new_og_set = set(new_og)
104+
105+
# patch in leftovers
106+
leftovers = [e for e in og if e not in new_og_set]
107+
new_og.extend(leftovers)
108+
109+
assert len(og) == len(new_og)
110+
t['ordered_globals'] = new_og
111+
95112

96113
# clobber the last entry
97114
if self.last_exec_is_exception:
@@ -125,9 +142,6 @@ def run_str(self, stmt_str, user_globals):
125142
return json_output
126143

127144

128-
def get_trace(input_code, output_trace):
129-
return output_trace
130-
131145

132146
# called right before a statement gets executed
133147
def opt_pre_run_code_hook(self):
@@ -140,9 +154,8 @@ def opt_pre_run_code_hook(self):
140154
filtered_ns[k] = v
141155

142156
last_cmd = self.history_manager.input_hist_parsed[-1]
143-
#print 'last_cmd:', last_cmd
144157
trace_json = self.meta.opt_history.run_str(last_cmd, filtered_ns)
145-
print trace_json
158+
#print trace_json
146159
urllib2.urlopen("http://localhost:8888/post", trace_json)
147160

148161

v3/pg_logger.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@
7575
resource_module_loaded = False
7676

7777

78+
# From http://coreygoldberg.blogspot.com/2009/05/python-redirect-or-turn-off-stdout-and.html
79+
class NullDevice():
80+
def write(self, s):
81+
pass
82+
7883

7984
# These could lead to XSS or other code injection attacks, so be careful:
8085
__html__ = None
@@ -1023,6 +1028,10 @@ def _runscript(self, script_str, custom_globals=None):
10231028
user_stdout = cStringIO.StringIO()
10241029

10251030
sys.stdout = user_stdout
1031+
1032+
self.ORIGINAL_STDERR = sys.stderr
1033+
sys.stderr = NullDevice # silence errors
1034+
10261035
user_globals = {"__name__" : "__main__",
10271036
"__builtins__" : user_builtins,
10281037
"__user_stdout__" : user_stdout}
@@ -1106,6 +1115,7 @@ def force_terminate(self):
11061115

11071116
def finalize(self):
11081117
sys.stdout = self.GAE_STDOUT # very important!
1118+
sys.stderr = self.ORIGINAL_STDERR
11091119

11101120
assert len(self.trace) <= (MAX_EXECUTED_LINES + 1)
11111121

0 commit comments

Comments
 (0)