Re: [Python-Dev] cpython: Issue #27999: Make "global after use" a SyntaxError, and ditto for nonlocal.
On 2016-09-09 18:53, guido.van.rossum wrote:
https://hg.python.org/cpython/rev/804b71d43c85 changeset: 103415:804b71d43c85 user: Guido van Rossum
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
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
On 2016-09-09 18:53, guido.van.rossum wrote:
https://hg.python.org/cpython/rev/804b71d43c85 changeset: 103415:804b71d43c85 user: Guido van Rossum
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@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/ levkivskyi%40gmail.com
On 2016-09-12 11:46, Ivan Levkivskyi wrote:
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.
Hi Ivan, thanks for your explanation. The code looks suspicious. Can you please provide a patch that makes it more obvious, e.g. either by using if / else if / else or a comment? Christian
On 12 September 2016 at 12:24, Christian Heimes
The code looks suspicious. Can you please provide a patch that makes it more obvious, e.g. either by using if / else if / else or a comment?
Sure, I will open an issue with a patch and will add you to nosy (cannot do this *right* now, sorry). -- Ivan
On 2016-09-12 12:29, Ivan Levkivskyi wrote:
On 12 September 2016 at 12:24, Christian Heimes
mailto:christian@python.org> wrote: The code looks suspicious. Can you please provide a patch that makes it more obvious, e.g. either by using if / else if / else or a comment?
Sure, I will open an issue with a patch and will add you to nosy (cannot do this *right* now, sorry).
Don't worry, it's not relevant for the beta release. My request is purely cosmetic to make the code a bit easier to understand. :) Christian
participants (2)
-
Christian Heimes
-
Ivan Levkivskyi