def run(self): self.JS_VARNAME = "" self.JS_VARVAL = "" def raw_dict(input_code, output_trace): ret = dict(code=input_code, trace=output_trace) return ret def js_var_finalizer(input_code, output_trace): global JS_VARNAME ret = dict(code=input_code, trace=output_trace) json_output = json.dumps(ret, indent=None) return "var %s = %s;" % (self.JS_VARNAME, json_output) self.options['divid'] = self.arguments[0] if self.content: source = "\n".join(self.content) else: source = '\n' CUMULATIVE_MODE=False self.JS_VARNAME = self.options['divid']+'_trace' if 'showoutput' not in self.options: self.options['embedded'] = 'true' # to set embeddedmode to true else: self.options['embedded'] = 'false' if 'question' in self.options: curTrace = exec_script_str_local(source, None, CUMULATIVE_MODE, None, raw_dict) self.inject_questions(curTrace) json_output = json.dumps(curTrace, indent=None) self.options['tracedata'] = "var %s = %s;" % (self.JS_VARNAME, json_output) else: self.options['tracedata'] = exec_script_str_local(source, None, CUMULATIVE_MODE, None, js_var_finalizer) res = VIS if 'caption' not in self.options: self.options['caption'] = '' if 'question' in self.options: res += QUESTION if 'tracedata' in self.options: res += DATA return [nodes.raw('',res % self.options,format='html')]
def run_str_and_broadcast(self, stmt_str): ''' Run stmt_str and transmit trace to server ''' self.executed_stmts.append(stmt_str) opt_trace = pg_logger.exec_script_str_local(self.get_code(), [], False, False, lambda cod, trace: trace) last_evt = opt_trace[-1]['event'] if last_evt == 'exception': epic_fail = True else: assert last_evt == 'return' epic_fail = False trace_dict = dict(code=self.get_code(), trace=opt_trace) json_output = json.dumps(trace_dict, indent=INDENT_LEVEL) # if this statement ended in an exception, delete it from the # history and pretend it never happened if epic_fail: self.pop_last() urlopen(SERVER_ADDR + 'wholetrace', json_output.encode())
def get_py_exec(): out_s = StringIO.StringIO() def json_finalizer(input_code, output_trace): ret = dict(code=input_code, trace=output_trace) json_output = json.dumps(ret, indent=None) out_s.write(json_output) options = json.loads(request.query.options_json) pg_logger.exec_script_str_local(request.query.user_script, request.query.raw_input_json, options['cumulative_mode'], options['heap_primitives'], json_finalizer) return out_s.getvalue()
def run(self): self.JS_VARNAME = "" self.JS_VARVAL = "" def js_var_finalizer(input_code, output_trace): global JS_VARNAME ret = dict(code=input_code, trace=output_trace) json_output = json.dumps(ret, indent=None) return "var %s = %s;" % (self.JS_VARNAME, json_output) self.options["divid"] = self.arguments[0] if self.content: source = "\n".join(self.content) else: source = "\n" CUMULATIVE_MODE = False self.JS_VARNAME = self.options["divid"] + "_trace" if "showoutput" not in self.options: self.options["embedded"] = "true" # to set embeddedmode to true else: self.options["embedded"] = "false" self.options["tracedata"] = exec_script_str_local(source, CUMULATIVE_MODE, js_var_finalizer) res = VIS if "caption" not in self.options: self.options["caption"] = "" if "tracedata" in self.options: res += DATA return [nodes.raw("", res % self.options, format="html")]
def get_exec(): out_s = StringIO.StringIO() def json_finalizer(input_code, output_trace): ret = dict(code=input_code, trace=output_trace) json_output = json.dumps(ret, indent=None) out_s.write(json_output) options = json.loads(request.query.options_json) pg_logger.exec_script_str_local(request.query.user_script, request.query.raw_input_json, options['cumulative_mode'], options['heap_primitives'], json_finalizer) return out_s.getvalue()
def trace(source, ri): def finalizer(input_code, output_trace): filtered_trace = [ep for ep in output_trace if ep['event'] not in ignored_events] return filtered_trace return pg_logger.exec_script_str_local(source, ri, True, True, finalizer)
def command_exec_script(self): raw_input_lst_json = False heap_primitives = False cumulative_mode = read_int(self._conn) if cumulative_mode == 0: cumulative_mode = False else: cumulative_mode = True allow_all_modules = read_int(self._conn) if allow_all_modules == 0: allow_all_modules = False else: allow_all_modules = True max_executed_lines = read_int(self._conn) folder = read_string(self._conn) sys.path[0] = folder resource = read_string(self._conn) script_str = read_string(self._conn) trace_str = pg_logger.exec_script_str_local(script_str, raw_input_lst_json, cumulative_mode, heap_primitives, max_executed_lines, json_finalizer, probe_exprs=None, allow_all_modules=allow_all_modules) write_bytes(self._conn, OUTP) write_string(self._conn, resource) write_string(self._conn, trace_str)
def run_str(self, stmt_str): self.executed_stmts.append(stmt_str) opt_trace = pg_logger.exec_script_str_local(self.get_code(), [], False, False, lambda cod, trace: trace) last_evt = opt_trace[-1]['event'] if last_evt == 'exception': epic_fail = True else: assert last_evt == 'return' epic_fail = False output_dict = dict(code=self.get_code(), trace=opt_trace) json_output = json.dumps(output_dict, indent=INDENT_LEVEL) # if this statement ended in an exception, delete it from the # history and pretend it never happened if epic_fail: self.pop_last() self.check_rep() return json_output
help='output compact trace.') parser.add_option('-i', '--input', default=False, action='store', help='JSON list of strings for simulated raw_input.', dest='raw_input_lst_json') parser.add_option("--create_jsvar", dest="js_varname", default=None, help="Create a JavaScript variable out of the trace") (options, args) = parser.parse_args() INDENT_LEVEL = None if options.compact else 2 fin = sys.stdin if args[0] == "-" else open(args[0]) if options.js_varname: JS_VARNAME = options.js_varname print( pg_logger.exec_script_str_local(fin.read(), options.raw_input_lst_json, options.cumulative, options.heapPrimitives, js_var_finalizer)) else: print( pg_logger.exec_script_str_local(fin.read(), options.raw_input_lst_json, options.cumulative, options.heapPrimitives, json_finalizer))
help="Create a JavaScript variable out of the trace") parser.add_option("--code", dest="usercode", default=None, help="Load user code from a string instead of a file and output compact JSON") parser.add_option("--probe-exprs", dest="probe_exprs_json", default=None, help="A JSON list of strings representing expressions whose values to probe at each step (advanced)") (options, args) = parser.parse_args() INDENT_LEVEL = None if options.compact else 2 # 模拟用户输入代码 options.usercode = "a = 1\nb = 2\nc = a\nd=[1,2,3,4,5]" if options.usercode: INDENT_LEVEL = None probe_exprs = None if options.probe_exprs_json: probe_exprs = json.loads(options.probe_exprs_json) allow_all_modules = False if options.allmodules: allow_all_modules = True print(pg_logger.exec_script_str_local(options.usercode, options.raw_input_lst_json, options.cumulative, options.heapPrimitives, json_finalizer, probe_exprs=probe_exprs, allow_all_modules=allow_all_modules))
def json_finalizer(input_code, output_trace): ret = dict(code=input_code, trace=output_trace) json_output = json.dumps(ret, indent=INDENT_LEVEL) return json_output def js_var_finalizer(input_code, output_trace): global JS_VARNAME ret = dict(code=input_code, trace=output_trace) json_output = json.dumps(ret, indent=None) return "var %s = %s;" % (JS_VARNAME, json_output) parser = OptionParser(usage="Generate JSON trace for pytutor") parser.add_option('-c', '--cumulative', default=False, action='store_true', help='output cumulative trace.') parser.add_option('-p', '--heapPrimitives', default=False, action='store_true', help='render primitives as heap objects.') parser.add_option('-o', '--compact', default=False, action='store_true', help='output compact trace.') parser.add_option("--create_jsvar", dest="js_varname", help="Create a JavaScript variable out of the trace") (options, args) = parser.parse_args() INDENT_LEVEL = None if options.compact else 2 fin = sys.stdin if args[0] == "-" else open(args[0]) if options.js_varname: JS_VARNAME = options.js_varname print(pg_logger.exec_script_str_local(fin.read(), options.cumulative, options.heapPrimitives, js_var_finalizer)) else: print(pg_logger.exec_script_str_local(fin.read(), options.cumulative, options.heapPrimitives, json_finalizer))
parser.add_option('-c', '--cumulative', default=False, action='store_true', help='output cumulative trace.') parser.add_option('-p', '--heapPrimitives', default=False, action='store_true', help='render primitives as heap objects.') parser.add_option('-o', '--compact', default=False, action='store_true', help='output compact trace.') parser.add_option('-i', '--input', default=False, action='store', help='JSON list of strings for simulated raw_input.', dest='raw_input_lst_json') parser.add_option("--create_jsvar", dest="js_varname", default=None, help="Create a JavaScript variable out of the trace") parser.add_option("--code", dest="usercode", default=None, help="Load user code from a string instead of a file and output compact JSON") (options, args) = parser.parse_args() INDENT_LEVEL = None if options.compact else 2 if options.usercode: INDENT_LEVEL = None print(pg_logger.exec_script_str_local(options.usercode, options.raw_input_lst_json, options.cumulative, options.heapPrimitives, json_finalizer)) else: fin = sys.stdin if args[0] == "-" else open(args[0]) if options.js_varname: JS_VARNAME = options.js_varname print(pg_logger.exec_script_str_local(fin.read(), options.raw_input_lst_json, options.cumulative, options.heapPrimitives, js_var_finalizer)) else: print(pg_logger.exec_script_str_local(fin.read(), options.raw_input_lst_json, options.cumulative, options.heapPrimitives, json_finalizer))
def get_json_trace(filename): pythonfile = open(filename) return pg_logger.exec_script_str_local(pythonfile.read(), False, False, False, json_finalizer)