Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
0bcba372
Commit
0bcba372
authored
Jan 04, 2018
by
Serhiy Storchaka
Committed by
GitHub
Jan 04, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-32482: Improve syntax and grammar tests. (#5085)
parent
eb08a929
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
397 additions
and
177 deletions
+397
-177
Lib/test/test_grammar.py
Lib/test/test_grammar.py
+140
-82
Lib/test/test_syntax.py
Lib/test/test_syntax.py
+253
-91
Lib/test/test_sys_settrace.py
Lib/test/test_sys_settrace.py
+4
-4
No files found.
Lib/test/test_grammar.py
View file @
0bcba372
...
...
@@ -11,7 +11,7 @@ from sys import *
class
TokenTests
(
unittest
.
TestCase
):
def
test
B
ackslash
(
self
):
def
test
_b
ackslash
(
self
):
# Backslash means line continuation:
x
=
1
\
+
1
...
...
@@ -21,29 +21,42 @@ class TokenTests(unittest.TestCase):
x
=
0
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
(
0377
,
255
)
self
.
assertEqual
(
0o377
,
255
)
self
.
assertEqual
(
2147483647
,
017777777777
)
self
.
assertEqual
(
2147483647
,
0o17777777777
)
self
.
assertEqual
(
0b1001
,
9
)
# "0x" is not a valid literal
self
.
assertRaises
(
SyntaxError
,
eval
,
"0x"
)
from
sys
import
maxint
if
maxint
==
2147483647
:
self
.
assertEqual
(
-
2147483647
-
1
,
-
020000000000
)
self
.
assertEqual
(
-
2147483647
-
1
,
-
0
o
20000000000
)
# XXX -2147483648
self
.
assertTrue
(
037777777777
>
0
)
self
.
assertTrue
(
0o37777777777
>
0
)
self
.
assertTrue
(
0xffffffff
>
0
)
for
s
in
'2147483648'
,
'040000000000'
,
'0x100000000'
:
self
.
assertTrue
(
0b1111111111111111111111111111111
>
0
)
for
s
in
(
'2147483648'
,
'040000000000'
,
'0o40000000000'
,
'0x100000000'
,
'0b10000000000000000000000000000000'
):
try
:
x
=
eval
(
s
)
except
OverflowError
:
self
.
fail
(
"OverflowError on huge integer literal %r"
%
s
)
elif
maxint
==
9223372036854775807
:
self
.
assertEqual
(
-
9223372036854775807
-
1
,
-
01000000000000000000000
)
self
.
assertEqual
(
-
9223372036854775807
-
1
,
-
0o1000000000000000000000
)
self
.
assertTrue
(
01777777777777777777777
>
0
)
self
.
assertTrue
(
0o1777777777777777777777
>
0
)
self
.
assertTrue
(
0xffffffffffffffff
>
0
)
self
.
assertTrue
(
0b11111111111111111111111111111111111111111111111111111111111111
>
0
)
for
s
in
'9223372036854775808'
,
'02000000000000000000000'
,
\
'0x10000000000000000'
:
'0o2000000000000000000000'
,
\
'0x10000000000000000'
,
\
'0b100000000000000000000000000000000000000000000000000000000000000'
:
try
:
x
=
eval
(
s
)
except
OverflowError
:
...
...
@@ -51,7 +64,7 @@ class TokenTests(unittest.TestCase):
else
:
self
.
fail
(
'Weird maxint value %r'
%
maxint
)
def
test
LongI
ntegers
(
self
):
def
test
_long_i
ntegers
(
self
):
x
=
0L
x
=
0
l
x
=
0xffffffffffffffff
L
...
...
@@ -61,7 +74,7 @@ class TokenTests(unittest.TestCase):
x
=
123456789012345678901234567890L
x
=
123456789012345678901234567890
l
def
test
F
loats
(
self
):
def
test
_f
loats
(
self
):
x
=
3.14
x
=
314.
x
=
0.314
...
...
@@ -81,7 +94,7 @@ class TokenTests(unittest.TestCase):
self
.
assertEqual
(
1
if
0
else
0
,
0
)
self
.
assertRaises
(
SyntaxError
,
eval
,
"0 if 1Else 0"
)
def
test
StringL
iterals
(
self
):
def
test
_string_l
iterals
(
self
):
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
)
==
34
)
...
...
@@ -133,11 +146,11 @@ class GrammarTests(unittest.TestCase):
# expr_input: testlist NEWLINE
# XXX Hard to test -- used only in calls to input()
def
test
EvalI
nput
(
self
):
def
test
_eval_i
nput
(
self
):
# testlist ENDMARKER
x
=
eval
(
'1, 0 or 1'
)
def
test
F
uncdef
(
self
):
def
test
_f
uncdef
(
self
):
### 'def' NAME parameters ':' suite
### parameters: '(' [varargslist] ')'
### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
...
...
@@ -153,7 +166,8 @@ class GrammarTests(unittest.TestCase):
f1
(
*
(),
**
{})
def
f2
(
one_argument
):
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 f5((compound, first), two): pass'
)
self
.
assertEqual
(
f2
.
func_code
.
co_varnames
,
(
'one_argument'
,))
...
...
@@ -174,7 +188,8 @@ class GrammarTests(unittest.TestCase):
def
v0
(
*
rest
):
pass
def
v1
(
a
,
*
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'
)
f1
()
...
...
@@ -212,7 +227,9 @@ class GrammarTests(unittest.TestCase):
d01
()
d01
(
1
)
d01
(
*
(
1
,))
d01
(
*
[]
or
[
2
])
d01
(
**
{
'a'
:
2
})
d01
(
**
{
'a'
:
2
}
or
{})
def
d11
(
a
,
b
=
1
):
pass
d11
(
1
)
d11
(
1
,
2
)
...
...
@@ -280,8 +297,11 @@ class GrammarTests(unittest.TestCase):
d22v
(
*
(
1
,
2
,
3
,
4
))
d22v
(
1
,
2
,
*
(
3
,
4
,
5
))
d22v
(
1
,
*
(
2
,
3
),
**
{
'd'
:
4
})
# Silence Py3k warning
with
check_py3k_warnings
((
'parenthesized argument names are invalid'
,
SyntaxWarning
)):
exec
(
'def d31v((x)): pass'
)
with
check_py3k_warnings
((
'tuple parameter unpacking has been removed'
,
SyntaxWarning
)):
exec
(
'def d32v((x,)): pass'
)
d31v
(
1
)
d32v
((
1
,))
...
...
@@ -293,12 +313,19 @@ class GrammarTests(unittest.TestCase):
{
'x'
:
2
,
'y'
:
5
}))
self
.
assertRaises
(
SyntaxError
,
eval
,
"f(1, *(2,3), 4)"
)
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_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
l1
=
lambda
:
0
self
.
assertEqual
(
l1
(),
0
)
...
...
@@ -311,12 +338,18 @@ class GrammarTests(unittest.TestCase):
self
.
assertEqual
(
l5
(
1
,
2
),
5
)
self
.
assertEqual
(
l5
(
1
,
2
,
3
),
6
)
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 that trailing commas are permitted
l10
=
lambda
a
,:
0
### stmt: simple_stmt | compound_stmt
# Tested below
def
test
SimpleS
tmt
(
self
):
def
test
_simple_s
tmt
(
self
):
### simple_stmt: small_stmt (';' small_stmt)* [';']
x
=
1
;
pass
;
del
x
def
foo
():
...
...
@@ -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
# Tested below
def
test
ExprS
tmt
(
self
):
def
test
_expr_s
tmt
(
self
):
# (exprlist '=')* exprlist
1
1
,
2
,
3
...
...
@@ -340,7 +373,7 @@ class GrammarTests(unittest.TestCase):
check_syntax_error
(
self
,
"x + 1 = 1"
)
check_syntax_error
(
self
,
"a + 1 = b + 2"
)
def
test
PrintS
tmt
(
self
):
def
test
_print_s
tmt
(
self
):
# 'print' (test ',')* [test]
import
StringIO
...
...
@@ -410,7 +443,7 @@ hello world
check_syntax_error
(
self
,
'print ,'
)
check_syntax_error
(
self
,
'print >> x,'
)
def
test
DelS
tmt
(
self
):
def
test
_del_s
tmt
(
self
):
# 'del' exprlist
abc
=
[
1
,
2
,
3
]
x
,
y
,
z
=
abc
...
...
@@ -419,18 +452,18 @@ hello world
del
abc
del
x
,
y
,
(
z
,
xyz
)
def
test
PassS
tmt
(
self
):
def
test
_pass_s
tmt
(
self
):
# 'pass'
pass
# flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
# Tested below
def
test
BreakS
tmt
(
self
):
def
test
_break_s
tmt
(
self
):
# 'break'
while
1
:
break
def
test
ContinueS
tmt
(
self
):
def
test
_continue_s
tmt
(
self
):
# 'continue'
i
=
1
while
i
:
i
=
0
;
continue
...
...
@@ -482,7 +515,7 @@ hello world
self
.
fail
(
"continue then break in try/except in loop broken!"
)
test_inner
()
def
test
R
eturn
(
self
):
def
test
_r
eturn
(
self
):
# 'return' [testlist]
def
g1
():
return
def
g2
():
return
1
...
...
@@ -513,7 +546,7 @@ hello world
while
count
<
2
:
count
+=
1
try
:
1
/
0
1
.0
/
0.
0
finally
:
break
self
.
assertEqual
(
count
,
1
)
...
...
@@ -537,7 +570,7 @@ hello world
for
count
in
[
0
,
1
]:
self
.
assertEqual
(
count
,
0
)
try
:
1
/
0
1
.0
/
0.
0
finally
:
break
self
.
assertEqual
(
count
,
0
)
...
...
@@ -559,13 +592,33 @@ hello world
def
g3
():
try
:
1
/
0
1
.0
/
0.
0
finally
:
return
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 annotation refleak on SyntaxError
check_syntax_error
(
self
,
"def g(a:(yield)): pass"
)
def
test_yield_in_comprehensions
(
self
):
# Check yield in comprehensions
...
...
@@ -607,14 +660,14 @@ hello world
check
(
"((yield x) for x in ())"
,
"'yield' inside generator expression"
)
def
test
R
aise
(
self
):
def
test
_r
aise
(
self
):
# 'raise' test [',' test]
try
:
raise
RuntimeError
,
'just testing'
except
RuntimeError
:
pass
try
:
raise
KeyboardInterrupt
except
KeyboardInterrupt
:
pass
def
test
I
mport
(
self
):
def
test
_i
mport
(
self
):
# 'import' dotted_as_names
import
sys
import
time
,
sys
...
...
@@ -627,13 +680,13 @@ hello world
from
sys
import
(
path
,
argv
)
from
sys
import
(
path
,
argv
,)
def
test
G
lobal
(
self
):
def
test
_g
lobal
(
self
):
# 'global' NAME (',' NAME)*
global
a
global
a
,
b
global
one
,
two
,
three
,
four
,
five
,
six
,
seven
,
eight
,
nine
,
ten
def
test
E
xec
(
self
):
def
test
_e
xec
(
self
):
# 'exec' expr ['in' expr [',' expr]]
z
=
None
del
z
...
...
@@ -665,7 +718,7 @@ hello world
if
(
g
,
l
)
!=
({
'a'
:
1
},
{
'b'
:
2
}):
self
.
fail
(
'exec ... in g (%s), l (%s)'
%
(
g
,
l
))
def
test
A
ssert
(
self
):
def
test
_a
ssert
(
self
):
# assertTruestmt: 'assert' test [',' test]
assert
1
assert
1
,
1
...
...
@@ -704,7 +757,7 @@ hello world
### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
# Tested below
def
test
I
f
(
self
):
def
test
_i
f
(
self
):
# 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
if
1
:
pass
if
1
:
pass
...
...
@@ -717,7 +770,7 @@ hello world
elif
0
:
pass
else
:
pass
def
test
W
hile
(
self
):
def
test
_w
hile
(
self
):
# 'while' test ':' suite ['else' ':' suite]
while
0
:
pass
while
0
:
pass
...
...
@@ -732,7 +785,7 @@ hello world
x
=
2
self
.
assertEqual
(
x
,
2
)
def
test
F
or
(
self
):
def
test
_f
or
(
self
):
# 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
for
i
in
1
,
2
,
3
:
pass
for
i
,
j
,
k
in
():
pass
...
...
@@ -759,30 +812,30 @@ hello world
result
.
append
(
x
)
self
.
assertEqual
(
result
,
[
1
,
2
,
3
])
def
test
T
ry
(
self
):
def
test
_t
ry
(
self
):
### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
### | 'try' ':' suite 'finally' ':' suite
### except_clause: 'except' [expr [('as' | ',') expr]]
try
:
1
/
0
1
/
0
.0
except
ZeroDivisionError
:
pass
else
:
pass
try
:
1
/
0
try
:
1
/
0
.0
except
EOFError
:
pass
except
TypeError
as
msg
:
pass
except
RuntimeError
,
msg
:
pass
except
:
pass
else
:
pass
try
:
1
/
0
try
:
1
/
0
.0
except
(
EOFError
,
TypeError
,
ZeroDivisionError
):
pass
try
:
1
/
0
try
:
1
/
0
.0
except
(
EOFError
,
TypeError
,
ZeroDivisionError
),
msg
:
pass
try
:
pass
finally
:
pass
def
test
S
uite
(
self
):
def
test
_s
uite
(
self
):
# simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
if
1
:
pass
if
1
:
...
...
@@ -797,7 +850,7 @@ hello world
pass
#
def
test
T
est
(
self
):
def
test
_t
est
(
self
):
### and_test ('or' and_test)*
### and_test: not_test ('and' not_test)*
### not_test: 'not' not_test | comparison
...
...
@@ -808,7 +861,7 @@ hello world
if
not
1
and
1
and
1
:
pass
if
1
and
1
or
1
and
1
and
1
or
not
1
and
1
:
pass
def
test
C
omparison
(
self
):
def
test
_c
omparison
(
self
):
### comparison: expr (comp_op expr)*
### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
if
1
:
pass
...
...
@@ -824,40 +877,49 @@ hello world
if
1
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
# Silence Py3k warning
with
check_py3k_warnings
((
'<> not supported in 3.x; use !='
,
DeprecationWarning
)):
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
def
test
BinaryMaskO
ps
(
self
):
def
test
_binary_mask_o
ps
(
self
):
x
=
1
&
1
x
=
1
^
1
x
=
1
|
1
def
test
ShiftO
ps
(
self
):
def
test
_shift_o
ps
(
self
):
x
=
1
<<
1
x
=
1
>>
1
x
=
1
<<
1
>>
1
def
test
AdditiveO
ps
(
self
):
def
test
_additive_o
ps
(
self
):
x
=
1
x
=
1
+
1
x
=
1
-
1
-
1
x
=
1
-
1
+
1
-
1
+
1
def
test
MultiplicativeO
ps
(
self
):
def
test
_multiplicative_o
ps
(
self
):
x
=
1
*
1
with
check_py3k_warnings
((
'classic int division'
,
DeprecationWarning
)):
x
=
1
/
1
x
=
1
/
1.0
x
=
1
%
1
with
check_py3k_warnings
((
'classic int division'
,
DeprecationWarning
)):
x
=
1
/
1
*
1
%
1
x
=
1
/
1.0
*
1
%
1
def
test
UnaryO
ps
(
self
):
def
test
_unary_o
ps
(
self
):
x
=
+
1
x
=
-
1
x
=
~
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.0
+
1
*
1
-
---
1
*
1
def
test
S
electors
(
self
):
def
test
_s
electors
(
self
):
### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
### subscript: expr | [expr] ':' [expr]
...
...
@@ -884,10 +946,10 @@ hello world
d
[
1
,
2
]
=
3
d
[
1
,
2
,
3
]
=
4
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)]'
)
def
test
A
toms
(
self
):
def
test
_a
toms
(
self
):
### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
### dictorsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
...
...
@@ -914,7 +976,7 @@ hello world
x
=
{
'one'
,
'two'
,
'three'
}
x
=
{
2
,
3
,
4
,}
# Silence Py3k warning
with
check_py3k_warnings
((
'backquote not supported'
,
SyntaxWarning
)):
x
=
eval
(
'`x`'
)
x
=
eval
(
'`1 or 2 or 3`'
)
self
.
assertEqual
(
eval
(
'`1,2`'
),
'(1, 2)'
)
...
...
@@ -927,7 +989,7 @@ hello world
### testlist: test (',' test)* [',']
# These have been exercised enough above
def
test
C
lassdef
(
self
):
def
test
_c
lassdef
(
self
):
# 'class' NAME ['(' [testlist] ')'] ':' suite
class
B
:
pass
class
B2
():
pass
...
...
@@ -938,6 +1000,7 @@ hello world
def
meth1
(
self
):
pass
def
meth2
(
self
,
arg
):
pass
def
meth3
(
self
,
a1
,
a2
):
pass
# decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
# decorators: decorator+
# decorated: decorators (classdef | funcdef)
...
...
@@ -949,14 +1012,14 @@ hello world
pass
self
.
assertEqual
(
G
.
decorated
,
True
)
def
test
D
ictcomps
(
self
):
def
test
_d
ictcomps
(
self
):
# dictorsetmaker: ( (test ':' test (comp_for |
# (',' test ':' test)* [','])) |
# (test (comp_for | (',' test)* [','])) )
nums
=
[
1
,
2
,
3
]
self
.
assertEqual
({
i
:
i
+
1
for
i
in
nums
},
{
1
:
2
,
2
:
3
,
3
:
4
})
def
test
L
istcomps
(
self
):
def
test
_l
istcomps
(
self
):
# list comprehension tests
nums
=
[
1
,
2
,
3
,
4
,
5
]
strs
=
[
"Apple"
,
"Banana"
,
"Coconut"
]
...
...
@@ -979,7 +1042,7 @@ hello world
[[
1
],
[
1
,
1
],
[
1
,
2
,
4
],
[
1
,
3
,
9
,
27
],
[
1
,
4
,
16
,
64
,
256
]])
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
])
...
...
@@ -1019,7 +1082,7 @@ hello world
self
.
assertEqual
(
x
,
[(
'Boeing'
,
'Airliner'
),
(
'Boeing'
,
'Engine'
),
(
'Ford'
,
'Engine'
),
(
'Macdonalds'
,
'Cheeseburger'
)])
def
test
G
enexps
(
self
):
def
test
_g
enexps
(
self
):
# generator expression tests
g
=
([
x
for
x
in
range
(
10
)]
for
x
in
range
(
1
))
self
.
assertEqual
(
g
.
next
(),
[
x
for
x
in
range
(
10
)])
...
...
@@ -1054,7 +1117,7 @@ hello world
check_syntax_error
(
self
,
"foo(x for x in range(10), 100)"
)
check_syntax_error
(
self
,
"foo(100, x for x in range(10))"
)
def
test
ComprehensionS
pecials
(
self
):
def
test
_comprehension_s
pecials
(
self
):
# test for outmost iterable precomputation
x
=
10
;
g
=
(
i
for
i
in
range
(
x
));
x
=
5
self
.
assertEqual
(
len
(
list
(
g
)),
10
)
...
...
@@ -1093,11 +1156,11 @@ hello world
with
manager
()
as
x
,
manager
():
pass
def
test
IfElseE
xpr
(
self
):
def
test
_if_else_e
xpr
(
self
):
# Test ifelse expressions in various cases
def
_checkeval
(
msg
,
ret
):
"helper to check that evaluation of expressions is done correctly"
print
x
print
(
msg
)
return
ret
self
.
assertEqual
([
x
()
for
x
in
lambda
:
True
,
lambda
:
False
if
x
()
],
[
True
])
...
...
@@ -1116,6 +1179,7 @@ hello world
self
.
assertEqual
((
6
+
1
if
1
else
2
),
7
)
self
.
assertEqual
((
6
-
1
if
1
else
2
),
5
)
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
<
4
if
0
else
2
),
2
)
...
...
@@ -1129,12 +1193,6 @@ hello world
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
)
if
__name__
==
'__main__'
:
...
...
Lib/test/test_syntax.py
View file @
0bcba372
...
...
@@ -73,13 +73,12 @@ SyntaxError: can't assign to literal
>>> "abc" = 1
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
>>>
`1`
= 1
>>>
b""
= 1
Traceback (most recent call last):
File "<doctest test.test_syntax[10]>", line 1
SyntaxError: can't assign to repr
SyntaxError: can't assign to literal
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.
...
...
@@ -148,7 +147,7 @@ SyntaxError: cannot assign to None
From ast_for_call():
>>> def f(it, *varargs):
>>> def f(it, *varargs
, **kwargs
):
... return list(it)
>>> L = range(10)
>>> f(x for x in L)
...
...
@@ -157,73 +156,179 @@ From ast_for_call():
Traceback (most recent call last):
File "<doctest test.test_syntax[23]>", line 1
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)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,
... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,
... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,
... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,
... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,
... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,
... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,
... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,
... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,
... i100, i101, i102, i103, i104, i105, i106, i107, i108,
... i109, i110, i111, i112, i113, i114, i115, i116, i117,
... i118, i119, i120, i121, i122, i123, i124, i125, i126,
... i127, i128, i129, i130, i131, i132, i133, i134, i135,
... i136, i137, i138, i139, i140, i141, i142, i143, i144,
... i145, i146, i147, i148, i149, i150, i151, i152, i153,
... i154, i155, i156, i157, i158, i159, i160, i161, i162,
... i163, i164, i165, i166, i167, i168, i169, i170, i171,
... i172, i173, i174, i175, i176, i177, i178, i179, i180,
... i181, i182, i183, i184, i185, i186, i187, i188, i189,
... i190, i191, i192, i193, i194, i195, i196, i197, i198,
... i199, i200, i201, i202, i203, i204, i205, i206, i207,
... i208, i209, i210, i211, i212, i213, i214, i215, i216,
... i217, i218, i219, i220, i221, i222, i223, i224, i225,
... i226, i227, i228, i229, i230, i231, i232, i233, i234,
... i235, i236, i237, i238, i239, i240, i241, i242, i243,
... i244, i245, i246, i247, i248, i249, i250, i251, i252,
... i253, i254, i255)
>>> def g(*args, **kwargs):
... print args, sorted(kwargs.items())
>>> g(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) []
>>> g(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[25]>", line 1
SyntaxError: more than 255 arguments
The actual error cases counts positional arguments, keyword arguments,
and generator expression arguments separately. This test combines the
three.
>>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11,
... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22,
... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33,
... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44,
... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55,
... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66,
... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77,
... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88,
... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99,
... i100, i101, i102, i103, i104, i105, i106, i107, i108,
... i109, i110, i111, i112, i113, i114, i115, i116, i117,
... i118, i119, i120, i121, i122, i123, i124, i125, i126,
... i127, i128, i129, i130, i131, i132, i133, i134, i135,
... i136, i137, i138, i139, i140, i141, i142, i143, i144,
... i145, i146, i147, i148, i149, i150, i151, i152, i153,
... i154, i155, i156, i157, i158, i159, i160, i161, i162,
... i163, i164, i165, i166, i167, i168, i169, i170, i171,
... i172, i173, i174, i175, i176, i177, i178, i179, i180,
... i181, i182, i183, i184, i185, i186, i187, i188, i189,
... i190, i191, i192, i193, i194, i195, i196, i197, i198,
... i199, i200, i201, i202, i203, i204, i205, i206, i207,
... i208, i209, i210, i211, i212, i213, i214, i215, i216,
... i217, i218, i219, i220, i221, i222, i223, i224, i225,
... i226, i227, i228, i229, i230, i231, i232, i233, i234,
... i235, i236, i237, i238, i239, i240, i241, i242, i243,
... (x for x in i244), i245, i246, i247, i248, i249, i250, i251,
... i252=1, i253=1, i254=1, i255=1)
Traceback (most recent call last):
File "<doctest test.test_syntax[26]>", line 1
>>> 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) # 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
>>> f(lambda x: x[0] = 3)
...
...
@@ -403,6 +508,16 @@ build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
...
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
a complex 'if' (one with 'elif') would fail to notice an invalid suite,
leading to spurious errors.
...
...
@@ -458,6 +573,18 @@ leading to spurious errors.
File "<doctest test.test_syntax[48]>", line 6
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)
Traceback (most recent call last):
...
...
...
@@ -476,6 +603,12 @@ Traceback (most recent call last):
File "<doctest test.test_syntax[50]>", line 1
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:
>>> def f(*xx, **__debug__): pass
...
...
@@ -488,12 +621,12 @@ import re
import
unittest
import
warnings
from
test
import
test_
support
from
test
import
support
class
SyntaxTestCase
(
unittest
.
TestCase
):
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.
errtest is a regular expression that must be present in the
...
...
@@ -501,13 +634,19 @@ class SyntaxTestCase(unittest.TestCase):
is the expected subclass of SyntaxError (e.g. IndentationError).
"""
try
:
compile
(
code
,
filename
,
mode
)
except
SyntaxError
,
err
:
compile
(
code
,
filename
or
"<testcase>"
,
mode
)
except
SyntaxError
as
err
:
if
subclass
and
not
isinstance
(
err
,
subclass
):
self
.
fail
(
"SyntaxError is not a %s"
%
subclass
.
__name__
)
mo
=
re
.
search
(
errtext
,
str
(
err
))
if
mo
is
None
:
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
:
self
.
fail
(
"compile() did not raise SyntaxError"
)
...
...
@@ -515,6 +654,11 @@ class SyntaxTestCase(unittest.TestCase):
self
.
_check_error
(
"def f((x)=23): pass"
,
"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
):
self
.
_check_error
(
"f() = 1"
,
"assign"
)
...
...
@@ -524,28 +668,48 @@ class SyntaxTestCase(unittest.TestCase):
def
test_global_err_then_warn
(
self
):
# Bug tickler: The SyntaxError raised for one global statement
# shouldn't be clobbered by a SyntaxWarning issued for a later one.
source
=
re
.
sub
(
'(?m)^ *:'
,
''
,
"""
\
:def error(a):
: global a # SyntaxError
:def warning():
: b = 1
: global b # SyntaxWarning
:"""
)
warnings
.
filterwarnings
(
action
=
'ignore'
,
category
=
SyntaxWarning
)
self
.
_check_error
(
source
,
"global"
)
warnings
.
filters
.
pop
(
0
)
source
=
"""if 1:
def error(a):
global a # SyntaxError
def warning():
b = 1
global b # SyntaxWarning
"""
with
support
.
check_warnings
((
".*assigned to before global declaration"
,
SyntaxWarning
)):
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
):
self
.
_check_error
(
"break"
,
"outside loop"
)
def
test_delete_deref
(
self
):
source
=
re
.
sub
(
'(?m)^ *:'
,
''
,
"""
\
:
def foo(x):
:
def bar():
: print x
:
del x
:"""
)
self
.
_check_error
(
source
,
"nested scope"
)
source
=
"""if 1:
def foo(x):
def bar():
print(x)
del x
"""
self
.
_check_error
(
source
,
"nested scope"
,
filename
=
None
)
def
test_unexpected_indent
(
self
):
self
.
_check_error
(
"foo()
\
n
bar()
\
n
"
,
"unexpected indent"
,
...
...
@@ -564,11 +728,9 @@ class SyntaxTestCase(unittest.TestCase):
self
.
_check_error
(
"int(base=10, '2')"
,
"non-keyword arg"
)
def
test_main
():
test_
support
.
run_unittest
(
SyntaxTestCase
)
support
.
run_unittest
(
SyntaxTestCase
)
from
test
import
test_syntax
with
test_support
.
check_py3k_warnings
((
"backquote not supported"
,
SyntaxWarning
)):
test_support
.
run_doctest
(
test_syntax
,
verbosity
=
True
)
support
.
run_doctest
(
test_syntax
,
verbosity
=
True
)
if
__name__
==
"__main__"
:
test_main
()
Lib/test/test_sys_settrace.py
View file @
0bcba372
...
...
@@ -619,7 +619,7 @@ class JumpTestCase(unittest.TestCase):
def
test_jump_in_nested_finally_2
(
output
):
try
:
output
.
append
(
2
)
1
/
0
1
.0
/
0.
0
return
finally
:
output
.
append
(
6
)
...
...
@@ -630,7 +630,7 @@ class JumpTestCase(unittest.TestCase):
def
test_jump_in_nested_finally_3
(
output
):
try
:
output
.
append
(
2
)
1
/
0
1
.0
/
0.
0
return
finally
:
output
.
append
(
6
)
...
...
@@ -697,7 +697,7 @@ class JumpTestCase(unittest.TestCase):
@
jump_test
(
5
,
7
,
[
4
,
7
,
8
])
def
test_jump_between_except_blocks
(
output
):
try
:
1
/
0
1
.0
/
0.
0
except
ZeroDivisionError
:
output
.
append
(
4
)
output
.
append
(
5
)
...
...
@@ -708,7 +708,7 @@ class JumpTestCase(unittest.TestCase):
@
jump_test
(
5
,
6
,
[
4
,
6
,
7
])
def
test_jump_within_except_block
(
output
):
try
:
1
/
0
1
.0
/
0.
0
except
:
output
.
append
(
4
)
output
.
append
(
5
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment