mirror of https://github.com/python/cpython
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):
|
||||
|
||||
check_syntax_error(self, """\
|
||||
def unoptimized_clash1(strip):
|
||||
def f(s):
|
||||
from sys import *
|
||||
return getrefcount(s) # ambiguity: free or local
|
||||
return f
|
||||
""")
|
||||
check_syntax_error(self, """if 1:
|
||||
def unoptimized_clash1(strip):
|
||||
def f(s):
|
||||
from sys import *
|
||||
return getrefcount(s) # ambiguity: free or local
|
||||
return f
|
||||
""")
|
||||
|
||||
check_syntax_error(self, """\
|
||||
def unoptimized_clash2():
|
||||
from sys import *
|
||||
def f(s):
|
||||
return getrefcount(s) # ambiguity: global or local
|
||||
return f
|
||||
""")
|
||||
check_syntax_error(self, """if 1:
|
||||
def unoptimized_clash2():
|
||||
from sys import *
|
||||
def f(s):
|
||||
return getrefcount(s) # ambiguity: global or local
|
||||
return f
|
||||
""")
|
||||
|
||||
check_syntax_error(self, """\
|
||||
def unoptimized_clash2():
|
||||
from sys import *
|
||||
def g():
|
||||
def f(s):
|
||||
return getrefcount(s) # ambiguity: global or local
|
||||
return f
|
||||
""")
|
||||
check_syntax_error(self, """if 1:
|
||||
def unoptimized_clash2():
|
||||
from sys import *
|
||||
def g():
|
||||
def f(s):
|
||||
return getrefcount(s) # ambiguity: global or local
|
||||
return f
|
||||
""")
|
||||
|
||||
check_syntax_error(self, """\
|
||||
def f(x):
|
||||
def g():
|
||||
return x
|
||||
del x # can't del name
|
||||
""")
|
||||
check_syntax_error(self, """if 1:
|
||||
def f(x):
|
||||
def g():
|
||||
return x
|
||||
del x # can't del name
|
||||
""")
|
||||
|
||||
check_syntax_error(self, """\
|
||||
def f():
|
||||
def g():
|
||||
from sys import *
|
||||
return getrefcount # global or local?
|
||||
""")
|
||||
check_syntax_error(self, """if 1:
|
||||
def f():
|
||||
def g():
|
||||
from sys import *
|
||||
return getrefcount # global or local?
|
||||
""")
|
||||
|
||||
def testLambdas(self):
|
||||
|
||||
|
@ -273,17 +273,17 @@ def f():
|
|||
self.assertRaises(NameError, errorInInner)
|
||||
|
||||
# test for bug #1501934: incorrect LOAD/STORE_GLOBAL generation
|
||||
exec("""
|
||||
global_x = 1
|
||||
def f():
|
||||
global_x += 1
|
||||
try:
|
||||
f()
|
||||
except UnboundLocalError:
|
||||
pass
|
||||
else:
|
||||
fail('scope of global_x not correctly determined')
|
||||
""", {'fail': self.fail})
|
||||
exec("""if 1:
|
||||
global_x = 1
|
||||
def f():
|
||||
global_x += 1
|
||||
try:
|
||||
f()
|
||||
except UnboundLocalError:
|
||||
pass
|
||||
else:
|
||||
fail('scope of global_x not correctly determined')
|
||||
""", {'fail': self.fail})
|
||||
|
||||
def testComplexDefinitions(self):
|
||||
|
||||
|
@ -302,88 +302,88 @@ else:
|
|||
self.assertEqual(makeReturner2(a=11)()['a'], 11)
|
||||
|
||||
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("""\
|
||||
# I
|
||||
x = 7
|
||||
def f():
|
||||
x = 1
|
||||
def g():
|
||||
global x
|
||||
def i():
|
||||
def h():
|
||||
return x
|
||||
return h()
|
||||
return i()
|
||||
return g()
|
||||
self.assertEqual(f(), 7)
|
||||
self.assertEqual(x, 7)
|
||||
exec("""if 1:
|
||||
# I
|
||||
x = 7
|
||||
def f():
|
||||
x = 1
|
||||
def g():
|
||||
global x
|
||||
def i():
|
||||
def h():
|
||||
return x
|
||||
return h()
|
||||
return i()
|
||||
return g()
|
||||
self.assertEqual(f(), 7)
|
||||
self.assertEqual(x, 7)
|
||||
|
||||
# II
|
||||
x = 7
|
||||
def f():
|
||||
x = 1
|
||||
def g():
|
||||
x = 2
|
||||
def i():
|
||||
def h():
|
||||
return x
|
||||
return h()
|
||||
return i()
|
||||
return g()
|
||||
self.assertEqual(f(), 2)
|
||||
self.assertEqual(x, 7)
|
||||
# II
|
||||
x = 7
|
||||
def f():
|
||||
x = 1
|
||||
def g():
|
||||
x = 2
|
||||
def i():
|
||||
def h():
|
||||
return x
|
||||
return h()
|
||||
return i()
|
||||
return g()
|
||||
self.assertEqual(f(), 2)
|
||||
self.assertEqual(x, 7)
|
||||
|
||||
# III
|
||||
x = 7
|
||||
def f():
|
||||
x = 1
|
||||
def g():
|
||||
global x
|
||||
x = 2
|
||||
def i():
|
||||
def h():
|
||||
return x
|
||||
return h()
|
||||
return i()
|
||||
return g()
|
||||
self.assertEqual(f(), 2)
|
||||
self.assertEqual(x, 2)
|
||||
# III
|
||||
x = 7
|
||||
def f():
|
||||
x = 1
|
||||
def g():
|
||||
global x
|
||||
x = 2
|
||||
def i():
|
||||
def h():
|
||||
return x
|
||||
return h()
|
||||
return i()
|
||||
return g()
|
||||
self.assertEqual(f(), 2)
|
||||
self.assertEqual(x, 2)
|
||||
|
||||
# IV
|
||||
x = 7
|
||||
def f():
|
||||
x = 3
|
||||
def g():
|
||||
global x
|
||||
x = 2
|
||||
def i():
|
||||
def h():
|
||||
return x
|
||||
return h()
|
||||
return i()
|
||||
return g()
|
||||
self.assertEqual(f(), 2)
|
||||
self.assertEqual(x, 2)
|
||||
# IV
|
||||
x = 7
|
||||
def f():
|
||||
x = 3
|
||||
def g():
|
||||
global x
|
||||
x = 2
|
||||
def i():
|
||||
def h():
|
||||
return x
|
||||
return h()
|
||||
return i()
|
||||
return g()
|
||||
self.assertEqual(f(), 2)
|
||||
self.assertEqual(x, 2)
|
||||
|
||||
# XXX what about global statements in class blocks?
|
||||
# do they affect methods?
|
||||
# XXX what about global statements in class blocks?
|
||||
# do they affect methods?
|
||||
|
||||
x = 12
|
||||
class Global:
|
||||
global x
|
||||
x = 13
|
||||
def set(self, val):
|
||||
x = val
|
||||
def get(self):
|
||||
return x
|
||||
x = 12
|
||||
class Global:
|
||||
global x
|
||||
x = 13
|
||||
def set(self, val):
|
||||
x = val
|
||||
def get(self):
|
||||
return x
|
||||
|
||||
g = Global()
|
||||
self.assertEqual(g.get(), 13)
|
||||
g.set(15)
|
||||
self.assertEqual(g.get(), 13)
|
||||
""")
|
||||
g = Global()
|
||||
self.assertEqual(g.get(), 13)
|
||||
g.set(15)
|
||||
self.assertEqual(g.get(), 13)
|
||||
""")
|
||||
|
||||
def testLeaks(self):
|
||||
|
||||
|
@ -409,28 +409,28 @@ self.assertEqual(g.get(), 13)
|
|||
|
||||
def testClassAndGlobal(self):
|
||||
|
||||
exec("""\
|
||||
def test(x):
|
||||
class Foo:
|
||||
global x
|
||||
def __call__(self, y):
|
||||
return x + y
|
||||
return Foo()
|
||||
exec("""if 1:
|
||||
def test(x):
|
||||
class Foo:
|
||||
global x
|
||||
def __call__(self, y):
|
||||
return x + y
|
||||
return Foo()
|
||||
|
||||
x = 0
|
||||
self.assertEqual(test(6)(2), 8)
|
||||
x = -1
|
||||
self.assertEqual(test(3)(2), 5)
|
||||
x = 0
|
||||
self.assertEqual(test(6)(2), 8)
|
||||
x = -1
|
||||
self.assertEqual(test(3)(2), 5)
|
||||
|
||||
looked_up_by_load_name = False
|
||||
class X:
|
||||
# Implicit globals inside classes are be looked up by LOAD_NAME, not
|
||||
# LOAD_GLOBAL.
|
||||
locals()['looked_up_by_load_name'] = True
|
||||
passed = looked_up_by_load_name
|
||||
looked_up_by_load_name = False
|
||||
class X:
|
||||
# Implicit globals inside classes are be looked up by LOAD_NAME, not
|
||||
# LOAD_GLOBAL.
|
||||
locals()['looked_up_by_load_name'] = True
|
||||
passed = looked_up_by_load_name
|
||||
|
||||
self.assertTrue(X.passed)
|
||||
""")
|
||||
self.assertTrue(X.passed)
|
||||
""")
|
||||
|
||||
def testLocalsFunction(self):
|
||||
|
||||
|
@ -626,22 +626,22 @@ self.assertTrue(X.passed)
|
|||
# function to other nested functions in the same block.
|
||||
# This test verifies that a global statement in the first
|
||||
# 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 = {}
|
||||
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(9, global_ns["result9"])
|
||||
|
||||
|
|
Loading…
Reference in New Issue