Index: Python/symtable.c =================================================================== --- Python/symtable.c (revision 85810) +++ Python/symtable.c (working copy) @@ -403,6 +403,8 @@ PyErr_Format(PyExc_SyntaxError, "name '%U' is nonlocal and global", name); + PyErr_SyntaxLocationEx(ste->ste_table->st_filename, + ste->ste_lineno, ste->ste_col_offset); return 0; } SET_SCOPE(scopes, name, GLOBAL_EXPLICIT); @@ -417,18 +419,24 @@ PyErr_Format(PyExc_SyntaxError, "name '%U' is parameter and nonlocal", name); + PyErr_SyntaxLocationEx(ste->ste_table->st_filename, + ste->ste_lineno, ste->ste_col_offset); return 0; } if (!bound) { PyErr_Format(PyExc_SyntaxError, - "nonlocal declaration not allowed at module level"); + "invalid nonlocal declaration for name '%U' " + "at module level", name); + PyErr_SyntaxLocationEx(ste->ste_table->st_filename, + ste->ste_lineno, ste->ste_col_offset); return 0; } if (!PySet_Contains(bound, name)) { PyErr_Format(PyExc_SyntaxError, "no binding for nonlocal '%U' found", name); - + PyErr_SyntaxLocationEx(ste->ste_table->st_filename, + ste->ste_lineno, ste->ste_col_offset); return 0; } SET_SCOPE(scopes, name, FREE); Index: Lib/test/test_syntax.py =================================================================== --- Lib/test/test_syntax.py (revision 85810) +++ Lib/test/test_syntax.py (working copy) @@ -390,7 +390,7 @@ >>> nonlocal x Traceback (most recent call last): ... - SyntaxError: nonlocal declaration not allowed at module level + SyntaxError: invalid nonlocal declaration for name 'x' at module level TODO(jhylton): Figure out how to test SyntaxWarning with doctest.