def case(code, ctx, debug=False, cpython_compat=True): stmt = parse(code).result code_obj = py_compile(stmt, is_entrypoint=False) if debug: with open('out_yapypy_bc.log', 'w') as yapypy_bc, open('out_yapypy_info.log', 'w') as yapypy_info: dis_code(code_obj, yapypy_bc) show_code(code_obj, yapypy_info) if cpython_compat: code_obj2 = compile(code, "", "exec") with open('out_cpy_bc.log', 'w') as cpy_bc, open('out_cpy_info.log', 'w') as cpy_info: dis_code(code_obj2, cpy_bc) show_code(code_obj2, cpy_info) print('python:') exec(Bytecode.from_code(code_obj2).to_code(), ctx or {}) print('yapypy') exec(Bytecode.from_code(code_obj).to_code(), ctx or {}) else: exec(code_obj, ctx)
def compile_ex_python_from_source(source_code, is_entry_point=True): filename = '<unknown>' result = parse_ext_py(source_code) result.state.filename = filename check_parsing_complete(source_code, result.tokens, result.state) ast = result.result code = py_compile(ast, filename, is_entrypoint=is_entry_point) return code
def test_all(self): for each in filter(lambda p: p[-1].endswith('.py'), yapypy.collect()): filename = each.__str__() if each.parent().exists(): pass else: each.parent().mkdir() with each.open('r') as fr: collector = DocStringsCollector() mod = ast.parse(fr.read()) collector.visit(mod) mod_name, _ = splitext(each.relative()) for idx, [fn_name, lineno, title, prepare_code, test_code] in enumerate(collector.docs): print(f'tests of {mod_name}.{title or fn_name} started...') context = {'self': self} prepare_code = dedent_all(prepare_code) test_code = dedent_all(test_code) fixer = FixLineno(lineno) try: node = ast.parse(prepare_code, filename, mode='exec') fixer.visit(node) code = compile(node, filename, "exec") except SyntaxError as exc: exc.lineno = lineno exc.filename = filename raise exc bc = Bytecode.from_code(code) bc.filename = filename bc.first_lineno = lineno exec(bc.to_code(), context) # not correct but as a workaround fixer = FixLineno(lineno + test_code.count('\n')) try: node = parse(test_code, filename).result # pprint(node) fixer.visit(node) code = py_compile(node, filename, is_entrypoint=True) except SyntaxError as exc: exc.lineno = lineno exc.filename = filename raise exc bc = Bytecode.from_code(code) bc.filename = filename bc.first_lineno = lineno code_obj = bc.to_code() exec(code_obj, context) print(f'tests of {mod_name}.{title or fn_name} passed.')
def compile_ex_python_from_filename(filename, is_entry_point=True): with Path(filename).open('r') as fr: source_code = fr.read() result = parse_ext_py(source_code) result.state.filename = filename check_parsing_complete(source_code, result.tokens, result.state) ast = result.result code = py_compile(ast, filename, is_entrypoint=is_entry_point) return code
def yapypy_debug(code: str, should_exec=False, ctx=None): res = to_tagged_ast(parse(code).result) c = py_compile(res) print("-----------Code") print(code) print("-----------YaPyPy") print(dis.dis(c)) print("-----------YaPyPy exec result") if should_exec: exec(c, ctx or {}) else: print("\t(skip)")
def get_yapypy_module_spec_from_path(names, module_path): with Path(module_path).open('r') as fr: spec = ModuleSpec(names, YAPyPyLoader(names, module_path)) __source__ = fr.read() result = parse(__source__, module_path) # pprint(result.result) check_parsing_complete(__source__, result.tokens, result.state) __bytecode__ = py_compile(result.result, filename=module_path, is_entrypoint=False) spec.__source__ = __source__ spec.__bytecode__ = __bytecode__ return spec
def easy_debug(code: str, should_exec=False, ctx=None): res = to_tagged_ast(parse(code).result) c = py_compile(res) print("-----------code") print(code) print("-----------Python") print(dis.dis(code)) print("-----------YaPyPy") print(dis.dis(c)) print("-----------astpretty") astpretty.pprint(ast.parse(code)) print("----------- Python exec result") exec(code, ctx or {}) print("-----------YaPyPy exec result") if should_exec: exec(c, ctx or {}) else: print("\t(skip)")
def yapypy_test_code(code: str, should_exec=False, ctx=None): res = to_tagged_ast(parse(code).result) c = py_compile(res) if should_exec: exec(c, ctx or {})