Skip to content

Commit b498169

Browse files
author
Philip Guo
committed
added a exec_script_str_local that should only be used when running locally,
which disables security checks and returns the result of the finalizer function
1 parent 9f53931 commit b498169

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

‎v3/generate_json_trace.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
def json_finalizer(input_code, output_trace):
1414
ret = dict(code=input_code, trace=output_trace)
1515
json_output = json.dumps(ret, indent=INDENT_LEVEL)
16-
print(json_output)
16+
return json_output
1717

1818
def js_var_finalizer(input_code, output_trace):
1919
global JS_VARNAME
2020
ret = dict(code=input_code, trace=output_trace)
2121
json_output = json.dumps(ret, indent=None)
22-
print("var %s = %s;" % (JS_VARNAME, json_output))
22+
return "var %s = %s;" % (JS_VARNAME, json_output)
2323

2424
parser = OptionParser(usage="Generate JSON trace for pytutor")
2525
parser.add_option('-c', '--cumulative', default=False, action='store_true',
@@ -35,6 +35,6 @@ def js_var_finalizer(input_code, output_trace):
3535

3636
if options.js_varname:
3737
JS_VARNAME = options.js_varname
38-
pg_logger.exec_script_str(fin.read(), options.cumulative, js_var_finalizer)
38+
print(pg_logger.exec_script_str_local(fin.read(), options.cumulative, js_var_finalizer))
3939
else:
40-
pg_logger.exec_script_str(fin.read(), options.cumulative, json_finalizer)
40+
print(pg_logger.exec_script_str_local(fin.read(), options.cumulative, json_finalizer))

‎v3/pg_logger.py‎

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

189189
class PGLogger(bdb.Bdb):
190190

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

196+
self.disable_security_checks = disable_security_checks
197+
196198
# a function that takes the output trace as a parameter and
197199
# processes it
198200
self.finalizer_func = finalizer_func
@@ -746,7 +748,7 @@ def _runscript(self, script_str):
746748
# memory bombs such as:
747749
# x = 2
748750
# while True: x = x*x
749-
if resource_module_loaded:
751+
if resource_module_loaded and (not self.disable_security_checks):
750752
resource.setrlimit(resource.RLIMIT_AS, (200000000, 200000000))
751753
resource.setrlimit(resource.RLIMIT_CPU, (5, 5))
752754

@@ -839,7 +841,7 @@ def finalize(self):
839841

840842
self.trace = res
841843

842-
self.finalizer_func(self.executed_script, self.trace)
844+
return self.finalizer_func(self.executed_script, self.trace)
843845

844846

845847

@@ -854,3 +856,17 @@ def exec_script_str(script_str, cumulative_mode, finalizer_func):
854856
finally:
855857
logger.finalize()
856858

859+
860+
# disables security check and returns the result of finalizer_func
861+
# WARNING: ONLY RUN THIS LOCALLY and never over the web, since
862+
# 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)
865+
866+
try:
867+
logger._runscript(script_str)
868+
except bdb.BdbQuit:
869+
pass
870+
finally:
871+
return logger.finalize()
872+

0 commit comments

Comments
 (0)