Commit f3fafd06 authored by Shane Hathaway's avatar Shane Hathaway

Fixed a nested scopes bug reported by jellej@pacbell.net. Thanks!

Evan's description:

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.
parent ce3191fa
...@@ -162,12 +162,12 @@ class Scope: ...@@ -162,12 +162,12 @@ class Scope:
child_globals.append(name) child_globals.append(name)
elif isinstance(self, FunctionScope) and sc == SC_LOCAL: elif isinstance(self, FunctionScope) and sc == SC_LOCAL:
self.cells[name] = 1 self.cells[name] = 1
else: elif sc != SC_CELL:
child_globals.append(name) child_globals.append(name)
else: else:
if sc == SC_LOCAL: if sc == SC_LOCAL:
self.cells[name] = 1 self.cells[name] = 1
else: elif sc != SC_CELL:
child_globals.append(name) child_globals.append(name)
return child_globals return child_globals
......
from __future__ import nested_scopes
def print0(): def print0():
print 'Hello, world!', print 'Hello, world!',
...@@ -136,4 +137,12 @@ def rot13(ss): ...@@ -136,4 +137,12 @@ def rot13(ss):
res = res + mapping.get(c, c) res = res + mapping.get(c, c)
return res return res
def nested_scopes_1():
# Fails if 'a' is consumed by the first function.
a = 1
def f1():
return a
def f2():
return a
return f1() + f2()
...@@ -57,7 +57,7 @@ def create_rmodule(): ...@@ -57,7 +57,7 @@ def create_rmodule():
compile(source, fn, 'exec') compile(source, fn, 'exec')
# Now compile it for real # Now compile it for real
code = compile_restricted(source, fn, 'exec') code = compile_restricted(source, fn, 'exec')
rmodule = {'__builtins__':None} rmodule = {'__builtins__':{'__import__':__import__, 'None':None}}
builtins = getattr(__builtins__, '__dict__', __builtins__) builtins = getattr(__builtins__, '__dict__', __builtins__)
for name in ('map', 'reduce', 'int', 'pow', 'range', 'filter', for name in ('map', 'reduce', 'int', 'pow', 'range', 'filter',
'len', 'chr', 'ord', 'len', 'chr', 'ord',
...@@ -250,6 +250,10 @@ class RestrictionTests(unittest.TestCase): ...@@ -250,6 +250,10 @@ class RestrictionTests(unittest.TestCase):
res = self.execFunc('rot13', 'Zope is k00l') res = self.execFunc('rot13', 'Zope is k00l')
assert (res == 'Mbcr vf x00y'), res assert (res == 'Mbcr vf x00y'), res
def checkNestedScopes1(self):
res = self.execFunc('nested_scopes_1')
assert (res == 2), res
def checkUnrestrictedEval(self): def checkUnrestrictedEval(self):
expr = RestrictionCapableEval("{'a':[m.pop()]}['a'] + [m[0]]") expr = RestrictionCapableEval("{'a':[m.pop()]}['a'] + [m[0]]")
v = [12, 34] v = [12, 34]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment