def test_import_multi(self): src = 'import mod1, mod2 as x, mod3' expect_src = '_LX_import_module("mod1")\n_LX_import_module("mod2", asname="x")\n_LX_import_module("mod3")' tree = ast.parse(src) tree = TransformImportsAst().visit(tree) expect_tree = ast.parse(expect_src) self.assertEquals(ast.dump(expect_tree), ast.dump(tree))
def test_import_from_asname(self): src = 'from mymodule import name as alias' expect_src = '_LX_import_module("mymodule", froms=[("name","alias")])' tree = ast.parse(src) tree = TransformImportsAst().visit(tree) expect_tree = ast.parse(expect_src) self.assertEquals(ast.dump(expect_tree), ast.dump(tree))
def test_import_asname(self): src = 'import mymodule as thatmodule' expect_src = '_LX_import_module("mymodule", asname="thatmodule")' tree = ast.parse(src) tree = TransformImportsAst().visit(tree) expect_tree = ast.parse(expect_src) self.assertEquals(ast.dump(expect_tree), ast.dump(tree))
def test_import_dotted(self): src = 'import package.mymodule' expect_src = '_LX_import_module("package.mymodule")' tree = ast.parse(src) tree = TransformImportsAst().visit(tree) expect_tree = ast.parse(expect_src) self.assertEquals(ast.dump(expect_tree), ast.dump(tree))
def main(): logging.basicConfig(level=logging.INFO) argument_parser = argparse.ArgumentParser( description='Translate PHP code from stdin to Python on stdout', ) argument_parser.add_argument('--php-ast', action='store_true', help='Dump PHP AST instead of translating to Python') argument_parser.add_argument('--python-ast', action='store_true', help='Dump Python AST instead of code') argument_parser.add_argument('--input-file', help='Read the given file instead of stdin') command_line_args = argument_parser.parse_args() if command_line_args.input_file: input_stream = open(command_line_args.input_file) else: input_stream = sys.stdin parser = XmlPhpParseTreeReader() statements = parser.parse_php(input_stream) input_stream.close() if command_line_args.php_ast: formatter = PhpAstPrettyFormatter() print formatter.pretty_format(statements) return translator = Translator() translated_statements = translator.translate_statements(statements) module = ast.Module(body=translated_statements) if command_line_args.python_ast: print ast.dump(module) else: unparse.Unparser(module)
def _diff_ast(self, code_ast): # Check if the code structure has changed and update # the internal variables accordingly diff_node_index = None try: nodes = itertools.izip_longest(code_ast.body, self._code_ast.body) except AttributeError: diff_node_index = -1 else: for i, node in enumerate(nodes): if (node[0] and not node[1] or not node[0] and node[1] or ast.dump(node[0]) != ast.dump(node[1])): diff_node_index = i break if diff_node_index is not None: self._code_ast = code_ast try: self._body_len = len(code_ast.body) except AttributeError: self._body_len = -1 if diff_node_index < len(self._compiled_cache): self._compiled_cache = self._compiled_cache[:diff_node_index] if diff_node_index < self._next_node_index: self._reset_execution()
def wrap(func): env=Env() source=get_func_source(func) print source argnames=get_args(func) print argnames args={arg_name:arg_type for arg_name,arg_type in zip(argnames, argtypes)} node=ast.parse(source) env.node=node InsertPass(env).run() InsertReturn(env).run() print ast.dump(node) myControlFlowAnalysis(env).run() clearUnreachedNode(env).run() TypeInfer(env, args, func.func_globals).run() print env.cf.blocks InsertCoerceNode(env).run() CoerceReturn(env).run() print ast.dump(node) NameRewrite(env).run() SubscriptRewrite(env).run() InsertArrayInfo(env).run() InsertDefination(env).run() print map_name_types from astunparse import Unparser from cStringIO import StringIO buf=StringIO() Unparser(node,buf) print buf.getvalue() print get_return_type(env.return_node_infos)
def test_import_from_multi(self): src = 'from mymodule import name1, name2 as x, name3' expect_src = '_LX_import_module("mymodule", froms=[("name1",None),("name2","x"),("name3",None),])' tree = ast.parse(src) tree = TransformImportsAst().visit(tree) expect_tree = ast.parse(expect_src) self.assertEquals(ast.dump(expect_tree), ast.dump(tree))
def prepare(self, node, ctx): self.env = {'__builtin__': __import__('__builtin__')} for module_name in modules: not_builtin = ["__builtin__", "__exception__", "__dispatch__", "__iterator__"] # module starting with "__" are pythran internal module and # should not be imported in the Python interpreter if not module_name.startswith('__'): import_name = module_name if module_name == "operator_": import_name = "operator" self.env[module_name] = __import__(import_name) elif module_name not in not_builtin: try: self.env[module_name] = __import__(module_name.strip('_')) except: try: # should try from another package than builtin, # e.g. for ndarray self.env[module_name] = getattr( self.env['__builtin__'], module_name.strip('_')) except: pass try: eval(compile(node, '<constant_folding>', 'exec'), self.env) except Exception as e: print ast.dump(node) print 'error in constant folding: ', e pass super(ConstantFolding, self).prepare(node, ctx)
def validate_ast(py_node, enaml_node, dump_ast=False, offset=0): """Validate each node of an ast against another ast. Typically used to compare an AST generated by the Python paser and one generated by the enaml parser. """ if dump_ast: print('Python node:\n', ast.dump(py_node)) print('Enaml node:\n', ast.dump(enaml_node)) assert type(py_node) == type(enaml_node) if isinstance(py_node, ast.AST): for name, field in ast.iter_fields(py_node): if name == 'ctx': assert type(field) == type(getattr(enaml_node, name)) elif name not in ('lineno', 'col_offset'): field2 = getattr(enaml_node, name, None) print(' '*offset, 'Validating:', name) validate_ast(field, field2, offset=offset+1) elif isinstance(py_node, list): if len(py_node) != len(enaml_node): return False for i, n1 in enumerate(py_node): print(' '*offset, 'Validating', i+1, 'th element') validate_ast(n1, enaml_node[i], offset=offset+1) else: assert py_node == enaml_node
def generic_visit(self, node): if node in self.constant_expressions: try: fake_node = ast.Expression(node.value if isinstance(node, ast.Index) else node) code = compile(fake_node, "<constant folding>", "eval") value = eval(code, self.env) new_node = self.to_ast(value) if isinstance(node, ast.Index) and not isinstance(new_node, ast.Index): new_node = ast.Index(new_node) return new_node except ConstantFolding.ConversionError as e: print ast.dump(node) print "error in constant folding: ", e raise except ConstantFolding.ToNotEval: return Transformation.generic_visit(self, node) except AttributeError as e: # FIXME union_ function is not handle by constant folding if "union_" in e.args[0]: return Transformation.generic_visit(self, node) raise except NameError as e: # FIXME dispatched function are not processed by constant # folding if "__dispatch__" in e.args[0]: return Transformation.generic_visit(self, node) raise else: return Transformation.generic_visit(self, node)
def make_lambda(expression, args, values): def make_arg(name): if sys.version_info >= (3, 0): return ast.arg(arg=name, annotation=None) else: return ast.Name(id=name, ctx=ast.Param(), lineno=1, col_offset=0) lambda_ = ast.Lambda( args=ast.arguments( args=[make_arg(arg) for arg in args + values], varargs=None, varargannotation=None, kwonlyargs=[], kwarg=None, kwargannotation=None, defaults=[ast.Num(i) for i in range(len(values))], kw_defaults=[]), body=expression.body, ) lambda_ = ast.copy_location(lambda_, expression.body) exp = ast.Expression(body=lambda_, lineno=1, col_offset=0) ast.dump(exp) ast.fix_missing_locations(exp) GLOBALS = __GLOBALS.copy() GLOBALS["__builtins__"] = {} return eval(compile(exp, "<lambda>", "eval"), GLOBALS)
def prepare(self, node, ctx): self.env = {'__builtin__': __import__('__builtin__')} class OperatorRenamer(ast.NodeTransformer): def visit_Import(self, node): for n in node.names: if n.name == "operator_": n.name = "operator" return node node = OperatorRenamer().visit(node) for module_name in modules: # module starting with "__" are pythran internal module and # should not be imported in the Python interpreter if not module_name.startswith('__'): if module_name == "operator_": module_name = "operator" # to import the python module # operator instead of trying to import the module # operator_ that does not exist self.env[module_name] = __import__(module_name) try: eval(compile(node, '<constant_folding>', 'exec'), self.env) except Exception as e: print ast.dump(node) print 'error in constant folding: ', e pass super(ConstantFolding, self).prepare(node, ctx)
def visit_If(self, node): if DEBUG: print "-----------start node %s -----------" % node.__class__.__name__ print ast.dump(node) #ast.NodeVisitor.generic_visit(self, node) self.iter_field(node.test) test = self.active.pop() start = self.active.size() self.iter_field(node.body) end = self.active.size() body = JavaStatements() self.fill(body, end-start) start = self.active.size() self.iter_field(node.orelse) end = self.active.size() orelse = None if end-start > 0: orelse = JavaStatements() self.fill(orelse, end-start) java_if = JavaIf(test, body, orelse) java_if.set_metadata(node) self.active.push(java_if) if DEBUG: print "-----------end node %s -----------" % node.__class__.__name__
def my_generic_visit(self, node): if DEBUG: print "-----------node %s -----------" % node.__class__.__name__ print ast.dump(node) #ast.NodeVisitor.generic_visit(self, node) self.visit(node) if DEBUG: print "-----------node-----------"
def visit_FunctionDef(self, node): if DEBUG: print "-----------start node %s -----------" % node.__class__.__name__ print ast.dump(node, True, True) self.function_stack.push(node) start = self.active.size() self.iter_field(node.body) end = self.active.size() body = JavaStatements() self.fill(body, end-start) start = self.active.size() self.iter_field(node.args.args) end = self.active.size() args = JavaList() self.fill(args, end-start) # identify argument types... self.infer_arguments_types(node, args) java_func = JavaFunction(node.name, args, body) if node in self.return_types: return_types = self.return_types[node] else: return_types = None java_func.set_return_type(return_types) java_func.set_metadata(node) self.active.push(java_func) self.function_stack.pop() if DEBUG: print "-----------end node %s -----------" % node.__class__.__name__
def visit_Mod(self, node): if DEBUG: print "-----------start node %s -----------" % node.__class__.__name__ print ast.dump(node) ast.NodeVisitor.generic_visit(self, node) self.active.push(JavaMod()) if DEBUG: print "-----------end node %s -----------" % node.__class__.__name__
def test_function_call(load): load.return_value = mock.sentinel.load node = orchestra.ast_util.function_call( mock.sentinel.name, ast.Num(1), ast.Num(2), a=ast.Num(3), b=ast.Num(4), ) print ast.dump(node) load.assert_called_with(mock.sentinel.name) assert isinstance(node, ast.Call) assert node.func == mock.sentinel.load assert node.args[0].n == 1 assert node.args[1].n == 2 assert isinstance(node.keywords[0], ast.keyword) assert node.keywords[0].arg == 'a' assert node.keywords[0].value.n == 3 assert isinstance(node.keywords[1], ast.keyword) assert node.keywords[1].arg == 'b' assert node.keywords[1].value.n == 4
def test_rewrite_notin_precedence(): code1 = "a and b not in c" code2 = "(a and b) not in c" code3 = "a and (b not in c)" code4 = "(b not in c) and a" rw = query_processor.RewriteChangeNotInPrescedence() tree1 = ast.parse(code1) tree2 = ast.parse(code2) tree3 = ast.parse(code3) tree1_rw = ast.parse(code1) tree2_rw = ast.parse(code2) tree3_rw = ast.parse(code3) rw.visit(tree1_rw) rw.visit(tree2_rw) rw.visit(tree3_rw) assert_not_equal(ast.dump(tree1), ast.dump(tree2)) assert_equal(ast.dump(tree2), ast.dump(tree2_rw)) assert_equal(ast.dump(tree1_rw), ast.dump(tree2)) assert_equal(ast.dump(tree3), ast.dump(tree3_rw)) assert_equal(ast.dump(tree1), ast.dump(tree3_rw))
def check_transform(self, input_, expect): result = self.transform.visit(input_) if expect is None: if result is not None: self.fail('{} is not None'.formatast.dump(result, False)) else: self.assertEqual(ast.dump(result, False), ast.dump(expect, False))
def test_load_simple(): node = orchestra.ast_util.load('var') print ast.dump(node) assert isinstance(node, ast.Name) assert node.id == 'var' assert isinstance(node.ctx, ast.Load)
def main(): #print lib2to3.__dict__ with open("game.py") as source_file: code = source_file.read() ast_node = ast.parse(code) print ast.dump(ast_node) StmtVisitor().visit(ast_node)
def get_changes(self): if not self.text_before: return tree_before = ast.parse(self.text_before) tree = ast.parse(self.text) if ast.dump(tree)==ast.dump(tree_before): print('status','no changes to the script') else: print('status','executing changes to %s'%self.file) # identify changed nodes in the tree and execute # note that this feature reruns any changed child of the script parent #! track line numbers are report to the user? tree_before,tree = [[self.CodeChunk(i,index=ii) for ii,i in enumerate(ast.iter_child_nodes(ast.parse(t)))] for t in [self.text_before,self.text]] intersect = set.intersection(set(tree),set(tree_before)) novel = list(set.difference(set(tree),intersect)) novel_linenos = set([i.this.lineno for i in novel]) class CodeSurgery(ast.NodeTransformer): def visit(self, node): if hasattr(node,'lineno') and node.lineno not in novel_linenos: return ast.parse('last_lineno = %d'%node.lineno).body[0] else: return ast.NodeTransformer.generic_visit(self,node) code_ready = ast.fix_missing_locations(CodeSurgery().visit(ast.parse(self.text))) # run the remainder out = self.namespace #! exec to eval for python <2.7.15 eval(compile(code_ready,filename='<ast>',mode='exec'),out,out)
def test_while(self): '''while loops''' larchcode = 'n = 5' self.assertTrue(ast.dump(ast.parse(larchcode)) == ast.dump(self.li.compile(larchcode)))
def test(modules=None): import os import ast as ast_mod if not modules: #d = 'C:\\Python25\\Lib\\' d = '/usr/lib/python2.7/' modules = [d + f for f in os.listdir(d) if f.endswith('.py')] for f in modules: shutil.copy(f, 'input.py') msg = '%s...' % os.path.basename(f) ast, comments = parse_with_comments(f) print_code_and_ast(f, ast, comments) print print print ast_mod.dump(ast_mod.parse(file(f).read()), 0) try: ast2py(ast, comments) except: raise msg += 'failed to converting AST back to python code' try: if f == 'parser_test_source.py': print;print print file('output.py').read() # see the results parse(file('output.py').read()) msg += 'OK' except: raise msg += 'failed to parse generated code' print msg break
def test_count_thresh(): import ast import inspect p = ast.parse(inspect.getsource(count_thresh_orig)) print "AST" print ast.dump(p) print "Bytecode" import dis dis.dis(count_thresh_orig) v = np.array([1.2, 1.4, 5.0, 2, 3]) parakeet_result = count_thresh(v, 2.0) python_result = count_thresh_orig(v, 2.0) assert parakeet_result == python_result, "Parakeet %s != Python %s" % (parakeet_result, python_result) v = np.random.randn(10 ** 4) py_start = time.time() count_thresh_orig(v, 2.0) py_time = time.time() - py_start np_start = time.time() np_thresh(v, 2.0) np_time = time.time() - np_start par_start = time.time() count_thresh(v, 2.0) par_time = time.time() - par_start print "Python time: %.5f" % py_time print "NumPy time: %.5f" % np_time print "Parakeet time: %.5f" % par_time
def parse_compare(compare_node): assert len(compare_node.ops) == 1, "multiple comparison ops?" + ast.dump(compare_node) assert isinstance(compare_node.ops[0], ast.Eq), "comparison should be ==" + \ ast.dump(compare_node.ops[0]) lhs = compare_node.left rhs = compare_node.comparators[0] if isinstance(lhs, ast.Name) and isinstance(rhs, ast.Num): var_name = lhs.id val = rhs.n elif isinstance(rhs, ast.Name) and isinstance(lhs, ast.Num): var_name = rhs.id val = lhs.n elif isinstance(rhs, ast.Name) and isinstance(lhs, ast.Name): # try to apply macro if is_int_constant(rhs): var_name = lhs.id val = rhs.id elif is_int_constant(lhs): var_name = rhs.id val = lhs.id else: assert False, "Unable to apply macro to fix comparator " + ast.dump(compare_node) else: assert False, "unexpected comparator" + ast.dump(compare_node) return var_name, val
def build_test(stud, sol, state, do_eval, eq_fun, feedback_msg, add_more, highlight = False): got_error = False if do_eval: eval_solution, str_solution = getResultInProcess(tree = sol, process = state.solution_process) if isinstance(str_solution, Exception): raise ValueError("Running an argument in the solution environment raised an error") if isinstance(eval_solution, ReprFail): raise ValueError("Couldn't figure out the argument: " + eval_solution.info) eval_student, str_student = getResultInProcess(tree = stud, process = state.student_process) if isinstance(str_student, Exception): got_error = True # The (eval_student, ) part is important, because when eval_student is a tuple, we don't want # to expand them all over the %'s during formatting, we just want the tuple to be represented # in the place of the %r. Same for eval_solution. if add_more: if got_error: feedback_msg += " Expected `%s`, but got %s." % (str_solution, "an error") else: feedback_msg += " Expected `%s`, but got `%s`." % (str_solution, str_student) else: # We don't want the 'expected...' message here. It's a pain in the ass to deparse the ASTs to # give something meaningful. eval_student = ast.dump(stud) eval_solution = ast.dump(sol) _msg = state.build_message(feedback_msg) return(Test(Feedback(_msg, stud if highlight else None)) if got_error else eq_fun(eval_student, eval_solution, Feedback(_msg, stud if highlight else None)))
def iterscribe(self, line, line_num, indentation, program_ast): variable_id, variable_type = utils.get_id_and_type(line, program_ast) for node in ast.walk(program_ast): # TODO: handle nested for loops if ('iter' in node._fields and ast.dump(ast.parse(line).body[0]) in ast.dump(node)): line_number = node.lineno + self.offset() self.desugared_lines.insert(line_number, self.iter_start(node, line, line_num, program_ast, indentation)) iterator_index = "".join(random.choice(string.ascii_uppercase) for _ in range(10)) iterator_update = indentation + iterator_index + " += 1\n" self.desugared_lines.insert(line_number, indentation[:-4] + iterator_index + " = -1\n") self.desugared_lines.append(iterator_update) output = ("In iteration ' + str(" + iterator_index + ") + ', " + variable_id + " changed to ' + str(" + variable_id + ") ") return output raise KeyError("Could not find for loop")
def eval_left(self, input_line, line_holder): """Evaluate left side of the comparison argument line.""" left_holder = list() left_bin = list() left_holder.append(self.oper_clean(input_line.test.ops[0])) if isinstance(input_line.test.comparators[0], ast.BinOp): if ast.dump(input_line.test.comparators[0].op) == "Mod()": left_holder.append(input_line.test.comparators[0].left.id) left_holder.append(self.oper_clean(input_line.test .comparators[0].op)) left_holder.append(self.var_clean(input_line.test .comparators[0].right)) else: left_holder.extend(self.binop_clean(input_line.test .comparators[0], left_bin)) else: left_holder.append(self.var_clean(input_line.test.comparators[0])) if isinstance(input_line.test.left, ast.Name): left_holder.insert(0, input_line.test.left.id) line_holder.append(left_holder) elif isinstance(input_line.test.left, ast.BinOp): if ast.dump(input_line.test.left.op) == "Mod()": left_holder.insert(0, input_line.test.left.left.id) left_holder.insert(1, self.oper_clean(input_line.test.left.op)) left_holder.insert(2, self.var_clean(input_line.test .left.right)) line_holder.append(left_holder) else: self.eval_binop(input_line.test.left, left_holder, line_holder) return line_holder
def visit_Return(self, node: ast.Return) -> Any: # pylint: disable=no-self-use """Raise an exception that this node is unexpected.""" raise AssertionError( "Unexpected return node during the re-computation: {}".format( ast.dump(node)))
import ast import sys print(sys.argv) with open(sys.argv[1]) as f: src = f.read() src_ast = ast.parse(src) print(src) print(ast.dump(src_ast.body[0]))
def flatten_expr(target, expr): # x = y if isinstance(expr, ast.Name): return [['set', target, expr.id]] # x = 5 elif isinstance(expr, ast.Num): return [['set', target, expr.n]] # x = y (op) z # Or, for that matter, x = y (op) 5 elif isinstance(expr, ast.BinOp): if isinstance(expr.op, ast.Add): op = '+' elif isinstance(expr.op, ast.Mult): op = '*' elif isinstance(expr.op, ast.Sub): op = '-' elif isinstance(expr.op, ast.Div): op = '/' # Exponentiation gets compiled to repeat multiplication, # requires constant exponent elif isinstance(expr.op, ast.Pow): assert isinstance(expr.right, ast.Num) if expr.right.n == 0: return [['set', target, 1]] elif expr.right.n == 1: return flatten_expr(target, expr.left) else: # This could be made more efficient via square-and-multiply but oh well if isinstance(expr.left, (ast.Name, ast.Num)): nxt = base = expr.left.id if isinstance( expr.left, ast.Name) else expr.left.n o = [] else: nxt = base = mksymbol() o = flatten_expr(base, expr.left) for i in range(1, expr.right.n): latest = nxt nxt = target if i == expr.right.n - 1 else mksymbol() o.append(['*', nxt, latest, base]) return o else: raise Exception("Bad operation: " % ast.dump(stmt.op)) # If the subexpression is a variable or a number, then include it directly if isinstance(expr.left, (ast.Name, ast.Num)): var1 = expr.left.id if isinstance(expr.left, ast.Name) else expr.left.n sub1 = [] # If one of the subexpressions is itself a compound expression, recursively # apply this method to it using an intermediate variable else: var1 = mksymbol() sub1 = flatten_expr(var1, expr.left) # Same for right subexpression as for left subexpression if isinstance(expr.right, (ast.Name, ast.Num)): var2 = expr.right.id if isinstance(expr.right, ast.Name) else expr.right.n sub2 = [] else: var2 = mksymbol() sub2 = flatten_expr(var2, expr.right) # Last expression represents the assignment; sub1 and sub2 represent the # processing for the subexpression if any return sub1 + sub2 + [[op, target, var1, var2]] else: raise Exception("Unexpected statement value: %r" % stmt.value)
def testFullNameNode(self): t = ast_edits.full_name_node("a.b.c") self.assertEquals( ast.dump(t), "Attribute(value=Attribute(value=Name(id='a', ctx=Load()), attr='b', " "ctx=Load()), attr='c', ctx=Load())")
def visitTuple(self, n, ctx=ast.Load): assert isinstance(n.ctx, ctx), '%s:%d\n%s' % (self.filename, n.lineno, ast.dump(n)) return self.reduce_expr(n.elts,ctx)
from django.shortcuts import render #getfile1() #getfile2() #from .views import getfile #fileres1=getfile() #open('file1.py', 'w').close() #open('file2.py', 'w').close() #open('file1_dump.py', 'w').close() #open('file2_dump.py', 'w').close() open('file1_funcDef.py', 'w').close() open('file2_funcDef.py', 'w').close() file1 = ast.parse(open('file1.py').read()) print("First File is read and parsed") file2 = ast.parse(open('file2.py').read()) print("Second file is read and parsed") file1_dump = ast.dump(file1, annotate_fields=False, include_attributes=True) with open('file1_dump.py', 'wt') as f1: f1.write(file1_dump) file2_dump = ast.dump(file2, annotate_fields=False, include_attributes=True) with open('file2_dump.py', 'wt') as f2: f2.write(file2_dump) f7 = open('file1_dump.py', 'r') f8 = open('file2_dump.py', 'r') #FO = open('op_funcDef.txt', 'w') def sameprog(): open('file1_dump.py', 'w').close() open('file2_dump.py', 'w').close() file1 = ast.parse(open('file1.py').read()) file2 = ast.parse(open('file2.py').read())
def __str__(self): return ast.dump(self._ast)
def parse_type(item, location): # Base types, eg. num if isinstance(item, ast.Name): if item.id in base_types: return BaseType(item.id) elif item.id in special_types: return special_types[item.id] else: raise InvalidTypeException("Invalid base type: " + item.id, item) # Units, eg. num (1/sec) elif isinstance(item, ast.Call): if not isinstance(item.func, ast.Name): raise InvalidTypeException("Malformed unit type:", item) base_type = item.func.id if base_type not in ('num', 'decimal'): raise InvalidTypeException( "Base type with units can only be num and decimal", item) if len(item.args) == 0: raise InvalidTypeException("Malformed unit type", item) if isinstance(item.args[-1], ast.Name) and item.args[-1].id == "positional": positional = True argz = item.args[:-1] else: positional = False argz = item.args if len(argz) != 1: raise InvalidTypeException("Malformed unit type", item) unit = parse_unit(argz[0]) return BaseType(base_type, unit, positional) # Subscripts elif isinstance(item, ast.Subscript): if 'value' not in vars(item.slice): raise InvalidTypeException( "Array access must access a single element, not a slice", item) # Fixed size lists, eg. num[100] elif isinstance(item.slice.value, ast.Num): if not isinstance(item.slice.value.n, int) or item.slice.value.n <= 0: raise InvalidTypeException( "Arrays must have a positive integral number of elements", item.slice.value) return ListType(parse_type(item.value, location), item.slice.value.n) # Mappings, eg. num[address] else: if location == 'memory': raise InvalidTypeException( "No mappings allowed for in-memory types, only fixed-size arrays", item) keytype = parse_type(item.slice.value, None) if not isinstance(keytype, BaseType): raise InvalidTypeException("Mapping keys must be base types", item.slice.value) return MappingType(keytype, parse_type(item.value, location)) # Dicts, used to represent mappings, eg. {uint: uint}. Key must be a base type elif isinstance(item, ast.Dict): o = {} for key, value in zip(item.keys, item.values): if not isinstance(key, ast.Name) or not is_varname_valid(key.id): raise InvalidTypeException( "Invalid member variable for struct", key) o[key.id] = parse_type(value, location) return StructType(o) elif isinstance(item, ast.Compare): if len(item.ops) != 1 or not isinstance(item.ops[0], ast.LtE): raise InvalidTypeException("Invalid type", item) if not isinstance(item.left, ast.Name) or item.left.id != "bytes": raise InvalidTypeException("Invalid type", item.left) if len(item.comparators) != 1 or not isinstance( item.comparators[0], ast.Num): raise InvalidTypeException("Byte array length must be a number", item) if not isinstance(item.comparators[0].n, int) or item.comparators[0].n <= 0: raise InvalidTypeException( "Bad byte array length: %r" % item.comparators[0].n, item.comparators[0]) return ByteArrayType(item.comparators[0].n) else: raise InvalidTypeException("Invalid type: %r" % ast.dump(item), item)
def dev_dump(self): print(ast.dump(self.expr))
def dump(node, **kw): """simple ast dumper""" return ast.dump(node, **kw)
def _gen(node, in_loop=False): if isinstance(node, AST): if isinstance(node, Module): return "\n".join(["#include <stdio.h>", "int main()", "{", "\n".join(_gen(stmt) for stmt in node.body), "return 0;", "}"]) elif isinstance(node, Print): format = "\"%s\\n\"" % " ".join("%d" for x in node.values) items = ", ".join(_gen(x) for x in node.values) return 'printf(%s, %s);' % (format, items) elif isinstance(node, Num): return '(%s)' % node.n elif isinstance(node, UnaryOp) and isinstance(node.op, USub): return "(-%s)" % _gen(node.operand) elif isinstance(node, BinOp) and isinstance(node.op, Add): return '(%s+%s)' % (_gen(node.left), _gen(node.right)) elif isinstance(node, Assign): line = "%s = %s;" % (_gen(node.targets[0], in_loop), #pass it in _gen(node.value)) variables[node.targets[0].id] = node.value # assigned return line elif isinstance(node, Name): if isinstance(node.ctx, Load): # R-value if node.id not in variables or variables[node.id] is None: raise Exception("undefined variable %s in Python code" % node.id) return node.id elif node.id in variables: return node.id else: # new variable assignment variables[node.id] = None # blank place-holder # do not declare anything in loop! return node.id if in_loop else "int %s" % node.id elif isinstance(node, For) and isinstance(node.iter, Call) \ and node.iter.func.id == "range": current_vars = set(variables.keys()) # make a snapshot of current vars var_def = _gen(node.target) # int i var_out = _gen(node.target) # i limit = _gen(node.iter.args[0]) # x in range(x) variables[var_out] = 0 body = _gen(node.body[0], in_loop=True) # only ONE stmt; TODO: stmt+ # declare local vars used in the loop body outside of the loop declars = "\n".join("int %s;" % x for x in variables.keys() if x not in current_vars) var_in = make_loop_var(node.target.id + "_") limit_var = make_loop_var(var_in + "_") variables[limit_var] = 0 # can't reuse limit_var, but can reuse loop_var return "\n".join([declars, "int %s = %s;" % (limit_var, limit), "for (int %s = 0; %s < %s; %s++)" % (var_in, var_in, limit_var, var_in), " { %s = %s; %s }" % (var_out, var_in, body)]) else: raise Exception('Error in _gen: unrecognized AST node: %s' % ast.dump(node)) else: raise Exception('Error in _gen: unrecognized non-AST of %s: %s' % (str(type(node))[1:-1], str(node)))
body = _gen(node.body[0], in_loop=True) # only ONE stmt; TODO: stmt+ # declare local vars used in the loop body outside of the loop declars = "\n".join("int %s;" % x for x in variables.keys() if x not in current_vars) var_in = make_loop_var(node.target.id + "_") limit_var = make_loop_var(var_in + "_") variables[limit_var] = 0 # can't reuse limit_var, but can reuse loop_var return "\n".join([declars, "int %s = %s;" % (limit_var, limit), "for (int %s = 0; %s < %s; %s++)" % (var_in, var_in, limit_var, var_in), " { %s = %s; %s }" % (var_out, var_in, body)]) else: raise Exception('Error in _gen: unrecognized AST node: %s' % ast.dump(node)) else: raise Exception('Error in _gen: unrecognized non-AST of %s: %s' % (str(type(node))[1:-1], str(node))) return _gen(node) if __name__ == "__main__": variables = set() try: tree = ast.parse("\n".join(sys.stdin.readlines())) print >> logs, ast.dump(tree) # debug print generate_c(tree) except Exception, e: print >> logs, e.args[0] exit(-1)
def ast_dump(*nodes): "Dump ast node without unicode strings" return ', '.join( ast.dump(node).replace("Str(s=u'", "Str(s='").replace( "{u'", "{'") for node in nodes)
def get_children(node): parent = ast.dump(node) children = [] for child_node in ast.iter_child_nodes(node): children.append(ast.dump(child_node)) return parent, children
def dump(node, **kw): """Simple ast dumper.""" return ast.dump(node, **kw)
def parse_type(item, location, sigs=None, custom_units=None): custom_units = [] if custom_units is None else custom_units sigs = {} if sigs is None else sigs # Base types, e.g. num if isinstance(item, ast.Name): if item.id in base_types: return BaseType(item.id) elif item.id in special_types: return special_types[item.id] else: raise InvalidTypeException("Invalid base type: " + item.id, item) # Units, e.g. num (1/sec) or contracts elif isinstance(item, ast.Call): # Contract_types if item.func.id == 'address': if sigs and item.args[0].id in sigs: return ContractType(item.args[0].id) if not isinstance(item.func, ast.Name): raise InvalidTypeException("Malformed unit type:", item) base_type = item.func.id if base_type not in ('int128', 'uint256', 'decimal'): raise InvalidTypeException( "You must use int128, uint256, decimal, address, contract, \ for variable declarations and indexed for logging topics ", item) if len(item.args) == 0: raise InvalidTypeException("Malformed unit type", item) if isinstance(item.args[-1], ast.Name) and item.args[-1].id == "positional": positional = True argz = item.args[:-1] else: positional = False argz = item.args if len(argz) != 1: raise InvalidTypeException("Malformed unit type", item) unit = parse_unit(argz[0], custom_units=custom_units) return BaseType(base_type, unit, positional) # Subscripts elif isinstance(item, ast.Subscript): if 'value' not in vars(item.slice): raise InvalidTypeException( "Array / ByteArray access must access a single element, not a slice", item) # Fixed size lists or bytearrays, e.g. num[100] elif isinstance(item.slice.value, ast.Num): if not isinstance(item.slice.value.n, int) or item.slice.value.n <= 0: raise InvalidTypeException( "Arrays / ByteArrays must have a positive integral number of elements", item.slice.value) # ByteArray if getattr(item.value, 'id', None) == 'bytes': return ByteArrayType(item.slice.value.n) # List else: return ListType( parse_type(item.value, location, custom_units=custom_units), item.slice.value.n) # Mappings, e.g. num[address] else: if location == 'memory': raise InvalidTypeException( "No mappings allowed for in-memory types, only fixed-size arrays", item) keytype = parse_type(item.slice.value, None) if not isinstance(keytype, (BaseType, ByteArrayType)): raise InvalidTypeException( "Mapping keys must be base or bytes types", item.slice.value) return MappingType( keytype, parse_type(item.value, location, custom_units=custom_units)) # Dicts, used to represent mappings, e.g. {uint: uint}. Key must be a base type elif isinstance(item, ast.Dict): o = {} for key, value in zip(item.keys, item.values): if not isinstance(key, ast.Name) or not is_varname_valid( key.id, custom_units): raise InvalidTypeException( "Invalid member variable for struct", key) o[key.id] = parse_type(value, location, custom_units=custom_units) return StructType(o) elif isinstance(item, ast.Tuple): members = [ parse_type(x, location, custom_units=custom_units) for x in item.elts ] return TupleType(members) else: raise InvalidTypeException("Invalid type: %r" % ast.dump(item), item)
import ast t = ast.parse("print('hello')") print(ast.dump(t))
# parsing and analyzing python source x = 42 var1 = eval('2 + 2*3 + x') print(var1) import ast ex = ast.parse('2 + 2*3 + x', mode='eval') print(ex) print(ast.dump((ex)))
def update(self, tag, kwargs): """update - Return """ c = self.c if kwargs['c'] != c: return if not self.w.isVisible(): return if c.p.v != self.v: self.status.setText("(paused - different node)") return if not self.active: return self.status.setText("ACTIVE") source = c.p.b lines = source.split('\n') try: top_level = ast.parse(source) except SyntaxError: self.status.setText("ACTIVE - INCOMPLETE CODE") return if self.dump: self.dump = False print(ast.dump(top_level)) block = [] # blocks (strings) of source code nodes = list(ast.iter_child_nodes(top_level)) self.scope['p'] = c.p run_count = 0 # break source up into blocks corresponding to top level nodes for n, node in enumerate(nodes): if n == len(nodes) - 1: next_node = len(lines) else: next_node = nodes[n + 1].lineno block.append("".join(lines[node.lineno - 1:next_node - 1])) result = [] for n, node in enumerate(nodes): node_result = None if (n < len(self.codeblocks) and self.codeblocks[n].code == block[n]): # same code, assume same result node_result = self.codeblocks[n].result else: run_count += 1 # drop all remaining stored results (maybe none) del self.codeblocks[n:] try: if isinstance(node, ast.Expr): # pylint: disable=eval-used node_result = repr(eval(block[n], self.scope)) else: # exec block[n] in self.scope # EKR: Python 3 compatibility. exec(block[n], self.scope) except Exception: self.status.setText("ACTIVE: fail at %s" % block[n].split('\n')[0]) break if isinstance(node, ast.Expr): pass # already handled above elif isinstance(node, (ast.Assign, ast.AugAssign)): node_result = [] if isinstance(node, ast.AugAssign): todo = [node.target] else: todo = list(node.targets) while todo: target = todo.pop(0) if isinstance(target, ast.Tuple): todo.extend(target.elts) continue code = asttools.dump_python_source(target) # pylint: disable=eval-used node_result.append( "%s = %r" % (code.strip(), eval(code, self.scope))) node_result = ''.join(node_result) ### was '\n'.join assert node_result is None or isinstance(node_result, str) if node_result is None: self.codeblocks.append(self.CodeBlock(block[n], None)) else: self.codeblocks.append(self.CodeBlock(block[n], node_result)) result.append(node_result) self.text.setText('\n'.join(result)) ###was '\n\n.join if run_count: self.status.setText("ACTIVE: %d blocks" % run_count)
def check_code(self, code, nodes): linestarts = dict(dis.findlinestarts(code)) instructions = get_instructions(code) lineno = None for inst in instructions: lineno = linestarts.get(inst.offset, lineno) if not inst.opname.startswith(( 'BINARY_', 'UNARY_', 'LOAD_ATTR', 'LOAD_METHOD', 'LOOKUP_METHOD', 'SLICE+', 'COMPARE_OP', 'CALL_', )): continue frame = C() frame.f_lasti = inst.offset frame.f_code = code frame.f_globals = globals() frame.f_lineno = lineno source = Source.for_frame(frame) node = None try: try: node = Source.executing(frame).node except Exception: if inst.opname.startswith(('COMPARE_OP', 'CALL_')): continue if isinstance(only(source.statements_at_line(lineno)), (ast.AugAssign, ast.Import)): continue raise try: self.assertIsNone(nodes[node]) except KeyError: print(ast.dump(source.tree), list(ast.walk(source.tree)), nodes, node, ast.dump(node), file=sys.stderr, sep='\n') except Exception: print(source.text, lineno, inst, node and ast.dump(node), code, file=sys.stderr, sep='\n') raise nodes[node] = (inst, frame.__dict__) yield [inst.opname, node_string(source, node)] for const in code.co_consts: if isinstance(const, type(code)): for x in self.check_code(const, nodes): yield x
def _parse_class(self, clazz): """Parse the AST of a single class definition @param clazz the AST node for the class """ componentData = {"name": clazz.name, "file": self.relpath + basename(self.source_file), "desc": "%s PyXPCOM component" % (clazz.name)} for stmt in clazz.body: if not isinstance(stmt, ast.Assign): # we only care about assignments (to the magic props) continue if isinstance(stmt.value, ast.Str): # assignment from a string; we might need this later. for target in stmt.targets: if not isinstance(target, ast.Name): continue self.set(clazz.name, target.id, self.get(stmt.value)) if filter(lambda n: n.id == "_reg_contractid_", stmt.targets): # this is an assignment to _reg_contractid_ value = self.get(stmt.value, clazz.name) if value is not None: componentData["contractid"] = value.replace(" ", "%20") if filter(lambda n: n.id == "_reg_clsid_", stmt.targets): # this is an assignment to _reg_clsid_ value = self.get(stmt.value, clazz.name) if value is not None: # check for CIDs with missing braces if re.match(r"^[0-9a-fA-F-]{36}$", value): value = "{%s}" % value componentData["clsid"] = value if filter(lambda n: n.id == "_reg_desc_", stmt.targets): # this is an assignment to _reg_desc_ value = self.get(stmt.value, clazz.name) if value is not None: componentData["desc"] = urllib.quote(value) if filter(lambda n: n.id == "_reg_categories_", stmt.targets): # this is a category registration assert isinstance(stmt.value, ast.Tuple) or isinstance(stmt.value, ast.List), \ "%s line %i: category assignement must be a list, got %s" % ( self.source_file, stmt.value.lineno, ast.dump(stmt.value)) for entry in stmt.value.elts: assert isinstance(entry, ast.Tuple) or isinstance(entry, ast.List), \ "%s line %i: category entry must be a list, got %s" % ( self.source_file, entry.lineno, ast.dump(entry)) assert len(entry.elts) == 2 or len(entry.elts) == 3, \ "%s line %i: category entry must have 2 or 3 values, got %s" % ( self.source_file, entry.lineno, ast.dump(entry)) if not "category" in componentData: componentData["category"] = [] entryData = list(map(lambda k: self.get(k, clazz.name), entry.elts)) if len(entryData) < 3: entryData.append(False) assert isinstance(entryData[2], bool), \ "%s line %i: category entry %s for class %s has non-bool third arg %s" % ( self.source_file, entry.lineno, entryData[0], clazz.name, ast.dump(entry.elts[2])) componentData["category"].append(entryData) if "contractid" in componentData and "clsid" in componentData: self.new_lines.add("component {clsid} {file}".format(**componentData)) self.new_lines.add("contract {contractid} {clsid}".format(**componentData)) if "category" in componentData: for entry in componentData["category"]: category = entry[0] contractid = componentData["contractid"] if category == "app-startup" and entry[2] == True: # app-startup with a true third arg means use as service contractid = "service,%s" % (contractid) entryname = urllib.quote(entry[1]) self.new_lines.add("category {category} {entryname} {contractid}".format(**locals()))
def encode(self, python_source): token_ids = [] for line in python_source.split("\n"): if line.startswith("(") and line.endswith(")"): # This is a condition. It was originally part of a statement like: # "while x > 3:" or "if y < 2:". # To us, it should symbolize vBranch = `line`, and vBranch will be used # immediately in the next line. # These lines are inserted in python_interpreter during interpretation # of while or if statements. original_line = line line = line[1:-1] line = line.replace("(", "").replace(")", "") assert ast.dump(ast.parse(line)) == ast.dump( ast.parse(original_line)) if not line: # Skip blank lines. continue indent = int( (len(line) - len(line.lstrip())) / constants.INDENT_SPACES) if "while" in line or "if" in line: if "%" in line: # while v3 % 10 > 0: control_op, var, mod_op, unused_mod_operand, cond_op, operand = ( line.rstrip(":").split()) operand = int(operand) assert control_op in ("while", "if") assert cond_op in (">", "<", ">=", "<=") op = f"{control_op} {cond_op} {mod_op}" statement = ([ self.indent_token_id(indent), self.op_token_id(op), self.var_token_id(var) ] + self.operand_token_ids(operand)) else: # while v3 > 0: control_op, var, cond_op, operand = line.rstrip( ":").split() operand = int(operand) assert control_op in ("while", "if") assert cond_op in (">", "<", ">=", "<=") op = f"{control_op} {cond_op}" statement = ([ self.indent_token_id(indent), self.op_token_id(op), self.var_token_id(var) ] + self.operand_token_ids(operand)) elif "else" in line: op = "else" statement = ([ self.indent_token_id(indent), self.op_token_id(op), self.op_token_id(op) ] + self.operand_token_ids(0)) elif "pass" in line: op = "pass" statement = ([ self.indent_token_id(indent), self.op_token_id(op), self.op_token_id(op) ] + self.operand_token_ids(0)) elif "continue" in line: op = "continue" statement = ([ self.indent_token_id(indent), self.op_token_id(op), self.op_token_id(op) ] + self.operand_token_ids(0)) elif "break" in line: op = "break" statement = ([ self.indent_token_id(indent), self.op_token_id(op), self.op_token_id(op) ] + self.operand_token_ids(0)) else: # Handles both the `var op operand` case and the `(v1 > 0)` case. # The former is a regular statement. The latter is used as a condition # in a control flow statement. # We cannot distinguish if statements from while statements here. if "%" in line: # v3 % 10 > 0 var, mod_op, unused_mod_operand, cond_op, operand = line.split( ) op = f"{cond_op} {mod_op}" else: # v3 > 0 var, op, operand = line.split() if var == "_": # This is a placeholder statement. placeholder = self.placeholder_token_id() statement = ([ self.indent_token_id(indent), placeholder, placeholder ] + self.operand_token_ids(0)) else: op_token_id = self.op_token_id(op) if operand.startswith("v"): # If the operand is a variable, pad the var_token_id. operand_token_ids = ([self.var_token_id(operand)] + [self.var_padding_token_id()] * (self.num_digits - 1)) else: # The operand is a number. operand_token_ids = self.operand_token_ids(operand) statement = [ self.indent_token_id(indent), op_token_id, self.var_token_id(var) ] + operand_token_ids token_ids.extend(statement) return token_ids
def parse_type(item, location, sigs={}): # Base types, eg. num if isinstance(item, ast.Name): if item.id in base_types: return BaseType(item.id) elif item.id in special_types: return special_types[item.id] else: raise InvalidTypeException("Invalid base type: " + item.id, item) # Units, eg. num (1/sec) or contracts elif isinstance(item, ast.Call): # Contract_types if item.func.id == 'contract' or item.func.id == 'address': if sigs and item.args[0].id in sigs: return BaseType('address', item.args[0].id) else: raise InvalidTypeException('Invalid contract declaration') if not isinstance(item.func, ast.Name): raise InvalidTypeException("Malformed unit type:", item) base_type = item.func.id if base_type not in ('num', 'decimal'): raise InvalidTypeException("You must use num, decimal, address, contract, \ for variable declarations and indexed for logging topics ", item) if len(item.args) == 0: raise InvalidTypeException("Malformed unit type", item) if isinstance(item.args[-1], ast.Name) and item.args[-1].id == "positional": positional = True argz = item.args[:-1] else: positional = False argz = item.args if len(argz) != 1: raise InvalidTypeException("Malformed unit type", item) # Check for num256 to num casting if item.func.id == 'num' and getattr(item.args[0], 'id', '') == 'num256': return BaseType('num', override_signature='num256') unit = parse_unit(argz[0]) return BaseType(base_type, unit, positional) # Subscripts elif isinstance(item, ast.Subscript): if 'value' not in vars(item.slice): raise InvalidTypeException("Array access must access a single element, not a slice", item) # Fixed size lists, eg. num[100] elif isinstance(item.slice.value, ast.Num): if not isinstance(item.slice.value.n, int) or item.slice.value.n <= 0: raise InvalidTypeException("Arrays must have a positive integral number of elements", item.slice.value) return ListType(parse_type(item.value, location), item.slice.value.n) # Mappings, eg. num[address] else: if location == 'memory': raise InvalidTypeException("No mappings allowed for in-memory types, only fixed-size arrays", item) keytype = parse_type(item.slice.value, None) if not isinstance(keytype, (BaseType, ByteArrayType)): raise InvalidTypeException("Mapping keys must be base or bytes types", item.slice.value) return MappingType(keytype, parse_type(item.value, location)) # Dicts, used to represent mappings, eg. {uint: uint}. Key must be a base type elif isinstance(item, ast.Dict): o = {} for key, value in zip(item.keys, item.values): if not isinstance(key, ast.Name) or not is_varname_valid(key.id): raise InvalidTypeException("Invalid member variable for struct", key) o[key.id] = parse_type(value, location) return StructType(o) elif isinstance(item, ast.Compare): if len(item.ops) != 1 or not isinstance(item.ops[0], ast.LtE): raise InvalidTypeException("Invalid type", item) if not isinstance(item.left, ast.Name) or item.left.id != "bytes": raise InvalidTypeException("Invalid type", item.left) if len(item.comparators) != 1 or not isinstance(item.comparators[0], ast.Num): raise InvalidTypeException("Byte array length must be a number", item) if not isinstance(item.comparators[0].n, int) or item.comparators[0].n <= 0: raise InvalidTypeException("Bad byte array length: %r" % item.comparators[0].n, item.comparators[0]) return ByteArrayType(item.comparators[0].n) elif isinstance(item, ast.Tuple): members = [parse_type(x, location) for x in item.elts] return TupleType(members) else: raise InvalidTypeException("Invalid type: %r" % ast.dump(item), item)
def get(self, expr, scope=None): """ Get the value of an expression @param expr the (AST) expression to evaluate @param name the scope in which to look for variables; the global scope will also be used. @return the evaluated expression (as a string) """ def lookup(key): """Look up the given variable name @param key the variable name """ # special python keyword literals value = {"None": None, "True": True, "False": False, }.get(key, "") if value != "": return value if scope in self.vars and key in self.vars[scope]: return self.vars[scope][key] if key in self.vars[""]: return self.vars[""][key] assert (scope in self.vars and key in self.vars[scope]) or (key in self.vars[""]), \ "Failed to find {key} for {scope} in {file} line {line}".format( {"key": key, "scope": scope, "file": self.source_file, "line": expr.lineno}) # the scope name "" (empty string) is reserved for globals since it's # not a valid python identifier assert scope != "", "Invalid scope name" if isinstance(expr, ast.Name): # variable or keyword return lookup(expr.id) if isinstance(expr, ast.Str): # string literal return expr.s if isinstance(expr, ast.BinOp) and isinstance(expr.op, ast.Mod) and isinstance(expr.left, ast.Str): # expression is a formatted string: "foo %s" % bar if isinstance(expr.right, ast.Str) or isinstance(expr.right, ast.Name): # a single value; wrap it in a list values = [self.get(expr.right, scope)] elif isinstance(expr.right, ast.Tuple) or isinstance(expr.right, ast.List): # multiple values; read them in turn values = [] for v in expr.right.elts: assert isinstance(v, ast.Name), \ "don't know how to handle %s (%s) when parsing %s line %s" % ( type(v).__name__, ast.dump(v), self.source_file, stmt.lineno) values.append(lookup(v.id)) else: assert False, \ "don't know how to handle %s (%s) when parsing %s line %s" % ( type(expr.right).__name__, ast.dump(expr.right), self.source_file, stmt.lineno) # value lookup done, do the formatting return expr.left.s % tuple(values) if isinstance(expr, ast.BinOp) and isinstance(expr.op, ast.Add): # x + y: hopefully these things are strings... return self.get(expr.left, scope) + self.get(expr.right, scope) raise NotImplementedError( "%s line %i col %i in class %s: don't know how to deal with expression %s" % ( self.source_file, expr.lineno, expr.col_offset, scope, ast.dump(expr)))
def _assert_source_equal(self, src1, src2): with open(src1) as f1, open(src2) as f2: ast1 = ast.dump(ast.parse(f1.read())) ast2 = ast.dump(ast.parse(f2.read())) self.assertEqual(ast1, ast2)
def visitName(self, n, ctx=ast.Load): assert isinstance(n.ctx, ctx), '%s:%d\n%s' % (self.filename, n.lineno, ast.dump(n)) return set()
def test_no_mutation_leaves_ast_unchanged(operator, code): node = ast.parse(code) core = MutatingCore(-1) replacer = operator(core) assert ast.dump(node) == ast.dump(replacer.visit(copy.deepcopy(node)))
def ast_to_string(vyper_ast_node: vyper_ast.VyperNode) -> str: py_ast_node = to_python_ast(vyper_ast_node) return python_ast.dump(python_ast.Module(body=py_ast_node))
def dict_fail(self): warnings.warn( "Anonymous structs have been removed in" " favor of named structs, see VIP300", DeprecationWarning) raise InvalidLiteralException( "Invalid literal: %r" % ast.dump(self.expr), self.expr)
def visitStarred(self, n, ctx=ast.Load): assert isinstance(n.ctx, ctx), '%s:%d\n%s' % (self.filename, n.lineno, ast.dump(n)) return self.dispatch(n.value, ctx)