Skip to content

Commit 7efb588

Browse files
author
Philip Guo
committed
rudimentary heap primitives working
1 parent 1d127f9 commit 7efb588

7 files changed

Lines changed: 46 additions & 18 deletions

File tree

‎v3/generate_json_trace.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def js_var_finalizer(input_code, output_trace):
2424
parser = OptionParser(usage="Generate JSON trace for pytutor")
2525
parser.add_option('-c', '--cumulative', default=False, action='store_true',
2626
help='output cumulative trace.')
27+
parser.add_option('-p', '--heapPrimitives', default=False, action='store_true',
28+
help='render primitives as heap objects.')
2729
parser.add_option('-o', '--compact', default=False, action='store_true',
2830
help='output compact trace.')
2931
parser.add_option("--create_jsvar", dest="js_varname",
@@ -35,6 +37,6 @@ def js_var_finalizer(input_code, output_trace):
3537

3638
if options.js_varname:
3739
JS_VARNAME = options.js_varname
38-
print(pg_logger.exec_script_str_local(fin.read(), options.cumulative, js_var_finalizer))
40+
print(pg_logger.exec_script_str_local(fin.read(), options.cumulative, options.heapPrimitives, js_var_finalizer))
3941
else:
40-
print(pg_logger.exec_script_str_local(fin.read(), options.cumulative, json_finalizer))
42+
print(pg_logger.exec_script_str_local(fin.read(), options.cumulative, options.heapPrimitives, json_finalizer))

‎v3/js/opt-frontend.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ $(document).ready(function() {
148148

149149
$.get(backend_script,
150150
{user_script : pyInputCodeMirror.getValue(),
151-
cumulative_mode: $('#cumulativeModeSelector').val()},
151+
cumulative_mode: $('#cumulativeModeSelector').val(),
152+
heap_primitives: $('#heapPrimitivesSelector').val()},
152153
function(dataFromBackend) {
153154
var trace = dataFromBackend.trace;
154155

‎v3/js/pytutor.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ ExecutionVisualizer.prototype.precomputeCurTraceLayouts = function() {
13971397

13981398
var myViz = this; // to prevent confusion of 'this' inside of nested functions
13991399

1400-
1400+
14011401
$.each(this.curTrace, function(i, curEntry) {
14021402
var prevLayout = myViz.curTraceLayouts[myViz.curTraceLayouts.length - 1];
14031403

@@ -2031,6 +2031,12 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
20312031
d3DomElement.append('<div class="funcObj">function ' + funcName + '</div>');
20322032
}
20332033
}
2034+
else if (obj[0] == 'HEAP_PRIMITIVE') {
2035+
assert(obj.length == 2);
2036+
2037+
var primitiveVal = obj[1];
2038+
renderPrimitiveObject(primitiveVal, d3DomElement);
2039+
}
20342040
else {
20352041
// render custom data type
20362042
assert(obj.length == 2);

‎v3/pg_encoder.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
# * None, int, long, float, str, bool - unchanged
3838
# (json.dumps encodes these fine verbatim)
3939
#
40+
# If render_heap_primitives is True, then primitive values are rendered
41+
# on the heap as ['HEAP_PRIMITIVE', <value>]
42+
#
4043
# Compound objects:
4144
# * list - ['LIST', elt1, elt2, elt3, ..., eltN]
4245
# * tuple - ['TUPLE', elt1, elt2, elt3, ..., eltN]
@@ -90,15 +93,19 @@ def get_name(obj):
9093
return obj.__name__ if hasattr(obj, '__name__') else get_name(type(obj))
9194

9295

96+
PRIMITIVE_TYPES = (int, long, float, str, bool, type(None))
97+
9398
# Note that this might BLOAT MEMORY CONSUMPTION since we're holding on
9499
# to every reference ever created by the program without ever releasing
95100
# anything!
96101
class ObjectEncoder:
97-
def __init__(self):
102+
def __init__(self, render_heap_primitives):
98103
# Key: canonicalized small ID
99104
# Value: encoded (compound) heap object
100105
self.encoded_heap_objects = {}
101106

107+
self.render_heap_primitives = render_heap_primitives
108+
102109
self.id_to_small_IDs = {}
103110
self.cur_small_ID = 1
104111

@@ -125,7 +132,7 @@ def set_function_parent_frame_ID(self, ref_obj, enclosing_frame_id):
125132
def encode(self, dat, get_parent):
126133
"""Encode a data value DAT using the GET_PARENT function for parent ids."""
127134
# primitive type
128-
if type(dat) in (int, long, float, str, bool, type(None)):
135+
if not self.render_heap_primitives and type(dat) in PRIMITIVE_TYPES:
129136
if type(dat) is float:
130137
return round(dat, FLOAT_PRECISION)
131138
else:
@@ -208,6 +215,9 @@ def encode(self, dat, get_parent):
208215
self.encode_class_or_instance(dat, new_obj)
209216
elif typ is types.ModuleType:
210217
new_obj.extend(['module', dat.__name__])
218+
elif typ in PRIMITIVE_TYPES:
219+
assert self.render_heap_primitives
220+
new_obj.extend(['HEAP_PRIMITIVE', dat])
211221
else:
212222
typeStr = str(typ)
213223
m = typeRE.match(typeStr)

‎v3/pg_logger.py‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,22 +188,25 @@ def visit_function_obj(v, ids_seen_set):
188188

189189
class PGLogger(bdb.Bdb):
190190

191-
def __init__(self, cumulative_mode, finalizer_func, disable_security_checks=False):
191+
def __init__(self, cumulative_mode, heap_primitives, finalizer_func, disable_security_checks=False):
192192
bdb.Bdb.__init__(self)
193193
self.mainpyfile = ''
194194
self._wait_for_mainpyfile = 0
195195

196196
self.disable_security_checks = disable_security_checks
197197

198-
# a function that takes the output trace as a parameter and
199-
# processes it
200-
self.finalizer_func = finalizer_func
201-
202198
# if True, then displays ALL stack frames that have ever existed
203199
# rather than only those currently on the stack (and their
204200
# lexical parents)
205201
self.cumulative_mode = cumulative_mode
206202

203+
# if True, then render certain primitive objects as heap objects
204+
self.render_heap_primitives = heap_primitives
205+
206+
# a function that takes the output trace as a parameter and
207+
# processes it
208+
self.finalizer_func = finalizer_func
209+
207210
# each entry contains a dict with the information for a single
208211
# executed line
209212
self.trace = []
@@ -241,7 +244,7 @@ def __init__(self, cumulative_mode, finalizer_func, disable_security_checks=Fals
241244

242245
# very important for this single object to persist throughout
243246
# execution, or else canonical small IDs won't be consistent.
244-
self.encoder = pg_encoder.ObjectEncoder()
247+
self.encoder = pg_encoder.ObjectEncoder(self.render_heap_primitives)
245248

246249
self.executed_script = None # Python script to be executed!
247250

@@ -846,8 +849,8 @@ def finalize(self):
846849

847850

848851
# the MAIN meaty function!!!
849-
def exec_script_str(script_str, cumulative_mode, finalizer_func):
850-
logger = PGLogger(cumulative_mode, finalizer_func)
852+
def exec_script_str(script_str, cumulative_mode, heap_primitives, finalizer_func):
853+
logger = PGLogger(cumulative_mode, heap_primitives, finalizer_func)
851854

852855
try:
853856
logger._runscript(script_str)
@@ -860,8 +863,8 @@ def exec_script_str(script_str, cumulative_mode, finalizer_func):
860863
# disables security check and returns the result of finalizer_func
861864
# WARNING: ONLY RUN THIS LOCALLY and never over the web, since
862865
# security checks are disabled
863-
def exec_script_str_local(script_str, cumulative_mode, finalizer_func):
864-
logger = PGLogger(cumulative_mode, finalizer_func, disable_security_checks=True)
866+
def exec_script_str_local(script_str, cumulative_mode, heap_primitives, finalizer_func):
867+
logger = PGLogger(cumulative_mode, heap_primitives, finalizer_func, disable_security_checks=True)
865868

866869
try:
867870
logger._runscript(script_str)

‎v3/pythontutor.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ def get(self):
7676

7777
# convert from string to a Python boolean ...
7878
cumulative_mode = (self.request.get('cumulative_mode') == 'true')
79-
79+
heap_primitives = (self.request.get('heap_primitives') == 'true')
80+
8081
pg_logger.exec_script_str(self.request.get('user_script'),
8182
cumulative_mode,
83+
heap_primitives,
8284
self.json_finalizer)
8385

8486

‎v3/web_exec.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def cgi_finalizer(input_code, output_trace):
5858

5959

6060
cumulative_mode = False
61+
heap_primitives = False
6162

6263
# If you pass in a filename as an argument, then process script from that file ...
6364
if len(sys.argv) > 1:
@@ -72,5 +73,8 @@ def cgi_finalizer(input_code, output_trace):
7273
if 'cumulative_mode' in form:
7374
# convert from string to a Python boolean ...
7475
cumulative_mode = (form['cumulative_mode'].value == 'true')
76+
if 'heap_primitives' in form:
77+
heap_primitives = (form['heap_primitives'].value == 'true')
7578

76-
pg_logger.exec_script_str(user_script, cumulative_mode, cgi_finalizer)
79+
80+
pg_logger.exec_script_str(user_script, cumulative_mode, heap_primitives, cgi_finalizer)

0 commit comments

Comments
 (0)