def run(self, verbose=False): try: with common.ListStream() as x: code = compile(self.code, filename="<script>", mode="exec") exec(code, {}) except SystemExit: pass return x.singlestring()
def result_from_script(source): source_lines = open(source).read() code = compile(source_lines, '<string>', 'exec') try: with common.ListStream() as x: exec(code, {}) except SystemExit: pass return x.singlestring()
def run(self, verbose=False): try: with common.ListStream() as x: code = compile(self.tree, filename="<ast>", mode="exec") # giving a new namespace is necessary to avoid exec interfering # with current context (x variable from with construct) # giving global and local namespace (or equivalently only global # namespace) is necessary to handle recursive function definitions # see https://stackoverflow.com/questions/871887/using-exec-with-recursive-functions exec(code, {}) except SystemExit: pass return x.singlestring()
def disassemble(tree): code = compile(tree, '<ast>', "exec") with common.ListStream() as x: if tuple(sys.version_info)[:2] <= (3, 6): dis.dis(code) else: dis.dis(code, depth=0) for oparg in code.co_consts: if isinstance(oparg, types.CodeType): func_code = oparg func_name = func_code.co_name func_args = func_code.co_varnames[:func_code.co_argcount] print('\n%12s%-29s %s %s' % ('', '-1 FUNCTION', make_function_label(func_name), ' '.join(func_args))) dis.disassemble(func_code) code = x.stringlist() # return list of instructions return code
def trace(self): print(ast.dump(self.tree)) with common.ListStream() as x: pprint_ast(self.tree) return x.singlestring()
def run(self, verbose=False): with common.ListStream() as x: interpreter(self.opcode, coverage=False) return x.singlestring()