3535# TODO: support incremental pushes to the OPT frontend for efficiency
3636# and better "snappiness"
3737
38-
3938# TODO: support line number adjustments for function definitions
4039
40+
4141class OptHistory (object ):
4242 def __init__ (self ):
4343 self .executed_stmts = []
4444
45+ # first line number of each line in self.executed_stmts
46+ self .first_lineno = []
47+
4548 # each element is a LIST containing an OPT trace
4649 self .output_traces = []
4750
4851 # was the last executed stmt an exception?
4952 self .last_exec_is_exception = False
5053
54+
5155 def pop_last (self ):
5256 self .executed_stmts .pop ()
57+ self .first_lineno .pop ()
5358 self .output_traces .pop ()
5459
55- def run_str (self , cmd_string , user_globals ):
56- opt_trace = pg_logger .exec_str_with_user_ns (cmd_string , user_globals , get_trace )
60+
61+ def check_rep (self ):
62+ assert len (self .executed_stmts ) == len (self .first_lineno ) == len (self .output_traces )
63+
64+
65+ def get_code (self ):
66+ return '\n ' .join (self .executed_stmts )
67+
68+ def get_trace (self ):
69+ ret = []
70+ for t in self .output_traces :
71+ for e in t :
72+ ret .append (e )
73+ return ret
74+
75+
76+ def run_str (self , stmt_str , user_globals ):
77+ opt_trace = pg_logger .exec_str_with_user_ns (stmt_str , user_globals , get_trace )
78+
79+ # 'clean up' the trace a bit:
80+ if len (self .output_traces ):
81+ # lop off the last element of the previous entry since it should match
82+ # the first element of opt_trace
83+ end_of_last_trace = self .output_traces [- 1 ].pop ()
84+ #print 'END:', end_of_last_trace
85+ #print 'CUR:', opt_trace[0]
5786
5887 # clobber the last entry
5988 if self .last_exec_is_exception :
@@ -67,18 +96,29 @@ def run_str(self, cmd_string, user_globals):
6796 assert last_evt == 'return'
6897 self .last_exec_is_exception = False
6998
70- pp .pprint (opt_trace )
71-
99+ if len (self .executed_stmts ):
100+ lineno = self .first_lineno [- 1 ] + len (self .executed_stmts [- 1 ].splitlines ())
101+ else :
102+ lineno = 1
72103
73-
74- def get_trace (input_code , output_trace ):
75- return output_trace
104+ # adjust all the line numbers in the trace
105+ for elt in opt_trace :
106+ elt ['line' ] += (lineno - 1 )
107+
108+ self .executed_stmts .append (stmt_str )
109+ self .first_lineno .append (lineno )
110+ self .output_traces .append (opt_trace )
76111
112+ output_dict = dict (code = self .get_code (), trace = self .get_trace ())
113+ json_output = json .dumps (output_dict , indent = INDENT_LEVEL )
77114
78- def custom_json_finalizer (input_code , output_trace ):
79- ret = dict (code = input_code , trace = output_trace )
80- json_output = json .dumps (ret , indent = INDENT_LEVEL )
81- return json_output
115+ print json_output
116+
117+ self .check_rep ()
118+
119+
120+ def get_trace (input_code , output_trace ):
121+ return output_trace
82122
83123
84124# called right before a statement gets executed
0 commit comments