Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd8a127

Browse files
committed
Fix for sibling nodes that define the same free variable
Evan Simpson's fix. And his explanation: If you defined two nested functions in a row that refer to the same non-global variable, the second one will be generated as though the variable were global.
1 parent 7e30c9b commit cd8a127

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

Lib/compiler/symbols.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ def get_children(self):
7979
return self.children
8080

8181
def DEBUG(self):
82-
return
8382
print >> sys.stderr, self.name, self.nested and "nested" or ""
8483
print >> sys.stderr, "\tglobals: ", self.globals
8584
print >> sys.stderr, "\tcells: ", self.cells
@@ -162,12 +161,12 @@ def add_frees(self, names):
162161
child_globals.append(name)
163162
elif isinstance(self, FunctionScope) and sc == SC_LOCAL:
164163
self.cells[name] = 1
165-
else:
164+
elif sc != SC_CELL:
166165
child_globals.append(name)
167166
else:
168167
if sc == SC_LOCAL:
169168
self.cells[name] = 1
170-
else:
169+
elif sc != SC_CELL:
171170
child_globals.append(name)
172171
return child_globals
173172

@@ -221,7 +220,6 @@ def visitFunction(self, node, parent):
221220
self._do_args(scope, node.argnames)
222221
self.visit(node.code, scope)
223222
self.handle_free_vars(scope, parent)
224-
scope.DEBUG()
225223

226224
def visitLambda(self, node, parent):
227225
for n in node.defaults:
@@ -243,8 +241,6 @@ def _do_args(self, scope, args):
243241

244242
def handle_free_vars(self, scope, parent):
245243
parent.add_child(scope)
246-
if scope.children:
247-
scope.DEBUG()
248244
scope.handle_children()
249245

250246
def visitClass(self, node, parent):
@@ -298,6 +294,14 @@ def visitImport(self, node, scope):
298294
def visitAssName(self, node, scope, assign=1):
299295
scope.add_def(node.name)
300296

297+
def visitAssAttr(self, node, scope, assign=0):
298+
self.visit(node.expr, scope, 0)
299+
300+
def visitSubscript(self, node, scope, assign=0):
301+
self.visit(node.expr, scope, 0)
302+
for n in node.subs:
303+
self.visit(n, scope, 0)
304+
301305
def visitAugAssign(self, node, scope):
302306
# If the LHS is a name, then this counts as assignment.
303307
# Otherwise, it's just use.

Tools/compiler/compiler/symbols.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ def get_children(self):
7979
return self.children
8080

8181
def DEBUG(self):
82-
return
8382
print >> sys.stderr, self.name, self.nested and "nested" or ""
8483
print >> sys.stderr, "\tglobals: ", self.globals
8584
print >> sys.stderr, "\tcells: ", self.cells
@@ -162,12 +161,12 @@ def add_frees(self, names):
162161
child_globals.append(name)
163162
elif isinstance(self, FunctionScope) and sc == SC_LOCAL:
164163
self.cells[name] = 1
165-
else:
164+
elif sc != SC_CELL:
166165
child_globals.append(name)
167166
else:
168167
if sc == SC_LOCAL:
169168
self.cells[name] = 1
170-
else:
169+
elif sc != SC_CELL:
171170
child_globals.append(name)
172171
return child_globals
173172

@@ -221,7 +220,6 @@ def visitFunction(self, node, parent):
221220
self._do_args(scope, node.argnames)
222221
self.visit(node.code, scope)
223222
self.handle_free_vars(scope, parent)
224-
scope.DEBUG()
225223

226224
def visitLambda(self, node, parent):
227225
for n in node.defaults:
@@ -243,8 +241,6 @@ def _do_args(self, scope, args):
243241

244242
def handle_free_vars(self, scope, parent):
245243
parent.add_child(scope)
246-
if scope.children:
247-
scope.DEBUG()
248244
scope.handle_children()
249245

250246
def visitClass(self, node, parent):
@@ -298,6 +294,14 @@ def visitImport(self, node, scope):
298294
def visitAssName(self, node, scope, assign=1):
299295
scope.add_def(node.name)
300296

297+
def visitAssAttr(self, node, scope, assign=0):
298+
self.visit(node.expr, scope, 0)
299+
300+
def visitSubscript(self, node, scope, assign=0):
301+
self.visit(node.expr, scope, 0)
302+
for n in node.subs:
303+
self.visit(n, scope, 0)
304+
301305
def visitAugAssign(self, node, scope):
302306
# If the LHS is a name, then this counts as assignment.
303307
# Otherwise, it's just use.

0 commit comments

Comments
 (0)