Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print("\n--------MEDIUM-------- (ok)\n")
x = ast.dump(my_ast, annotate_fields=True, include_attributes=False)
print(x)
print("\n--------SHORT-------- (view-only)\n")
x = ast.dump(my_ast, annotate_fields=False, include_attributes=False)
print(x)
print("")
if do_exec: exec(expr)
exit(0)
try:
inline_ast = "set in execfile:"
if sys.version < '3':execfile(ast_file)
else: exec(open(ast_file).read())
try:
source = codegen.to_source(inline_ast)
print(source) # => CODE
except Exception as ex:
print(ex)
my_ast = ast.fix_missing_locations(inline_ast)
code = compile(inline_ast, "out/inline", 'exec')
# if do_exec:
exec(code)
except Exception as e:
raise
def docstring_dbg():
print(astor.dump_tree(astor.code_to_ast(func)))
def assertAstEqual(self, ast1, ast2):
dmp1 = astor.dump_tree(ast1)
dmp2 = astor.dump_tree(ast2)
self.assertEqual(dmp1, dmp2)
def assertAstEqual(self, ast1, ast2):
dmp1 = astor.dump_tree(ast1)
dmp2 = astor.dump_tree(ast2)
self.assertEqual(dmp1, dmp2)
def makelib():
parse = ast.parse
dump_tree = astor.dump_tree
def default_value(): return 1000000, ''
mydict = collections.defaultdict(default_value)
allparams = [tuple('abcdefghijklmnop'[:x]) for x in range(13)]
alltxt = itertools.chain(build(1, use_operands=True),
build(2, use_operands=True),
build(3, select_operators))
yieldrepl = list(('yield %s %s' % (operator, operand),
'yield %s%s' % (operator, operand))
for operator in '+-' for operand in '(ab')
yieldrepl.append(('yield[', 'yield ['))
# alltxt = itertools.chain(build(1), build(2))
badexpr = 0
goodexpr = 0
def test_aliases(self):
self.assertIs(astor.parse_file, astor.code_to_ast.parse_file)
def test_split_lines_unicode_support(self):
source = [u'copy', '\n']
self.assertEqual(split_lines(source), source)
def test_auto_generated_attributes(self):
# See #136 for more details.
treewalk = astor.TreeWalk()
self.assertIsInstance(treewalk.__dict__, dict)
# Check that the inital state of the instance is empty.
self.assertEqual(treewalk.__dict__['nodestack'], [])
self.assertEqual(treewalk.__dict__['pre_handlers'], {})
self.assertEqual(treewalk.__dict__['post_handlers'], {})
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import astor
def TestMe(x, y, width=10, foo=None):
from foo.bar.baz.murgatroyd import sally as bob
a.b = c.d + x.y.z.a.b
m.n = q = (w.w, x.x.y.y) = f(x.x.y.z)
func_ast = astor.codetoast(TestMe)
print(astor.dump(func_ast))
print(astor.to_source(func_ast))
class ConsolidateAttributes(astor.TreeWalk):
def post_Attribute(self):
node = self.cur_node
value = node.value
value.id += '.' + node.attr
self.replace(value)
ConsolidateAttributes(func_ast)
class FindNames(astor.TreeWalk):
def init_Assign(self):
self.assignments = []
self.current_assign = None
def pre_Assign(self):
self.current_assign = [], []
def post_Assign(self):
self.assignments.append(self.current_assign)
m.n = q = (w.w, x.x.y.y) = f(x.x.y.z)
func_ast = astor.codetoast(TestMe)
print(astor.dump(func_ast))
print(astor.to_source(func_ast))
class ConsolidateAttributes(astor.TreeWalk):
def post_Attribute(self):
node = self.cur_node
value = node.value
value.id += '.' + node.attr
self.replace(value)
ConsolidateAttributes(func_ast)
class FindNames(astor.TreeWalk):
def init_Assign(self):
self.assignments = []
self.current_assign = None
def pre_Assign(self):
self.current_assign = [], []
def post_Assign(self):
self.assignments.append(self.current_assign)
self.current_assign = None
def pre_targets_name(self):
self.in_targets = True
def post_targets_name(self):
self.in_targets = False
def post_Name(self):
if self.current_assign:
self.current_assign[self.in_targets].append(self.cur_node.id)