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

Ivan Levkivskyi levkivskyi at gmail.com
Mon Sep 12 05:46:12 EDT 2016


Christian,

When I wrote this, my intention was like: cur & DEF_LOCAL is a "more
serious" error, so that if both errors are made in the same statement:
def f():
    x: int = 5
    global x

"SyntaxError: global after assignment" will be reported. The same logic
applies to nonlocal.

--
Ivan


On 12 September 2016 at 11:37, Christian Heimes <christian at python.org>
wrote:

> 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
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> levkivskyi%40gmail.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20160912/020eb81e/attachment.html>


More information about the Python-Dev mailing list