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
399b1fe8
Commit
399b1fe8
authored
Oct 25, 2008
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
give a py3k warning when 'nonlocal' is used as a variable name
parent
c756dcdd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
44 deletions
+34
-44
Lib/test/test_py3kwarn.py
Lib/test/test_py3kwarn.py
+23
-41
Misc/NEWS
Misc/NEWS
+3
-0
Python/ast.c
Python/ast.c
+8
-3
No files found.
Lib/test/test_py3kwarn.py
View file @
399b1fe8
...
...
@@ -28,53 +28,35 @@ class TestPy3KWarnings(unittest.TestCase):
exec
"`2`"
in
{}
self
.
assertWarning
(
None
,
w
,
expected
)
def
test_
bool_assign
(
self
):
def
test_
forbidden_names
(
self
):
# So we don't screw up our globals
def
safe_exec
(
expr
):
def
f
(
**
kwargs
):
pass
exec
expr
in
{
'f'
:
f
}
expected
=
"assignment to True or False is forbidden in 3.x"
tests
=
[(
"True"
,
"assignment to True or False is forbidden in 3.x"
),
(
"False"
,
"assignment to True or False is forbidden in 3.x"
),
(
"nonlocal"
,
"nonlocal is a keyword in 3.x"
)]
with
check_warnings
()
as
w
:
safe_exec
(
"True = False"
)
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
safe_exec
(
"False = True"
)
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
try
:
safe_exec
(
"obj.False = True"
)
except
NameError
:
pass
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
try
:
safe_exec
(
"obj.True = False"
)
except
NameError
:
pass
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
safe_exec
(
"def False(): pass"
)
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
safe_exec
(
"def True(): pass"
)
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
safe_exec
(
"class False: pass"
)
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
safe_exec
(
"class True: pass"
)
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
safe_exec
(
"def f(True=43): pass"
)
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
safe_exec
(
"def f(False=None): pass"
)
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
safe_exec
(
"f(False=True)"
)
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
safe_exec
(
"f(True=1)"
)
self
.
assertWarning
(
None
,
w
,
expected
)
for
keyword
,
expected
in
tests
:
safe_exec
(
"{0} = False"
.
format
(
keyword
))
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
try
:
safe_exec
(
"obj.{0} = True"
.
format
(
keyword
))
except
NameError
:
pass
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
safe_exec
(
"def {0}(): pass"
.
format
(
keyword
))
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
safe_exec
(
"class {0}: pass"
.
format
(
keyword
))
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
safe_exec
(
"def f({0}=43): pass"
.
format
(
keyword
))
self
.
assertWarning
(
None
,
w
,
expected
)
w
.
reset
()
def
test_type_inequality_comparisons
(
self
):
...
...
Misc/NEWS
View file @
399b1fe8
...
...
@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1
Core and Builtins
-----------------
- Using ``nonlocal`` as a variable name will now raise a Py3k SyntaxWarning
because it is a reserved work in 3.x.
- On windows, os.chdir given unicode was not working if GetCurrentDirectoryW
returned a path longer than MAX_PATH. (But It's doubtful this code path is
really executed because I cannot move to such directory on win2k)
...
...
Python/ast.c
View file @
399b1fe8
...
...
@@ -131,9 +131,14 @@ forbidden_check(struct compiling *c, const node *n, const char *x)
{
if
(
!
strcmp
(
x
,
"None"
))
return
ast_error
(
n
,
"assignment to None"
);
if
(
Py_Py3kWarningFlag
&&
!
(
strcmp
(
x
,
"True"
)
&&
strcmp
(
x
,
"False"
))
&&
!
ast_warn
(
c
,
n
,
"assignment to True or False is forbidden in 3.x"
))
return
0
;
if
(
Py_Py3kWarningFlag
)
{
if
(
!
(
strcmp
(
x
,
"True"
)
&&
strcmp
(
x
,
"False"
))
&&
!
ast_warn
(
c
,
n
,
"assignment to True or False is forbidden in 3.x"
))
return
0
;
if
(
!
strcmp
(
x
,
"nonlocal"
)
&&
!
ast_warn
(
c
,
n
,
"nonlocal is a keyword in 3.x"
))
return
0
;
}
return
1
;
}
...
...
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