def wrapper(*args, **kwargs): # create a global variable to store the result within. Make sure it is unlikely to already exist exec_code, var_name = append_fn_call(source, fn) tracer = CodeTracer() tracer.max_width = max_width local_context_copy = dict(locals()) trace = tracer.trace_code(exec_code, global_context=fn.__globals__, local_context=local_context_copy, dump=dump) trace = remove_fn_call(trace) # add back in the preamble if dumping code also if dump: trace = preamble + "\n" + trace print("*** Trace Output *** ".ljust(80, "-")) print(trace) print("*** END Trace Output *** ".ljust(80, "-")) # get return val ret_val = None if var_name in local_context_copy: ret_val = local_context_copy[var_name] return ret_val
def parsecode(): try: code = request.form["code"] if not code or len(code.strip()) == 0: return jsonify({"error": "code is null or empty"}) tracer = CodeTracer() tracer.max_width = 200000 code_report = tracer.trace_code(code, dump=False) return jsonify({"code_report": code_report}) except Exception as e: exc = format_exc() print(exc) return (jsonify( {"error": "an exception occurred: {exc}".format(exc=exc)}), 500)
def test_infinite_loop_by_width(self): # SETUP code = """\ n = 0 while True: n += 1 """ expected_report = """\ n = 0 | | n = 1 | n = 2 | RuntimeError: live coding message limit exceeded """ tracer = CodeTracer() tracer.max_width = 20 # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_infinite_recursion_by_width(self): # SETUP code = """\ def foo(n): foo(n+1) foo(0) """ expected_report = """\ n = 0 | n = 1 | n = 2 | RuntimeError: live coding message limit exceeded | RuntimeError: live coding message limit exceeded | RuntimeError: live coding message limit exceeded | RuntimeError: live coding message limit exceeded """ tracer = CodeTracer() tracer.max_width = 20 # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def test_infinite_recursion_by_width(self): # SETUP code = """\ def foo(n): foo(n+1) foo(0) """ expected_report = """\ n = 0 | n = 1 | RuntimeError: live coding message limit exceeded | RuntimeError: live coding message limit exceeded | RuntimeError: live coding message limit exceeded """ tracer = CodeTracer() tracer.max_width = 13 # EXEC report = tracer.trace_code(code) # VERIFY self.assertReportEqual(expected_report, report)
def trace(self, line, cell): source = "i = 0\ni+=1\n" ipython = get_ipython() dump = True # Set this to None (or don't set) when logging the code rather than printing it max_width = 20000 tracer = CodeTracer() tracer.max_width = max_width trace = tracer.trace_code(source, global_context=ipython.user_ns, local_context=dict(), dump=dump) # add back in the preamble if dumping code also ipython.write("*** Trace Output *** ".ljust(80, "-")) ipython.write(trace) ipython.write("*** END Trace Output *** ".ljust(80, "-")) return line, cell