def check(expr1=None, expr2=None): ok = 1 expr1 = expr1 or sys.argv[1] expr2 = expr2 or sys.argv[2] l1 = munge(astl(expr1)) l2 = astl(expr2) try: c1 = compileast(sequence2ast(l1)) except: traceback.print_exc c1 = None c2 = compileast(sequence2ast(l2)) if c1 != c2: ok = 0 print 'test failed', expr1, expr2 print print l1 print print l2 print ast = parser.sequence2ast(l1) c = parser.compileast(ast) pretty(expr1) pret(l1) pret(l2) return ok
def check(expr1=None, expr2=None): ok=1 expr1=expr1 or sys.argv[1] expr2=expr2 or sys.argv[2] l1=munge(astl(expr1)) l2=astl(expr2) try: c1=compileast(sequence2ast(l1)) except: traceback.print_exc c1=None c2=compileast(sequence2ast(l2)) if c1 !=c2: ok=0 print 'test failed', expr1, expr2 print print l1 print print l2 print ast=parser.sequence2ast(l1) c=parser.compileast(ast) pretty(expr1) pret(l1) pret(l2) return ok
def compile(src, file_name, ctype): if ctype == 'eval': ast = parser.expr(src) elif ctype == 'exec': ast = parser.suite(src) l = ast2list(ast) l = munge(l) ast = sequence2ast(l) return parser.compileast(ast, file_name)
def parse_config(confstr): """ Use *eval(...)* to execute a filtered AST tree formed by parsing a configuration file, removing unwanted expressions (if any) and then compiling the filtered output to Python byte code. This approach allows the use of Python expressions and comments in config files, avoids the use of modules which is not particularily pretty (IMO). The following expressions (and combinations of) are allowed: Assignments, Arithmetic, Strings, Lists, if...then...else and Comments. Returns a dict containing the configuration values, if successful. """ dummy = dict() result = dict() # Parse the python source code into a filtered AST-tree. cast = _parseconf(confstr) # Compile AST to bytecode, this also detects syntax errors. cobj = parser.compileast(cast) # Run the bytecode. The dicts *dummy* and *results* are placeholders # for the *globals* *locals* environments used by *eval(...)*. The # variables declared and initialised in the config file will end up # in *locals* together with the *__globals__* environment while the # *locals* will contain only the values created by *eval(...)*. This # is good, because we can protect *__globals__* and return only the # configuration values by doing this: eval(cobj, dummy, result) return result
def compile(src, file_name, ctype): if ctype=='eval': ast=parser.expr(src) elif ctype=='exec': ast=parser.suite(src) l=ast2list(ast) l=munge(l) ast=sequence2ast(l) return parser.compileast(ast, file_name)
def reallycompile(tuples_or_src, filename, mode, flag_names): if type(tuples_or_src) is str: flags = 0 if 'nested_scopes' in flag_names: flags |= __future__.CO_NESTED if 'generators' in flag_names: flags |= __future__.CO_GENERATOR_ALLOWED if 'division' in flag_names: flags |= __future__.CO_FUTURE_DIVISION return compile(tuples_or_src, filename, mode, flags) return parser.compileast(parser.tuple2ast(tuples_or_src), filename)
def spam(): # Regression test import traceback ok=1 for expr1, expr2 in ( ("a*b", "__guarded_mul__(_vars, a, b)"), ("a*b*c", "__guarded_mul__(_vars, __guarded_mul__(_vars, a, b), c)" ), ("a.b", "__guarded_getattr__(_vars, a, 'b')"), ("a[b]", "__guarded_getitem__(_vars, a, b)"), ("a[b,c]", "__guarded_getitem__(_vars, a, b, c)"), ("a[b:c]", "__guarded_getslice__(_vars, a, b, c)"), ("a[:c]", "__guarded_getslice__(_vars, a, 0, c)"), ("a[b:]", "__guarded_getslice__(_vars, a, b)"), ("a[:]", "__guarded_getslice__(_vars, a)"), ("_vars['sequence-index'] % 2", "__guarded_getitem__(_vars, _vars, 'sequence-index') % 2" ), ): l1=munge(astl(expr1)) l2=astl(expr2) try: c1=compileast(sequence2ast(l1)) except: traceback.print_exc c1=None c2=compileast(sequence2ast(l2)) if c1 !=c2: ok=0 print 'test failed', expr1, expr2 print print l1 print print l2 print ast=parser.sequence2ast(l1) c=parser.compileast(ast) if ok: print 'all tests succeeded'
def spam(): # Regression test import traceback ok = 1 for expr1, expr2 in ( ("a*b", "__guarded_mul__(_vars, a, b)"), ("a*b*c", "__guarded_mul__(_vars, __guarded_mul__(_vars, a, b), c)"), ("a.b", "__guarded_getattr__(_vars, a, 'b')"), ("a[b]", "__guarded_getitem__(_vars, a, b)"), ("a[b,c]", "__guarded_getitem__(_vars, a, b, c)"), ("a[b:c]", "__guarded_getslice__(_vars, a, b, c)"), ("a[:c]", "__guarded_getslice__(_vars, a, 0, c)"), ("a[b:]", "__guarded_getslice__(_vars, a, b)"), ("a[:]", "__guarded_getslice__(_vars, a)"), ("_vars['sequence-index'] % 2", "__guarded_getitem__(_vars, _vars, 'sequence-index') % 2"), ): l1 = munge(astl(expr1)) l2 = astl(expr2) try: c1 = compileast(sequence2ast(l1)) except: traceback.print_exc c1 = None c2 = compileast(sequence2ast(l2)) if c1 != c2: ok = 0 print 'test failed', expr1, expr2 print print l1 print print l2 print ast = parser.sequence2ast(l1) c = parser.compileast(ast) if ok: print 'all tests succeeded'
def find_executable_linenos(filename): """return a dict of the line numbers from executable statements in a file Works by finding all of the code-like objects in the module then searching the byte code for 'SET_LINENO' terms (so this won't work one -O files). """ import parser prog = open(filename).read() ast = parser.suite(prog) code = parser.compileast(ast, filename) # The only way I know to find line numbers is to look for the # SET_LINENO instructions. Isn't there some way to get it from # the AST? return _find_LINENO(code)
name = token.tok_name.get(node[0]) print(name, end=' ') for i in range(1, len(node)): item = node[i] if type(item) is type([]): dump_and_modify(item) else: print(repr(item)) if name == "NUMBER": # increment all numbers! node[i] = repr(int(item)+1) ast = parser.expr("1 + 3") list = ast.tolist() dump_and_modify(list) ast = parser.sequence2ast(list) print(eval(parser.compileast(ast))) ## eval_input testlist test and_test not_test comparison ## expr xor_expr and_expr shift_expr arith_expr term factor ## power atom NUMBER '1' ## PLUS '+' ## term factor power atom NUMBER '3' ## NEWLINE '' ## ENDMARKER '' ## 6
name = token.tok_name.get(node[0]) print name, for i in range(1, len(node)): item = node[i] if type(item) is type([]): dump_and_modify(item) else: print repr(item) if name == "NUMBER": # increment all numbers! node[i] = repr(int(item)+1) ast = parser.expr("1 + 3") list = ast.tolist() dump_and_modify(list) ast = parser.sequence2ast(list) print eval(parser.compileast(ast)) ## eval_input testlist test and_test not_test comparison ## expr xor_expr and_expr shift_expr arith_expr term factor ## power atom NUMBER '1' ## PLUS '+' ## term factor power atom NUMBER '3' ## NEWLINE '' ## ENDMARKER '' ## 6
#!/usr/bin/env python