Skip to content

Commit 6e8be9a

Browse files
author
Jonathan Feinberg
committed
Fix typo. Reformat.
1 parent b7aaf6e commit 6e8be9a

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

misc/ast/ast.pyde

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import ast
22

3+
34
class NameAccumulator(ast.NodeVisitor):
5+
46
"""
57
NameAccumulator walks an AST "target" node, recursively gathering the 'id'
68
properties of all of the Names it finds.
79
"""
10+
811
def __init__(self):
912
self.names = set()
1013

1114
def visit_Name(self, name):
1215
self.names.add(name.id)
1316

17+
1418
def get_module_globals(module):
1519
"""
1620
Examine all of the top-level nodes in the module, and remember the names
@@ -22,13 +26,15 @@ def get_module_globals(module):
2226
for t in node.targets:
2327
acc.visit(t)
2428
return acc.names
25-
29+
2630

2731
class FindFunctionAssignments(ast.NodeVisitor):
32+
2833
"""
29-
Finds assignments in a function body, and accumlates the names of the
30-
assigned-to entities.
34+
Finds assignments in a function body, and accumulates the
35+
names of the assigned-to entities.
3136
"""
37+
3238
def __init__(self):
3339
self.acc = NameAccumulator()
3440

@@ -43,18 +49,19 @@ class FindFunctionAssignments(ast.NodeVisitor):
4349
self.visit(func)
4450
return self.acc.names
4551

52+
4653
def insert_global_statements(module):
4754
"""
4855
Finds all of the function definitions in a module, and inserts global
4956
statements in those that assign to names that are the same as existing
5057
module globals.
51-
58+
5259
For example, insert_global_statements will transform the AST for
53-
60+
5461
x = 0
5562
def draw():
5663
x = (x + 1) % width
57-
64+
5865
into the AST for
5966
6067
x = 0
@@ -69,7 +76,7 @@ def insert_global_statements(module):
6976
globals = get_module_globals(module)
7077
needed = assigned_names.difference(args).intersection(globals)
7178
node.body.insert(0, __global__(needed))
72-
79+
7380
source = """
7481
x = 0
7582
@@ -85,7 +92,8 @@ def draw():
8592
"""
8693
module = ast.parse(source)
8794
insert_global_statements(module)
88-
#fixed = ast.fix_missing_locations(p)
95+
# Is this desirable?
96+
# fixed = ast.fix_missing_locations(p)
8997
codeobj = compile(module, __file__, mode='exec')
9098
exec(codeobj)
9199

0 commit comments

Comments
 (0)