Skip to content

Commit d715cfb

Browse files
committed
added probe_exprs feature to selectively probe values of user-written exprs
1 parent 12ad02c commit d715cfb

3 files changed

Lines changed: 54 additions & 8 deletions

File tree

‎v5-unity/doctest_exec.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- sys.argv[3] - a zero-indexed integer to run the example at a specific index in the list,
77
or 'all' to run all examples for this test (NOT IMPLEMENTED YET)
88
- sys.argv[4] - a string containing the student's code to be run with the test
9+
- sys.argv[5] - [optional] a string representing a JSON list of strings of
10+
expressions whose values should be probed at each execution step
911

1012
Output:
1113

@@ -36,7 +38,7 @@
3638
pp = pprint.PrettyPrinter()
3739

3840

39-
def opt_run_doctest(doctest, example_number, student_code):
41+
def opt_run_doctest(doctest, example_number, student_code, probe_exprs):
4042
def my_finalizer(input_code, output_trace):
4143
ret = dict(doctest=encode_doctest(doctest),
4244
example_number=example_number,
@@ -51,7 +53,8 @@ def opt_doctest_exec_script_str(script_str, custom_modules):
5153
logger = pg_logger.PGLogger(False, False, False, my_finalizer,
5254
disable_security_checks=False,
5355
custom_modules=custom_modules,
54-
separate_stdout_by_module=True)
56+
separate_stdout_by_module=True,
57+
probe_exprs=probe_exprs)
5558
try:
5659
logger._runscript(script_str)
5760
except bdb.BdbQuit:
@@ -79,6 +82,12 @@ def opt_doctest_exec_script_str(script_str, custom_modules):
7982
test_name = sys.argv[2]
8083
example_number = sys.argv[3]
8184
student_code = sys.argv[4]
85+
86+
if len(sys.argv) > 5:
87+
probe_exprs = json.loads(sys.argv[5])
88+
else:
89+
probe_exprs = None
90+
8291
if example_number != 'all':
8392
example_number = int(example_number)
8493

@@ -91,7 +100,7 @@ def opt_doctest_exec_script_str(script_str, custom_modules):
91100
for t in tests:
92101
# run this test!
93102
if t.name == test_name:
94-
opt_run_doctest(t, example_number, student_code)
103+
opt_run_doctest(t, example_number, student_code, probe_exprs)
95104
found = True
96105
break
97106
if not found:

‎v5-unity/generate_json_trace.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,25 @@ def js_var_finalizer(input_code, output_trace):
3737
help="Create a JavaScript variable out of the trace")
3838
parser.add_option("--code", dest="usercode", default=None,
3939
help="Load user code from a string instead of a file and output compact JSON")
40+
parser.add_option("--probe-exprs", dest="probe_exprs_json", default=None,
41+
help="A JSON list of strings representing expressions whose values to probe at each step (advanced)")
4042

4143
(options, args) = parser.parse_args()
4244
INDENT_LEVEL = None if options.compact else 2
4345

4446
if options.usercode:
4547
INDENT_LEVEL = None
48+
49+
probe_exprs = None
50+
if options.probe_exprs_json:
51+
probe_exprs = json.loads(options.probe_exprs_json)
52+
4653
print(pg_logger.exec_script_str_local(options.usercode,
4754
options.raw_input_lst_json,
4855
options.cumulative,
4956
options.heapPrimitives,
50-
json_finalizer))
57+
json_finalizer,
58+
probe_exprs=probe_exprs))
5159
else:
5260
fin = sys.stdin if args[0] == "-" else open(args[0])
5361
if options.js_varname:

‎v5-unity/pg_logger.py‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,16 @@ class PGLogger(bdb.Bdb):
445445
# for each module rather than all stdout going to a single stream
446446
def __init__(self, cumulative_mode, heap_primitives, show_only_outputs, finalizer_func,
447447
disable_security_checks=False, crazy_mode=False,
448-
custom_modules=None, separate_stdout_by_module=False):
448+
custom_modules=None, separate_stdout_by_module=False, probe_exprs=None):
449449
bdb.Bdb.__init__(self)
450450
self.mainpyfile = ''
451451
self._wait_for_mainpyfile = 0
452452

453+
if probe_exprs:
454+
self.probe_exprs = probe_exprs
455+
else:
456+
self.probe_exprs = None
457+
453458
self.separate_stdout_by_module = separate_stdout_by_module
454459
self.stdout_by_module = {} # Key: module name, Value: StringIO faux-stdout
455460

@@ -1036,6 +1041,7 @@ def create_encoded_stack_entry(cur_frame):
10361041

10371042

10381043
# climb up until you find '<module>', which is (hopefully) the global scope
1044+
top_frame = None
10391045
while True:
10401046
cur_frame = self.stack[i][0]
10411047
cur_name = cur_frame.f_code.co_name
@@ -1061,6 +1067,8 @@ def create_encoded_stack_entry(cur_frame):
10611067
# stack to render should only be [foo, baz].
10621068
if cur_frame in self.frame_ordered_ids:
10631069
encoded_stack_locals.append(create_encoded_stack_entry(cur_frame))
1070+
if not top_frame:
1071+
top_frame = cur_frame
10641072
i -= 1
10651073

10661074
zombie_encoded_stack_locals = [create_encoded_stack_entry(e) for e in zombie_frames_to_render]
@@ -1069,7 +1077,8 @@ def create_encoded_stack_entry(cur_frame):
10691077
# encode in a JSON-friendly format now, in order to prevent ill
10701078
# effects of aliasing later down the line ...
10711079
encoded_globals = {}
1072-
for (k, v) in get_user_globals(tos[0], at_global_scope=(self.curindex <= 1)).items():
1080+
cur_globals_dict = get_user_globals(tos[0], at_global_scope=(self.curindex <= 1))
1081+
for (k, v) in cur_globals_dict.items():
10731082
encoded_val = self.encoder.encode(v, self.get_parent_of_function)
10741083
encoded_globals[k] = encoded_val
10751084

@@ -1143,6 +1152,21 @@ def create_encoded_stack_entry(cur_frame):
11431152
e['unique_hash'] = hash_str
11441153

11451154

1155+
# handle probe_exprs *before* encoding the heap with self.encoder.get_heap
1156+
encoded_probe_vals = {}
1157+
if self.probe_exprs:
1158+
if top_frame: # are we in a function call?
1159+
top_frame_locals = get_user_locals(top_frame)
1160+
else:
1161+
top_frame_locals = {}
1162+
for e in self.probe_exprs:
1163+
try:
1164+
# evaluate it with globals + locals of the top frame ...
1165+
probe_val = eval(e, cur_globals_dict, top_frame_locals)
1166+
encoded_probe_vals[e] = self.encoder.encode(probe_val, self.get_parent_of_function)
1167+
except:
1168+
pass # don't encode the value if there's been an error
1169+
11461170
if self.show_only_outputs:
11471171
trace_entry = dict(line=lineno,
11481172
event=event_type,
@@ -1161,6 +1185,8 @@ def create_encoded_stack_entry(cur_frame):
11611185
stack_to_render=stack_to_render,
11621186
heap=self.encoder.get_heap(),
11631187
stdout=self.get_user_stdout())
1188+
if encoded_probe_vals:
1189+
trace_entry['probe_exprs'] = encoded_probe_vals
11641190

11651191
# optional column numbers for greater precision
11661192
# (only relevant in Py2crazy, a hacked CPython that supports column numbers)
@@ -1524,9 +1550,12 @@ def exec_script_str(script_str, raw_input_lst_json, options_json, finalizer_func
15241550
# disables security check and returns the result of finalizer_func
15251551
# WARNING: ONLY RUN THIS LOCALLY and never over the web, since
15261552
# security checks are disabled
1527-
def exec_script_str_local(script_str, raw_input_lst_json, cumulative_mode, heap_primitives, finalizer_func):
1553+
#
1554+
# [optional] probe_exprs is a list of strings representing
1555+
# expressions whose values to probe at each step (advanced)
1556+
def exec_script_str_local(script_str, raw_input_lst_json, cumulative_mode, heap_primitives, finalizer_func, probe_exprs=None):
15281557
# TODO: add py_crazy_mode option here too ...
1529-
logger = PGLogger(cumulative_mode, heap_primitives, False, finalizer_func, disable_security_checks=True)
1558+
logger = PGLogger(cumulative_mode, heap_primitives, False, finalizer_func, disable_security_checks=True, probe_exprs=probe_exprs)
15301559

15311560
# TODO: refactor these NOT to be globals
15321561
global input_string_queue

0 commit comments

Comments
 (0)