[Python-Dev] cpython: Issue #27999: Make "global after use" a SyntaxError, and ditto for nonlocal.

Christian Heimes christian at python.org
Mon Sep 12 05:37:36 EDT 2016


On 2016-09-09 18:53, guido.van.rossum wrote:
> https://hg.python.org/cpython/rev/804b71d43c85
> changeset:   103415:804b71d43c85
> user:        Guido van Rossum <guido at dropbox.com>
> date:        Fri Sep 09 09:36:26 2016 -0700
> summary:
>   Issue #27999: Make "global after use" a SyntaxError, and ditto for nonlocal.
> Patch by Ivan Levkivskyi.
> 
> files:
>   Doc/reference/simple_stmts.rst |    5 +-
>   Lib/test/test_syntax.py        |   18 +++-
>   Misc/NEWS                      |    3 +
>   Python/symtable.c              |  104 +++++++-------------
>   4 files changed, 59 insertions(+), 71 deletions(-)
> 

[...]

> @@ -1337,31 +1313,23 @@
>              long cur = symtable_lookup(st, name);
>              if (cur < 0)
>                  VISIT_QUIT(st, 0);
> -            if (cur & DEF_ANNOT) {
> -                PyErr_Format(PyExc_SyntaxError,
> -                             "annotated name '%U' can't be nonlocal",
> -                             name);
> +            if (cur & (DEF_LOCAL | USE | DEF_ANNOT)) {
> +                char* msg;
> +                if (cur & DEF_ANNOT) {
> +                    msg = NONLOCAL_ANNOT;
> +                }
> +                if (cur & DEF_LOCAL) {
> +                    msg = NONLOCAL_AFTER_ASSIGN;
> +                }
> +                else {
> +                    msg = NONLOCAL_AFTER_USE;
> +                }
> +                PyErr_Format(PyExc_SyntaxError, msg, name);

Hi Guido,

did you mean if / else if / else here? It's not completely clear if the
code means to set msg a second time if both cur & DEF_ANNOT and cur &
DEF_LOCAL are true.

Christian


More information about the Python-Dev mailing list