Use the "if 1:" prefix so that quoted code appears nicely
nested inside the test suite. def test_me(): exec("""if 1: ...code... """) No other change here.
This commit is contained in:
parent
4f5e298075
commit
dfa9b294fa
|
@ -192,44 +192,44 @@ class ScopeTests(unittest.TestCase):
|
||||||
|
|
||||||
def testUnoptimizedNamespaces(self):
|
def testUnoptimizedNamespaces(self):
|
||||||
|
|
||||||
check_syntax_error(self, """\
|
check_syntax_error(self, """if 1:
|
||||||
def unoptimized_clash1(strip):
|
def unoptimized_clash1(strip):
|
||||||
def f(s):
|
def f(s):
|
||||||
from sys import *
|
from sys import *
|
||||||
return getrefcount(s) # ambiguity: free or local
|
return getrefcount(s) # ambiguity: free or local
|
||||||
return f
|
return f
|
||||||
""")
|
""")
|
||||||
|
|
||||||
check_syntax_error(self, """\
|
check_syntax_error(self, """if 1:
|
||||||
def unoptimized_clash2():
|
def unoptimized_clash2():
|
||||||
from sys import *
|
from sys import *
|
||||||
def f(s):
|
def f(s):
|
||||||
return getrefcount(s) # ambiguity: global or local
|
return getrefcount(s) # ambiguity: global or local
|
||||||
return f
|
return f
|
||||||
""")
|
""")
|
||||||
|
|
||||||
check_syntax_error(self, """\
|
check_syntax_error(self, """if 1:
|
||||||
def unoptimized_clash2():
|
def unoptimized_clash2():
|
||||||
from sys import *
|
from sys import *
|
||||||
def g():
|
def g():
|
||||||
def f(s):
|
def f(s):
|
||||||
return getrefcount(s) # ambiguity: global or local
|
return getrefcount(s) # ambiguity: global or local
|
||||||
return f
|
return f
|
||||||
""")
|
""")
|
||||||
|
|
||||||
check_syntax_error(self, """\
|
check_syntax_error(self, """if 1:
|
||||||
def f(x):
|
def f(x):
|
||||||
def g():
|
def g():
|
||||||
return x
|
return x
|
||||||
del x # can't del name
|
del x # can't del name
|
||||||
""")
|
""")
|
||||||
|
|
||||||
check_syntax_error(self, """\
|
check_syntax_error(self, """if 1:
|
||||||
def f():
|
def f():
|
||||||
def g():
|
def g():
|
||||||
from sys import *
|
from sys import *
|
||||||
return getrefcount # global or local?
|
return getrefcount # global or local?
|
||||||
""")
|
""")
|
||||||
|
|
||||||
def testLambdas(self):
|
def testLambdas(self):
|
||||||
|
|
||||||
|
@ -273,17 +273,17 @@ def f():
|
||||||
self.assertRaises(NameError, errorInInner)
|
self.assertRaises(NameError, errorInInner)
|
||||||
|
|
||||||
# test for bug #1501934: incorrect LOAD/STORE_GLOBAL generation
|
# test for bug #1501934: incorrect LOAD/STORE_GLOBAL generation
|
||||||
exec("""
|
exec("""if 1:
|
||||||
global_x = 1
|
global_x = 1
|
||||||
def f():
|
def f():
|
||||||
global_x += 1
|
global_x += 1
|
||||||
try:
|
try:
|
||||||
f()
|
f()
|
||||||
except UnboundLocalError:
|
except UnboundLocalError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
fail('scope of global_x not correctly determined')
|
fail('scope of global_x not correctly determined')
|
||||||
""", {'fail': self.fail})
|
""", {'fail': self.fail})
|
||||||
|
|
||||||
def testComplexDefinitions(self):
|
def testComplexDefinitions(self):
|
||||||
|
|
||||||
|
@ -302,88 +302,88 @@ else:
|
||||||
self.assertEqual(makeReturner2(a=11)()['a'], 11)
|
self.assertEqual(makeReturner2(a=11)()['a'], 11)
|
||||||
|
|
||||||
def testScopeOfGlobalStmt(self):
|
def testScopeOfGlobalStmt(self):
|
||||||
# Examples posted by Samuele Pedroni to python-dev on 3/1/2001
|
# Examples posted by Samuele Pedroni to python-dev on 3/1/2001
|
||||||
|
|
||||||
exec("""\
|
exec("""if 1:
|
||||||
# I
|
# I
|
||||||
x = 7
|
x = 7
|
||||||
def f():
|
def f():
|
||||||
x = 1
|
x = 1
|
||||||
def g():
|
def g():
|
||||||
global x
|
global x
|
||||||
def i():
|
def i():
|
||||||
def h():
|
def h():
|
||||||
return x
|
return x
|
||||||
return h()
|
return h()
|
||||||
return i()
|
return i()
|
||||||
return g()
|
return g()
|
||||||
self.assertEqual(f(), 7)
|
self.assertEqual(f(), 7)
|
||||||
self.assertEqual(x, 7)
|
self.assertEqual(x, 7)
|
||||||
|
|
||||||
# II
|
# II
|
||||||
x = 7
|
x = 7
|
||||||
def f():
|
def f():
|
||||||
x = 1
|
x = 1
|
||||||
def g():
|
def g():
|
||||||
x = 2
|
x = 2
|
||||||
def i():
|
def i():
|
||||||
def h():
|
def h():
|
||||||
return x
|
return x
|
||||||
return h()
|
return h()
|
||||||
return i()
|
return i()
|
||||||
return g()
|
return g()
|
||||||
self.assertEqual(f(), 2)
|
self.assertEqual(f(), 2)
|
||||||
self.assertEqual(x, 7)
|
self.assertEqual(x, 7)
|
||||||
|
|
||||||
# III
|
# III
|
||||||
x = 7
|
x = 7
|
||||||
def f():
|
def f():
|
||||||
x = 1
|
x = 1
|
||||||
def g():
|
def g():
|
||||||
global x
|
global x
|
||||||
x = 2
|
x = 2
|
||||||
def i():
|
def i():
|
||||||
def h():
|
def h():
|
||||||
return x
|
return x
|
||||||
return h()
|
return h()
|
||||||
return i()
|
return i()
|
||||||
return g()
|
return g()
|
||||||
self.assertEqual(f(), 2)
|
self.assertEqual(f(), 2)
|
||||||
self.assertEqual(x, 2)
|
self.assertEqual(x, 2)
|
||||||
|
|
||||||
# IV
|
# IV
|
||||||
x = 7
|
x = 7
|
||||||
def f():
|
def f():
|
||||||
x = 3
|
x = 3
|
||||||
def g():
|
def g():
|
||||||
global x
|
global x
|
||||||
x = 2
|
x = 2
|
||||||
def i():
|
def i():
|
||||||
def h():
|
def h():
|
||||||
return x
|
return x
|
||||||
return h()
|
return h()
|
||||||
return i()
|
return i()
|
||||||
return g()
|
return g()
|
||||||
self.assertEqual(f(), 2)
|
self.assertEqual(f(), 2)
|
||||||
self.assertEqual(x, 2)
|
self.assertEqual(x, 2)
|
||||||
|
|
||||||
# XXX what about global statements in class blocks?
|
# XXX what about global statements in class blocks?
|
||||||
# do they affect methods?
|
# do they affect methods?
|
||||||
|
|
||||||
x = 12
|
x = 12
|
||||||
class Global:
|
class Global:
|
||||||
global x
|
global x
|
||||||
x = 13
|
x = 13
|
||||||
def set(self, val):
|
def set(self, val):
|
||||||
x = val
|
x = val
|
||||||
def get(self):
|
def get(self):
|
||||||
return x
|
return x
|
||||||
|
|
||||||
g = Global()
|
g = Global()
|
||||||
self.assertEqual(g.get(), 13)
|
self.assertEqual(g.get(), 13)
|
||||||
g.set(15)
|
g.set(15)
|
||||||
self.assertEqual(g.get(), 13)
|
self.assertEqual(g.get(), 13)
|
||||||
""")
|
""")
|
||||||
|
|
||||||
def testLeaks(self):
|
def testLeaks(self):
|
||||||
|
|
||||||
|
@ -409,28 +409,28 @@ self.assertEqual(g.get(), 13)
|
||||||
|
|
||||||
def testClassAndGlobal(self):
|
def testClassAndGlobal(self):
|
||||||
|
|
||||||
exec("""\
|
exec("""if 1:
|
||||||
def test(x):
|
def test(x):
|
||||||
class Foo:
|
class Foo:
|
||||||
global x
|
global x
|
||||||
def __call__(self, y):
|
def __call__(self, y):
|
||||||
return x + y
|
return x + y
|
||||||
return Foo()
|
return Foo()
|
||||||
|
|
||||||
x = 0
|
x = 0
|
||||||
self.assertEqual(test(6)(2), 8)
|
self.assertEqual(test(6)(2), 8)
|
||||||
x = -1
|
x = -1
|
||||||
self.assertEqual(test(3)(2), 5)
|
self.assertEqual(test(3)(2), 5)
|
||||||
|
|
||||||
looked_up_by_load_name = False
|
looked_up_by_load_name = False
|
||||||
class X:
|
class X:
|
||||||
# Implicit globals inside classes are be looked up by LOAD_NAME, not
|
# Implicit globals inside classes are be looked up by LOAD_NAME, not
|
||||||
# LOAD_GLOBAL.
|
# LOAD_GLOBAL.
|
||||||
locals()['looked_up_by_load_name'] = True
|
locals()['looked_up_by_load_name'] = True
|
||||||
passed = looked_up_by_load_name
|
passed = looked_up_by_load_name
|
||||||
|
|
||||||
self.assertTrue(X.passed)
|
self.assertTrue(X.passed)
|
||||||
""")
|
""")
|
||||||
|
|
||||||
def testLocalsFunction(self):
|
def testLocalsFunction(self):
|
||||||
|
|
||||||
|
@ -626,22 +626,22 @@ self.assertTrue(X.passed)
|
||||||
# function to other nested functions in the same block.
|
# function to other nested functions in the same block.
|
||||||
# This test verifies that a global statement in the first
|
# This test verifies that a global statement in the first
|
||||||
# function does not affect the second function.
|
# function does not affect the second function.
|
||||||
CODE = """def f():
|
|
||||||
y = 1
|
|
||||||
def g():
|
|
||||||
global y
|
|
||||||
return y
|
|
||||||
def h():
|
|
||||||
return y + 1
|
|
||||||
return g, h
|
|
||||||
y = 9
|
|
||||||
g, h = f()
|
|
||||||
result9 = g()
|
|
||||||
result2 = h()
|
|
||||||
"""
|
|
||||||
local_ns = {}
|
local_ns = {}
|
||||||
global_ns = {}
|
global_ns = {}
|
||||||
exec(CODE, local_ns, global_ns)
|
exec("""if 1:
|
||||||
|
def f():
|
||||||
|
y = 1
|
||||||
|
def g():
|
||||||
|
global y
|
||||||
|
return y
|
||||||
|
def h():
|
||||||
|
return y + 1
|
||||||
|
return g, h
|
||||||
|
y = 9
|
||||||
|
g, h = f()
|
||||||
|
result9 = g()
|
||||||
|
result2 = h()
|
||||||
|
""", local_ns, global_ns)
|
||||||
self.assertEqual(2, global_ns["result2"])
|
self.assertEqual(2, global_ns["result2"])
|
||||||
self.assertEqual(9, global_ns["result9"])
|
self.assertEqual(9, global_ns["result9"])
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue