Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
+ >>> def f():
+ ... print(x)
+ ... global x
+ Traceback (most recent call last):
+ ...
+ SyntaxError: name 'x' is used prior to global declaration
+
>>> def f():
... x = 1
... global x
...
SyntaxError: name 'x' is assigned to before global declaration
+ >>> def f(x):
+ ... global x
+ Traceback (most recent call last):
+ ...
+ SyntaxError: name 'x' is parameter and global
+
>>> def f():
... x = 1
... def g():
import re
import unittest
-import warnings
from test import support
def test_assign_del(self):
self._check_error("del f()", "delete")
- def test_global_err_then_warn(self):
- # Bug #763201: The SyntaxError raised for one global statement
- # shouldn't be clobbered by a SyntaxWarning issued for a later one.
+ def test_global_param_err_first(self):
source = """if 1:
def error(a):
global a # SyntaxError
- def warning():
+ def error2():
+ b = 1
+ global b # SyntaxError
+ """
+ self._check_error(source, "parameter and global", lineno=3)
+
+ def test_nonlocal_param_err_first(self):
+ source = """if 1:
+ def error(a):
+ nonlocal a # SyntaxError
+ def error2():
b = 1
- global b # SyntaxWarning
+ global b # SyntaxError
"""
- warnings.filterwarnings(action='ignore', category=SyntaxWarning)
- self._check_error(source, "global")
- warnings.filters.pop(0)
+ self._check_error(source, "parameter and nonlocal", lineno=3)
def test_break_outside_loop(self):
self._check_error("break", "outside loop")
#include "structmember.h"
/* error strings used for warnings */
+#define GLOBAL_PARAM \
+"name '%U' is parameter and global"
+
+#define NONLOCAL_PARAM \
+"name '%U' is parameter and nonlocal"
+
#define GLOBAL_AFTER_ASSIGN \
"name '%U' is assigned to before global declaration"
PyObject *global)
{
if (flags & DEF_GLOBAL) {
- if (flags & DEF_PARAM) {
- PyErr_Format(PyExc_SyntaxError,
- "name '%U' is parameter and global",
- name);
- return error_at_directive(ste, name);
- }
if (flags & DEF_NONLOCAL) {
PyErr_Format(PyExc_SyntaxError,
"name '%U' is nonlocal and global",
return 1;
}
if (flags & DEF_NONLOCAL) {
- if (flags & DEF_PARAM) {
- PyErr_Format(PyExc_SyntaxError,
- "name '%U' is parameter and nonlocal",
- name);
- return error_at_directive(ste, name);
- }
if (!bound) {
PyErr_Format(PyExc_SyntaxError,
"nonlocal declaration not allowed at module level");
long cur = symtable_lookup(st, name);
if (cur < 0)
VISIT_QUIT(st, 0);
- if (cur & (DEF_LOCAL | USE | DEF_ANNOT)) {
- char* msg;
- if (cur & USE) {
+ if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
+ const char* msg;
+ if (cur & DEF_PARAM) {
+ msg = GLOBAL_PARAM;
+ } else if (cur & USE) {
msg = GLOBAL_AFTER_USE;
} else if (cur & DEF_ANNOT) {
msg = GLOBAL_ANNOT;
long cur = symtable_lookup(st, name);
if (cur < 0)
VISIT_QUIT(st, 0);
- if (cur & (DEF_LOCAL | USE | DEF_ANNOT)) {
- char* msg;
- if (cur & USE) {
+ if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
+ const char* msg;
+ if (cur & DEF_PARAM) {
+ msg = NONLOCAL_PARAM;
+ } else if (cur & USE) {
msg = NONLOCAL_AFTER_USE;
} else if (cur & DEF_ANNOT) {
msg = NONLOCAL_ANNOT;