Commit 0bcba372 authored by Serhiy Storchaka's avatar Serhiy Storchaka Committed by GitHub

bpo-32482: Improve syntax and grammar tests. (#5085)

parent eb08a929
...@@ -11,7 +11,7 @@ from sys import * ...@@ -11,7 +11,7 @@ from sys import *
class TokenTests(unittest.TestCase): class TokenTests(unittest.TestCase):
def testBackslash(self): def test_backslash(self):
# Backslash means line continuation: # Backslash means line continuation:
x = 1 \ x = 1 \
+ 1 + 1
...@@ -21,29 +21,42 @@ class TokenTests(unittest.TestCase): ...@@ -21,29 +21,42 @@ class TokenTests(unittest.TestCase):
x = 0 x = 0
self.assertEqual(x, 0, 'backslash ending comment') self.assertEqual(x, 0, 'backslash ending comment')
def testPlainIntegers(self): def test_plain_integers(self):
self.assertEqual(type(000), type(0))
self.assertEqual(0xff, 255) self.assertEqual(0xff, 255)
self.assertEqual(0377, 255) self.assertEqual(0377, 255)
self.assertEqual(0o377, 255)
self.assertEqual(2147483647, 017777777777) self.assertEqual(2147483647, 017777777777)
self.assertEqual(2147483647, 0o17777777777)
self.assertEqual(0b1001, 9)
# "0x" is not a valid literal # "0x" is not a valid literal
self.assertRaises(SyntaxError, eval, "0x") self.assertRaises(SyntaxError, eval, "0x")
from sys import maxint from sys import maxint
if maxint == 2147483647: if maxint == 2147483647:
self.assertEqual(-2147483647-1, -020000000000) self.assertEqual(-2147483647-1, -0o20000000000)
# XXX -2147483648 # XXX -2147483648
self.assertTrue(037777777777 > 0) self.assertTrue(037777777777 > 0)
self.assertTrue(0o37777777777 > 0)
self.assertTrue(0xffffffff > 0) self.assertTrue(0xffffffff > 0)
for s in '2147483648', '040000000000', '0x100000000': self.assertTrue(0b1111111111111111111111111111111 > 0)
for s in ('2147483648', '040000000000', '0o40000000000',
'0x100000000',
'0b10000000000000000000000000000000'):
try: try:
x = eval(s) x = eval(s)
except OverflowError: except OverflowError:
self.fail("OverflowError on huge integer literal %r" % s) self.fail("OverflowError on huge integer literal %r" % s)
elif maxint == 9223372036854775807: elif maxint == 9223372036854775807:
self.assertEqual(-9223372036854775807-1, -01000000000000000000000) self.assertEqual(-9223372036854775807-1, -01000000000000000000000)
self.assertEqual(-9223372036854775807-1, -0o1000000000000000000000)
self.assertTrue(01777777777777777777777 > 0) self.assertTrue(01777777777777777777777 > 0)
self.assertTrue(0o1777777777777777777777 > 0)
self.assertTrue(0xffffffffffffffff > 0) self.assertTrue(0xffffffffffffffff > 0)
self.assertTrue(0b11111111111111111111111111111111111111111111111111111111111111 > 0)
for s in '9223372036854775808', '02000000000000000000000', \ for s in '9223372036854775808', '02000000000000000000000', \
'0x10000000000000000': '0o2000000000000000000000', \
'0x10000000000000000', \
'0b100000000000000000000000000000000000000000000000000000000000000':
try: try:
x = eval(s) x = eval(s)
except OverflowError: except OverflowError:
...@@ -51,7 +64,7 @@ class TokenTests(unittest.TestCase): ...@@ -51,7 +64,7 @@ class TokenTests(unittest.TestCase):
else: else:
self.fail('Weird maxint value %r' % maxint) self.fail('Weird maxint value %r' % maxint)
def testLongIntegers(self): def test_long_integers(self):
x = 0L x = 0L
x = 0l x = 0l
x = 0xffffffffffffffffL x = 0xffffffffffffffffL
...@@ -61,7 +74,7 @@ class TokenTests(unittest.TestCase): ...@@ -61,7 +74,7 @@ class TokenTests(unittest.TestCase):
x = 123456789012345678901234567890L x = 123456789012345678901234567890L
x = 123456789012345678901234567890l x = 123456789012345678901234567890l
def testFloats(self): def test_floats(self):
x = 3.14 x = 3.14
x = 314. x = 314.
x = 0.314 x = 0.314
...@@ -81,7 +94,7 @@ class TokenTests(unittest.TestCase): ...@@ -81,7 +94,7 @@ class TokenTests(unittest.TestCase):
self.assertEqual(1 if 0else 0, 0) self.assertEqual(1 if 0else 0, 0)
self.assertRaises(SyntaxError, eval, "0 if 1Else 0") self.assertRaises(SyntaxError, eval, "0 if 1Else 0")
def testStringLiterals(self): def test_string_literals(self):
x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y) x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39) x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)
x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34) x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34)
...@@ -133,11 +146,11 @@ class GrammarTests(unittest.TestCase): ...@@ -133,11 +146,11 @@ class GrammarTests(unittest.TestCase):
# expr_input: testlist NEWLINE # expr_input: testlist NEWLINE
# XXX Hard to test -- used only in calls to input() # XXX Hard to test -- used only in calls to input()
def testEvalInput(self): def test_eval_input(self):
# testlist ENDMARKER # testlist ENDMARKER
x = eval('1, 0 or 1') x = eval('1, 0 or 1')
def testFuncdef(self): def test_funcdef(self):
### 'def' NAME parameters ':' suite ### 'def' NAME parameters ':' suite
### parameters: '(' [varargslist] ')' ### parameters: '(' [varargslist] ')'
### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME] ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
...@@ -153,7 +166,8 @@ class GrammarTests(unittest.TestCase): ...@@ -153,7 +166,8 @@ class GrammarTests(unittest.TestCase):
f1(*(), **{}) f1(*(), **{})
def f2(one_argument): pass def f2(one_argument): pass
def f3(two, arguments): pass def f3(two, arguments): pass
# Silence Py3k warning with check_py3k_warnings(('tuple parameter unpacking has been removed',
SyntaxWarning)):
exec('def f4(two, (compound, (argument, list))): pass') exec('def f4(two, (compound, (argument, list))): pass')
exec('def f5((compound, first), two): pass') exec('def f5((compound, first), two): pass')
self.assertEqual(f2.func_code.co_varnames, ('one_argument',)) self.assertEqual(f2.func_code.co_varnames, ('one_argument',))
...@@ -174,7 +188,8 @@ class GrammarTests(unittest.TestCase): ...@@ -174,7 +188,8 @@ class GrammarTests(unittest.TestCase):
def v0(*rest): pass def v0(*rest): pass
def v1(a, *rest): pass def v1(a, *rest): pass
def v2(a, b, *rest): pass def v2(a, b, *rest): pass
# Silence Py3k warning with check_py3k_warnings(('tuple parameter unpacking has been removed',
SyntaxWarning)):
exec('def v3(a, (b, c), *rest): return a, b, c, rest') exec('def v3(a, (b, c), *rest): return a, b, c, rest')
f1() f1()
...@@ -212,7 +227,9 @@ class GrammarTests(unittest.TestCase): ...@@ -212,7 +227,9 @@ class GrammarTests(unittest.TestCase):
d01() d01()
d01(1) d01(1)
d01(*(1,)) d01(*(1,))
d01(*[] or [2])
d01(**{'a':2}) d01(**{'a':2})
d01(**{'a':2} or {})
def d11(a, b=1): pass def d11(a, b=1): pass
d11(1) d11(1)
d11(1, 2) d11(1, 2)
...@@ -280,8 +297,11 @@ class GrammarTests(unittest.TestCase): ...@@ -280,8 +297,11 @@ class GrammarTests(unittest.TestCase):
d22v(*(1, 2, 3, 4)) d22v(*(1, 2, 3, 4))
d22v(1, 2, *(3, 4, 5)) d22v(1, 2, *(3, 4, 5))
d22v(1, *(2, 3), **{'d': 4}) d22v(1, *(2, 3), **{'d': 4})
# Silence Py3k warning with check_py3k_warnings(('parenthesized argument names are invalid',
SyntaxWarning)):
exec('def d31v((x)): pass') exec('def d31v((x)): pass')
with check_py3k_warnings(('tuple parameter unpacking has been removed',
SyntaxWarning)):
exec('def d32v((x,)): pass') exec('def d32v((x,)): pass')
d31v(1) d31v(1)
d32v((1,)) d32v((1,))
...@@ -293,12 +313,19 @@ class GrammarTests(unittest.TestCase): ...@@ -293,12 +313,19 @@ class GrammarTests(unittest.TestCase):
{'x':2, 'y':5})) {'x':2, 'y':5}))
self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)") self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")
self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)") self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
self.assertEqual(f(**{'eggs':'scrambled', 'spam':'fried'}),
((), {'eggs':'scrambled', 'spam':'fried'}))
self.assertEqual(f(spam='fried', **{'eggs':'scrambled'}),
((), {'eggs':'scrambled', 'spam':'fried'}))
# Check ast errors in *args and *kwargs # Check ast errors in *args and *kwargs
check_syntax_error(self, "f(*g(1=2))") check_syntax_error(self, "f(*g(1=2))")
check_syntax_error(self, "f(**g(1=2))") check_syntax_error(self, "f(**g(1=2))")
def testLambdef(self): # Check trailing commas are permitted in funcdef argument list
def f(a,): pass
def test_lambdef(self):
### lambdef: 'lambda' [varargslist] ':' test ### lambdef: 'lambda' [varargslist] ':' test
l1 = lambda : 0 l1 = lambda : 0
self.assertEqual(l1(), 0) self.assertEqual(l1(), 0)
...@@ -311,12 +338,18 @@ class GrammarTests(unittest.TestCase): ...@@ -311,12 +338,18 @@ class GrammarTests(unittest.TestCase):
self.assertEqual(l5(1, 2), 5) self.assertEqual(l5(1, 2), 5)
self.assertEqual(l5(1, 2, 3), 6) self.assertEqual(l5(1, 2, 3), 6)
check_syntax_error(self, "lambda x: x = 2") check_syntax_error(self, "lambda x: x = 2")
with check_py3k_warnings(('tuple parameter unpacking has been removed',
SyntaxWarning)):
check_syntax_error(self, "lambda (None,): None") check_syntax_error(self, "lambda (None,): None")
# check that trailing commas are permitted
l10 = lambda a,: 0
### stmt: simple_stmt | compound_stmt ### stmt: simple_stmt | compound_stmt
# Tested below # Tested below
def testSimpleStmt(self): def test_simple_stmt(self):
### simple_stmt: small_stmt (';' small_stmt)* [';'] ### simple_stmt: small_stmt (';' small_stmt)* [';']
x = 1; pass; del x x = 1; pass; del x
def foo(): def foo():
...@@ -327,7 +360,7 @@ class GrammarTests(unittest.TestCase): ...@@ -327,7 +360,7 @@ class GrammarTests(unittest.TestCase):
### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
# Tested below # Tested below
def testExprStmt(self): def test_expr_stmt(self):
# (exprlist '=')* exprlist # (exprlist '=')* exprlist
1 1
1, 2, 3 1, 2, 3
...@@ -340,7 +373,7 @@ class GrammarTests(unittest.TestCase): ...@@ -340,7 +373,7 @@ class GrammarTests(unittest.TestCase):
check_syntax_error(self, "x + 1 = 1") check_syntax_error(self, "x + 1 = 1")
check_syntax_error(self, "a + 1 = b + 2") check_syntax_error(self, "a + 1 = b + 2")
def testPrintStmt(self): def test_print_stmt(self):
# 'print' (test ',')* [test] # 'print' (test ',')* [test]
import StringIO import StringIO
...@@ -410,7 +443,7 @@ hello world ...@@ -410,7 +443,7 @@ hello world
check_syntax_error(self, 'print ,') check_syntax_error(self, 'print ,')
check_syntax_error(self, 'print >> x,') check_syntax_error(self, 'print >> x,')
def testDelStmt(self): def test_del_stmt(self):
# 'del' exprlist # 'del' exprlist
abc = [1,2,3] abc = [1,2,3]
x, y, z = abc x, y, z = abc
...@@ -419,18 +452,18 @@ hello world ...@@ -419,18 +452,18 @@ hello world
del abc del abc
del x, y, (z, xyz) del x, y, (z, xyz)
def testPassStmt(self): def test_pass_stmt(self):
# 'pass' # 'pass'
pass pass
# flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
# Tested below # Tested below
def testBreakStmt(self): def test_break_stmt(self):
# 'break' # 'break'
while 1: break while 1: break
def testContinueStmt(self): def test_continue_stmt(self):
# 'continue' # 'continue'
i = 1 i = 1
while i: i = 0; continue while i: i = 0; continue
...@@ -482,7 +515,7 @@ hello world ...@@ -482,7 +515,7 @@ hello world
self.fail("continue then break in try/except in loop broken!") self.fail("continue then break in try/except in loop broken!")
test_inner() test_inner()
def testReturn(self): def test_return(self):
# 'return' [testlist] # 'return' [testlist]
def g1(): return def g1(): return
def g2(): return 1 def g2(): return 1
...@@ -513,7 +546,7 @@ hello world ...@@ -513,7 +546,7 @@ hello world
while count < 2: while count < 2:
count += 1 count += 1
try: try:
1/0 1.0/0.0
finally: finally:
break break
self.assertEqual(count, 1) self.assertEqual(count, 1)
...@@ -537,7 +570,7 @@ hello world ...@@ -537,7 +570,7 @@ hello world
for count in [0, 1]: for count in [0, 1]:
self.assertEqual(count, 0) self.assertEqual(count, 0)
try: try:
1/0 1.0/0.0
finally: finally:
break break
self.assertEqual(count, 0) self.assertEqual(count, 0)
...@@ -559,13 +592,33 @@ hello world ...@@ -559,13 +592,33 @@ hello world
def g3(): def g3():
try: try:
1/0 1.0/0.0
finally: finally:
return 4 return 4
self.assertEqual(g3(), 4) self.assertEqual(g3(), 4)
def testYield(self): def test_yield(self):
# Allowed as standalone statement
def g(): yield 1
# Allowed as RHS of assignment
def g(): x = yield 1
# Ordinary yield accepts implicit tuples
def g(): yield 1, 1
def g(): x = yield 1, 1
# Requires parentheses as subexpression
def g(): 1, (yield 1)
check_syntax_error(self, "def g(): 1, yield 1")
# Requires parentheses as call argument
def g(): f((yield 1))
def g(): f((yield 1), 1)
check_syntax_error(self, "def g(): f(yield 1)")
check_syntax_error(self, "def g(): f(yield 1, 1)")
# Not allowed at top level
check_syntax_error(self, "yield")
# Not allowed at class scope
check_syntax_error(self, "class foo:yield 1") check_syntax_error(self, "class foo:yield 1")
# Check annotation refleak on SyntaxError
check_syntax_error(self, "def g(a:(yield)): pass")
def test_yield_in_comprehensions(self): def test_yield_in_comprehensions(self):
# Check yield in comprehensions # Check yield in comprehensions
...@@ -607,14 +660,14 @@ hello world ...@@ -607,14 +660,14 @@ hello world
check("((yield x) for x in ())", check("((yield x) for x in ())",
"'yield' inside generator expression") "'yield' inside generator expression")
def testRaise(self): def test_raise(self):
# 'raise' test [',' test] # 'raise' test [',' test]
try: raise RuntimeError, 'just testing' try: raise RuntimeError, 'just testing'
except RuntimeError: pass except RuntimeError: pass
try: raise KeyboardInterrupt try: raise KeyboardInterrupt
except KeyboardInterrupt: pass except KeyboardInterrupt: pass
def testImport(self): def test_import(self):
# 'import' dotted_as_names # 'import' dotted_as_names
import sys import sys
import time, sys import time, sys
...@@ -627,13 +680,13 @@ hello world ...@@ -627,13 +680,13 @@ hello world
from sys import (path, argv) from sys import (path, argv)
from sys import (path, argv,) from sys import (path, argv,)
def testGlobal(self): def test_global(self):
# 'global' NAME (',' NAME)* # 'global' NAME (',' NAME)*
global a global a
global a, b global a, b
global one, two, three, four, five, six, seven, eight, nine, ten global one, two, three, four, five, six, seven, eight, nine, ten
def testExec(self): def test_exec(self):
# 'exec' expr ['in' expr [',' expr]] # 'exec' expr ['in' expr [',' expr]]
z = None z = None
del z del z
...@@ -665,7 +718,7 @@ hello world ...@@ -665,7 +718,7 @@ hello world
if (g, l) != ({'a':1}, {'b':2}): if (g, l) != ({'a':1}, {'b':2}):
self.fail('exec ... in g (%s), l (%s)' %(g,l)) self.fail('exec ... in g (%s), l (%s)' %(g,l))
def testAssert(self): def test_assert(self):
# assertTruestmt: 'assert' test [',' test] # assertTruestmt: 'assert' test [',' test]
assert 1 assert 1
assert 1, 1 assert 1, 1
...@@ -704,7 +757,7 @@ hello world ...@@ -704,7 +757,7 @@ hello world
### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
# Tested below # Tested below
def testIf(self): def test_if(self):
# 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
if 1: pass if 1: pass
if 1: pass if 1: pass
...@@ -717,7 +770,7 @@ hello world ...@@ -717,7 +770,7 @@ hello world
elif 0: pass elif 0: pass
else: pass else: pass
def testWhile(self): def test_while(self):
# 'while' test ':' suite ['else' ':' suite] # 'while' test ':' suite ['else' ':' suite]
while 0: pass while 0: pass
while 0: pass while 0: pass
...@@ -732,7 +785,7 @@ hello world ...@@ -732,7 +785,7 @@ hello world
x = 2 x = 2
self.assertEqual(x, 2) self.assertEqual(x, 2)
def testFor(self): def test_for(self):
# 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
for i in 1, 2, 3: pass for i in 1, 2, 3: pass
for i, j, k in (): pass for i, j, k in (): pass
...@@ -759,30 +812,30 @@ hello world ...@@ -759,30 +812,30 @@ hello world
result.append(x) result.append(x)
self.assertEqual(result, [1, 2, 3]) self.assertEqual(result, [1, 2, 3])
def testTry(self): def test_try(self):
### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
### | 'try' ':' suite 'finally' ':' suite ### | 'try' ':' suite 'finally' ':' suite
### except_clause: 'except' [expr [('as' | ',') expr]] ### except_clause: 'except' [expr [('as' | ',') expr]]
try: try:
1/0 1/0.0
except ZeroDivisionError: except ZeroDivisionError:
pass pass
else: else:
pass pass
try: 1/0 try: 1/0.0
except EOFError: pass except EOFError: pass
except TypeError as msg: pass except TypeError as msg: pass
except RuntimeError, msg: pass except RuntimeError, msg: pass
except: pass except: pass
else: pass else: pass
try: 1/0 try: 1/0.0
except (EOFError, TypeError, ZeroDivisionError): pass except (EOFError, TypeError, ZeroDivisionError): pass
try: 1/0 try: 1/0.0
except (EOFError, TypeError, ZeroDivisionError), msg: pass except (EOFError, TypeError, ZeroDivisionError), msg: pass
try: pass try: pass
finally: pass finally: pass
def testSuite(self): def test_suite(self):
# simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
if 1: pass if 1: pass
if 1: if 1:
...@@ -797,7 +850,7 @@ hello world ...@@ -797,7 +850,7 @@ hello world
pass pass
# #
def testTest(self): def test_test(self):
### and_test ('or' and_test)* ### and_test ('or' and_test)*
### and_test: not_test ('and' not_test)* ### and_test: not_test ('and' not_test)*
### not_test: 'not' not_test | comparison ### not_test: 'not' not_test | comparison
...@@ -808,7 +861,7 @@ hello world ...@@ -808,7 +861,7 @@ hello world
if not 1 and 1 and 1: pass if not 1 and 1 and 1: pass
if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
def testComparison(self): def test_comparison(self):
### comparison: expr (comp_op expr)* ### comparison: expr (comp_op expr)*
### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
if 1: pass if 1: pass
...@@ -824,40 +877,49 @@ hello world ...@@ -824,40 +877,49 @@ hello world
if 1 in (): pass if 1 in (): pass
if 1 not in (): pass if 1 not in (): pass
if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass
# Silence Py3k warning with check_py3k_warnings(('<> not supported in 3.x; use !=',
DeprecationWarning)):
if eval('1 <> 1'): pass if eval('1 <> 1'): pass
with check_py3k_warnings(('<> not supported in 3.x; use !=',
DeprecationWarning)):
if eval('1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1'): pass if eval('1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1'): pass
def testBinaryMaskOps(self): def test_binary_mask_ops(self):
x = 1 & 1 x = 1 & 1
x = 1 ^ 1 x = 1 ^ 1
x = 1 | 1 x = 1 | 1
def testShiftOps(self): def test_shift_ops(self):
x = 1 << 1 x = 1 << 1
x = 1 >> 1 x = 1 >> 1
x = 1 << 1 >> 1 x = 1 << 1 >> 1
def testAdditiveOps(self): def test_additive_ops(self):
x = 1 x = 1
x = 1 + 1 x = 1 + 1
x = 1 - 1 - 1 x = 1 - 1 - 1
x = 1 - 1 + 1 - 1 + 1 x = 1 - 1 + 1 - 1 + 1
def testMultiplicativeOps(self): def test_multiplicative_ops(self):
x = 1 * 1 x = 1 * 1
with check_py3k_warnings(('classic int division', DeprecationWarning)):
x = 1 / 1 x = 1 / 1
x = 1 / 1.0
x = 1 % 1 x = 1 % 1
with check_py3k_warnings(('classic int division', DeprecationWarning)):
x = 1 / 1 * 1 % 1 x = 1 / 1 * 1 % 1
x = 1 / 1.0 * 1 % 1
def testUnaryOps(self): def test_unary_ops(self):
x = +1 x = +1
x = -1 x = -1
x = ~1 x = ~1
x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
with check_py3k_warnings(('classic int division', DeprecationWarning)):
x = -1*1/1 + 1*1 - ---1*1 x = -1*1/1 + 1*1 - ---1*1
x = -1*1/1.0 + 1*1 - ---1*1
def testSelectors(self): def test_selectors(self):
### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
### subscript: expr | [expr] ':' [expr] ### subscript: expr | [expr] ':' [expr]
...@@ -884,10 +946,10 @@ hello world ...@@ -884,10 +946,10 @@ hello world
d[1,2] = 3 d[1,2] = 3
d[1,2,3] = 4 d[1,2,3] = 4
L = list(d) L = list(d)
L.sort() L.sort(key=lambda x: (type(x).__name__, x))
self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
def testAtoms(self): def test_atoms(self):
### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
### dictorsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [',']) ### dictorsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
...@@ -914,7 +976,7 @@ hello world ...@@ -914,7 +976,7 @@ hello world
x = {'one', 'two', 'three'} x = {'one', 'two', 'three'}
x = {2, 3, 4,} x = {2, 3, 4,}
# Silence Py3k warning with check_py3k_warnings(('backquote not supported', SyntaxWarning)):
x = eval('`x`') x = eval('`x`')
x = eval('`1 or 2 or 3`') x = eval('`1 or 2 or 3`')
self.assertEqual(eval('`1,2`'), '(1, 2)') self.assertEqual(eval('`1,2`'), '(1, 2)')
...@@ -927,7 +989,7 @@ hello world ...@@ -927,7 +989,7 @@ hello world
### testlist: test (',' test)* [','] ### testlist: test (',' test)* [',']
# These have been exercised enough above # These have been exercised enough above
def testClassdef(self): def test_classdef(self):
# 'class' NAME ['(' [testlist] ')'] ':' suite # 'class' NAME ['(' [testlist] ')'] ':' suite
class B: pass class B: pass
class B2(): pass class B2(): pass
...@@ -938,6 +1000,7 @@ hello world ...@@ -938,6 +1000,7 @@ hello world
def meth1(self): pass def meth1(self): pass
def meth2(self, arg): pass def meth2(self, arg): pass
def meth3(self, a1, a2): pass def meth3(self, a1, a2): pass
# decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
# decorators: decorator+ # decorators: decorator+
# decorated: decorators (classdef | funcdef) # decorated: decorators (classdef | funcdef)
...@@ -949,14 +1012,14 @@ hello world ...@@ -949,14 +1012,14 @@ hello world
pass pass
self.assertEqual(G.decorated, True) self.assertEqual(G.decorated, True)
def testDictcomps(self): def test_dictcomps(self):
# dictorsetmaker: ( (test ':' test (comp_for | # dictorsetmaker: ( (test ':' test (comp_for |
# (',' test ':' test)* [','])) | # (',' test ':' test)* [','])) |
# (test (comp_for | (',' test)* [','])) ) # (test (comp_for | (',' test)* [','])) )
nums = [1, 2, 3] nums = [1, 2, 3]
self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4}) self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
def testListcomps(self): def test_listcomps(self):
# list comprehension tests # list comprehension tests
nums = [1, 2, 3, 4, 5] nums = [1, 2, 3, 4, 5]
strs = ["Apple", "Banana", "Coconut"] strs = ["Apple", "Banana", "Coconut"]
...@@ -979,7 +1042,7 @@ hello world ...@@ -979,7 +1042,7 @@ hello world
[[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]) [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
def test_in_func(l): def test_in_func(l):
return [None < x < 3 for x in l if x > 2] return [0 < x < 3 for x in l if x > 2]
self.assertEqual(test_in_func(nums), [False, False, False]) self.assertEqual(test_in_func(nums), [False, False, False])
...@@ -1019,7 +1082,7 @@ hello world ...@@ -1019,7 +1082,7 @@ hello world
self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
('Macdonalds', 'Cheeseburger')]) ('Macdonalds', 'Cheeseburger')])
def testGenexps(self): def test_genexps(self):
# generator expression tests # generator expression tests
g = ([x for x in range(10)] for x in range(1)) g = ([x for x in range(10)] for x in range(1))
self.assertEqual(g.next(), [x for x in range(10)]) self.assertEqual(g.next(), [x for x in range(10)])
...@@ -1054,7 +1117,7 @@ hello world ...@@ -1054,7 +1117,7 @@ hello world
check_syntax_error(self, "foo(x for x in range(10), 100)") check_syntax_error(self, "foo(x for x in range(10), 100)")
check_syntax_error(self, "foo(100, x for x in range(10))") check_syntax_error(self, "foo(100, x for x in range(10))")
def testComprehensionSpecials(self): def test_comprehension_specials(self):
# test for outmost iterable precomputation # test for outmost iterable precomputation
x = 10; g = (i for i in range(x)); x = 5 x = 10; g = (i for i in range(x)); x = 5
self.assertEqual(len(list(g)), 10) self.assertEqual(len(list(g)), 10)
...@@ -1093,11 +1156,11 @@ hello world ...@@ -1093,11 +1156,11 @@ hello world
with manager() as x, manager(): with manager() as x, manager():
pass pass
def testIfElseExpr(self): def test_if_else_expr(self):
# Test ifelse expressions in various cases # Test ifelse expressions in various cases
def _checkeval(msg, ret): def _checkeval(msg, ret):
"helper to check that evaluation of expressions is done correctly" "helper to check that evaluation of expressions is done correctly"
print x print(msg)
return ret return ret
self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True]) self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
...@@ -1116,6 +1179,7 @@ hello world ...@@ -1116,6 +1179,7 @@ hello world
self.assertEqual((6 + 1 if 1 else 2), 7) self.assertEqual((6 + 1 if 1 else 2), 7)
self.assertEqual((6 - 1 if 1 else 2), 5) self.assertEqual((6 - 1 if 1 else 2), 5)
self.assertEqual((6 * 2 if 1 else 4), 12) self.assertEqual((6 * 2 if 1 else 4), 12)
with check_py3k_warnings(('classic int division', DeprecationWarning)):
self.assertEqual((6 / 2 if 1 else 3), 3) self.assertEqual((6 / 2 if 1 else 3), 3)
self.assertEqual((6 < 4 if 0 else 2), 2) self.assertEqual((6 < 4 if 0 else 2), 2)
...@@ -1129,12 +1193,6 @@ hello world ...@@ -1129,12 +1193,6 @@ hello world
def test_main(): def test_main():
with check_py3k_warnings(
("backquote not supported", SyntaxWarning),
("tuple parameter unpacking has been removed", SyntaxWarning),
("parenthesized argument names are invalid", SyntaxWarning),
("classic int division", DeprecationWarning),
(".+ not supported in 3.x", DeprecationWarning)):
run_unittest(TokenTests, GrammarTests) run_unittest(TokenTests, GrammarTests)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -73,13 +73,12 @@ SyntaxError: can't assign to literal ...@@ -73,13 +73,12 @@ SyntaxError: can't assign to literal
>>> "abc" = 1 >>> "abc" = 1
Traceback (most recent call last): Traceback (most recent call last):
File "<doctest test.test_syntax[8]>", line 1 File "<doctest test.test_syntax[9]>", line 1
SyntaxError: can't assign to literal SyntaxError: can't assign to literal
>>> `1` = 1 >>> b"" = 1
Traceback (most recent call last): Traceback (most recent call last):
File "<doctest test.test_syntax[10]>", line 1 SyntaxError: can't assign to literal
SyntaxError: can't assign to repr
If the left-hand side of an assignment is a list or tuple, an illegal If the left-hand side of an assignment is a list or tuple, an illegal
expression inside that contain should still cause a syntax error. expression inside that contain should still cause a syntax error.
...@@ -148,7 +147,7 @@ SyntaxError: cannot assign to None ...@@ -148,7 +147,7 @@ SyntaxError: cannot assign to None
From ast_for_call(): From ast_for_call():
>>> def f(it, *varargs): >>> def f(it, *varargs, **kwargs):
... return list(it) ... return list(it)
>>> L = range(10) >>> L = range(10)
>>> f(x for x in L) >>> f(x for x in L)
...@@ -157,73 +156,179 @@ From ast_for_call(): ...@@ -157,73 +156,179 @@ From ast_for_call():
Traceback (most recent call last): Traceback (most recent call last):
File "<doctest test.test_syntax[23]>", line 1 File "<doctest test.test_syntax[23]>", line 1
SyntaxError: Generator expression must be parenthesized if not sole argument SyntaxError: Generator expression must be parenthesized if not sole argument
>>> f(x for x in L, y=1)
Traceback (most recent call last):
SyntaxError: Generator expression must be parenthesized if not sole argument
>>> f(L, x for x in L)
Traceback (most recent call last):
SyntaxError: Generator expression must be parenthesized if not sole argument
>>> f(x for x in L, y for y in L)
Traceback (most recent call last):
SyntaxError: Generator expression must be parenthesized if not sole argument
>>> f(x for x in L,)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> f((x for x in L), 1) >>> f((x for x in L), 1)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, >>> def g(*args, **kwargs):
... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22, ... print args, sorted(kwargs.items())
... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, >>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44, ... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55, ... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66, ... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77, ... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88, ... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99, ... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
... i100, i101, i102, i103, i104, i105, i106, i107, i108, ... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
... i109, i110, i111, i112, i113, i114, i115, i116, i117, ... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
... i118, i119, i120, i121, i122, i123, i124, i125, i126, ... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
... i127, i128, i129, i130, i131, i132, i133, i134, i135, ... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
... i136, i137, i138, i139, i140, i141, i142, i143, i144, ... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
... i145, i146, i147, i148, i149, i150, i151, i152, i153, ... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
... i154, i155, i156, i157, i158, i159, i160, i161, i162, ... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
... i163, i164, i165, i166, i167, i168, i169, i170, i171, ... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
... i172, i173, i174, i175, i176, i177, i178, i179, i180, ... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
... i181, i182, i183, i184, i185, i186, i187, i188, i189, ... 248, 249, 250, 251, 252, 253, 254) # doctest: +ELLIPSIS
... i190, i191, i192, i193, i194, i195, i196, i197, i198, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 252, 253, 254) []
... i199, i200, i201, i202, i203, i204, i205, i206, i207, >>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
... i208, i209, i210, i211, i212, i213, i214, i215, i216, ... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
... i217, i218, i219, i220, i221, i222, i223, i224, i225, ... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
... i226, i227, i228, i229, i230, i231, i232, i233, i234, ... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
... i235, i236, i237, i238, i239, i240, i241, i242, i243, ... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
... i244, i245, i246, i247, i248, i249, i250, i251, i252, ... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
... i253, i254, i255) ... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
... 248, 249, 250, 251, 252, 253, 254, 255)
Traceback (most recent call last): Traceback (most recent call last):
File "<doctest test.test_syntax[25]>", line 1 File "<doctest test.test_syntax[25]>", line 1
SyntaxError: more than 255 arguments SyntaxError: more than 255 arguments
The actual error cases counts positional arguments, keyword arguments, >>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
and generator expression arguments separately. This test combines the ... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
three. ... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, ... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22, ... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, ... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44, ... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55, ... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66, ... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77, ... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88, ... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99, ... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
... i100, i101, i102, i103, i104, i105, i106, i107, i108, ... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
... i109, i110, i111, i112, i113, i114, i115, i116, i117, ... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
... i118, i119, i120, i121, i122, i123, i124, i125, i126, ... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
... i127, i128, i129, i130, i131, i132, i133, i134, i135, ... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
... i136, i137, i138, i139, i140, i141, i142, i143, i144, ... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
... i145, i146, i147, i148, i149, i150, i151, i152, i153, ... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
... i154, i155, i156, i157, i158, i159, i160, i161, i162, ... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
... i163, i164, i165, i166, i167, i168, i169, i170, i171, ... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
... i172, i173, i174, i175, i176, i177, i178, i179, i180, ... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
... i181, i182, i183, i184, i185, i186, i187, i188, i189, ... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
... i190, i191, i192, i193, i194, i195, i196, i197, i198, ... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
... i199, i200, i201, i202, i203, i204, i205, i206, i207, ... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
... i208, i209, i210, i211, i212, i213, i214, i215, i216, ... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
... i217, i218, i219, i220, i221, i222, i223, i224, i225, ... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
... i226, i227, i228, i229, i230, i231, i232, i233, i234, ... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
... i235, i236, i237, i238, i239, i240, i241, i242, i243, ... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
... (x for x in i244), i245, i246, i247, i248, i249, i250, i251, ... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
... i252=1, i253=1, i254=1, i255=1) ... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
Traceback (most recent call last): ... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
File "<doctest test.test_syntax[26]>", line 1 ... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
... a251=251, a252=252, a253=253, a254=254) # doctest: +ELLIPSIS
() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a253', 253), ('a254', 254)]
>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
... a251=251, a252=252, a253=253, a254=254, a255=255)
Traceback (most recent call last):
File "<doctest test.test_syntax[35]>", line 1
SyntaxError: more than 255 arguments
>>> class C:
... def meth(self, *args):
... return args
>>> obj = C()
>>> obj.meth(
... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
... 248, 249, 250, 251, 252, 253, 254) # doctest: +ELLIPSIS
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 252, 253, 254)
>>> obj.meth(
... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
... 248, 249, 250, 251, 252, 253, 254, 255)
Traceback (most recent call last):
File "<doctest test.test_syntax[38]>", line 1
SyntaxError: more than 255 arguments SyntaxError: more than 255 arguments
>>> f(lambda x: x[0] = 3) >>> f(lambda x: x[0] = 3)
...@@ -403,6 +508,16 @@ build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514 ...@@ -403,6 +508,16 @@ build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
... ...
SyntaxError: too many statically nested blocks SyntaxError: too many statically nested blocks
Misuse of the global statement can lead to a few unique syntax errors.
>>> def f(x):
... global x
... # doctest: +ELLIPSIS
Traceback (most recent call last):
...
SyntaxError: name 'x' is local and global (<doctest ...>, line 1)
This tests assignment-context; there was a bug in Python 2.5 where compiling This tests assignment-context; there was a bug in Python 2.5 where compiling
a complex 'if' (one with 'elif') would fail to notice an invalid suite, a complex 'if' (one with 'elif') would fail to notice an invalid suite,
leading to spurious errors. leading to spurious errors.
...@@ -458,6 +573,18 @@ leading to spurious errors. ...@@ -458,6 +573,18 @@ leading to spurious errors.
File "<doctest test.test_syntax[48]>", line 6 File "<doctest test.test_syntax[48]>", line 6
SyntaxError: can't assign to function call SyntaxError: can't assign to function call
Test the "raise X, Y[, Z]" form:
>>> raise ArithmeticError, 'bad number'
Traceback (most recent call last):
...
ArithmeticError: bad number
>>> raise ArithmeticError, 'bad number', None
Traceback (most recent call last):
...
ArithmeticError: bad number
>>> f(a=23, a=234) >>> f(a=23, a=234)
Traceback (most recent call last): Traceback (most recent call last):
... ...
...@@ -476,6 +603,12 @@ Traceback (most recent call last): ...@@ -476,6 +603,12 @@ Traceback (most recent call last):
File "<doctest test.test_syntax[50]>", line 1 File "<doctest test.test_syntax[50]>", line 1
SyntaxError: can't assign to literal SyntaxError: can't assign to literal
Corner-case that used to fail to raise the correct error:
>>> def f(x=lambda __debug__:0): pass
Traceback (most recent call last):
SyntaxError: cannot assign to __debug__
Corner-case that used to crash: Corner-case that used to crash:
>>> def f(*xx, **__debug__): pass >>> def f(*xx, **__debug__): pass
...@@ -488,12 +621,12 @@ import re ...@@ -488,12 +621,12 @@ import re
import unittest import unittest
import warnings import warnings
from test import test_support from test import support
class SyntaxTestCase(unittest.TestCase): class SyntaxTestCase(unittest.TestCase):
def _check_error(self, code, errtext, def _check_error(self, code, errtext,
filename="<testcase>", mode="exec", subclass=None): filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
"""Check that compiling code raises SyntaxError with errtext. """Check that compiling code raises SyntaxError with errtext.
errtest is a regular expression that must be present in the errtest is a regular expression that must be present in the
...@@ -501,13 +634,19 @@ class SyntaxTestCase(unittest.TestCase): ...@@ -501,13 +634,19 @@ class SyntaxTestCase(unittest.TestCase):
is the expected subclass of SyntaxError (e.g. IndentationError). is the expected subclass of SyntaxError (e.g. IndentationError).
""" """
try: try:
compile(code, filename, mode) compile(code, filename or "<testcase>", mode)
except SyntaxError, err: except SyntaxError as err:
if subclass and not isinstance(err, subclass): if subclass and not isinstance(err, subclass):
self.fail("SyntaxError is not a %s" % subclass.__name__) self.fail("SyntaxError is not a %s" % subclass.__name__)
mo = re.search(errtext, str(err)) mo = re.search(errtext, str(err))
if mo is None: if mo is None:
self.fail("%s did not contain '%r'" % (err, errtext,)) self.fail("%s did not contain '%r'" % (err, errtext,))
if filename is not None:
self.assertEqual(err.filename, filename)
if lineno is not None:
self.assertEqual(err.lineno, lineno)
if offset is not None:
self.assertEqual(err.offset, offset)
else: else:
self.fail("compile() did not raise SyntaxError") self.fail("compile() did not raise SyntaxError")
...@@ -515,6 +654,11 @@ class SyntaxTestCase(unittest.TestCase): ...@@ -515,6 +654,11 @@ class SyntaxTestCase(unittest.TestCase):
self._check_error("def f((x)=23): pass", self._check_error("def f((x)=23): pass",
"parenthesized arg with default") "parenthesized arg with default")
def test_assign_repr(self):
with support.check_py3k_warnings(('backquote not supported',
SyntaxWarning)):
self._check_error("`1` = 1", "assign to repr")
def test_assign_call(self): def test_assign_call(self):
self._check_error("f() = 1", "assign") self._check_error("f() = 1", "assign")
...@@ -524,28 +668,48 @@ class SyntaxTestCase(unittest.TestCase): ...@@ -524,28 +668,48 @@ class SyntaxTestCase(unittest.TestCase):
def test_global_err_then_warn(self): def test_global_err_then_warn(self):
# Bug tickler: The SyntaxError raised for one global statement # Bug tickler: The SyntaxError raised for one global statement
# shouldn't be clobbered by a SyntaxWarning issued for a later one. # shouldn't be clobbered by a SyntaxWarning issued for a later one.
source = re.sub('(?m)^ *:', '', """\ source = """if 1:
:def error(a): def error(a):
: global a # SyntaxError global a # SyntaxError
:def warning(): def warning():
: b = 1 b = 1
: global b # SyntaxWarning global b # SyntaxWarning
:""") """
warnings.filterwarnings(action='ignore', category=SyntaxWarning) with support.check_warnings((".*assigned to before global declaration",
self._check_error(source, "global") SyntaxWarning)):
warnings.filters.pop(0) self._check_error(source, "local and global", lineno=2)
def test_misuse_global(self):
source = """if 1:
def f():
print(x)
global x
"""
with support.check_warnings(('.*used prior to global declaration',
SyntaxWarning)):
compile(source, '<testcase>', 'exec')
def test_misuse_global_2(self):
source = """if 1:
def f():
x = 1
global x
"""
with support.check_warnings(('.*assigned to before global declaration',
SyntaxWarning)):
compile(source, '<testcase>', 'exec')
def test_break_outside_loop(self): def test_break_outside_loop(self):
self._check_error("break", "outside loop") self._check_error("break", "outside loop")
def test_delete_deref(self): def test_delete_deref(self):
source = re.sub('(?m)^ *:', '', """\ source = """if 1:
:def foo(x): def foo(x):
: def bar(): def bar():
: print x print(x)
: del x del x
:""") """
self._check_error(source, "nested scope") self._check_error(source, "nested scope", filename=None)
def test_unexpected_indent(self): def test_unexpected_indent(self):
self._check_error("foo()\n bar()\n", "unexpected indent", self._check_error("foo()\n bar()\n", "unexpected indent",
...@@ -564,11 +728,9 @@ class SyntaxTestCase(unittest.TestCase): ...@@ -564,11 +728,9 @@ class SyntaxTestCase(unittest.TestCase):
self._check_error("int(base=10, '2')", "non-keyword arg") self._check_error("int(base=10, '2')", "non-keyword arg")
def test_main(): def test_main():
test_support.run_unittest(SyntaxTestCase) support.run_unittest(SyntaxTestCase)
from test import test_syntax from test import test_syntax
with test_support.check_py3k_warnings(("backquote not supported", support.run_doctest(test_syntax, verbosity=True)
SyntaxWarning)):
test_support.run_doctest(test_syntax, verbosity=True)
if __name__ == "__main__": if __name__ == "__main__":
test_main() test_main()
...@@ -619,7 +619,7 @@ class JumpTestCase(unittest.TestCase): ...@@ -619,7 +619,7 @@ class JumpTestCase(unittest.TestCase):
def test_jump_in_nested_finally_2(output): def test_jump_in_nested_finally_2(output):
try: try:
output.append(2) output.append(2)
1/0 1.0/0.0
return return
finally: finally:
output.append(6) output.append(6)
...@@ -630,7 +630,7 @@ class JumpTestCase(unittest.TestCase): ...@@ -630,7 +630,7 @@ class JumpTestCase(unittest.TestCase):
def test_jump_in_nested_finally_3(output): def test_jump_in_nested_finally_3(output):
try: try:
output.append(2) output.append(2)
1/0 1.0/0.0
return return
finally: finally:
output.append(6) output.append(6)
...@@ -697,7 +697,7 @@ class JumpTestCase(unittest.TestCase): ...@@ -697,7 +697,7 @@ class JumpTestCase(unittest.TestCase):
@jump_test(5, 7, [4, 7, 8]) @jump_test(5, 7, [4, 7, 8])
def test_jump_between_except_blocks(output): def test_jump_between_except_blocks(output):
try: try:
1/0 1.0/0.0
except ZeroDivisionError: except ZeroDivisionError:
output.append(4) output.append(4)
output.append(5) output.append(5)
...@@ -708,7 +708,7 @@ class JumpTestCase(unittest.TestCase): ...@@ -708,7 +708,7 @@ class JumpTestCase(unittest.TestCase):
@jump_test(5, 6, [4, 6, 7]) @jump_test(5, 6, [4, 6, 7])
def test_jump_within_except_block(output): def test_jump_within_except_block(output):
try: try:
1/0 1.0/0.0
except: except:
output.append(4) output.append(4)
output.append(5) output.append(5)
......
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