This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author lys.nikolaou
Recipients Esa.Peuha, Mark.Shannon, RJ722, Rosuav, cheryl.sabella, lys.nikolaou, mbussonn, ncoghlan, pablogsal, r.david.murray, terry.reedy
Date 2020-06-29.12:16:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1593432985.93.0.454355260959.issue19335@roundup.psfhosted.org>
In-reply-to
Content
> What do either of you think?  Can the new parser handle it better?

Pablo, correct me if I'm wrong, but I think that the parser has nothing to do with this. It's not the parser that produces this SyntaxError, it's some later compilation phase (I guess symtable creation phase?), which I know very very little about, so I can't really offer an opinion on what can be done.

In any case, this should actually indicate that this is not a parser issue:

(venv) ➜  cpython git:(master) ./python.exe
Python 3.10.0a0 (heads/master:7f569c9bc0, Jun 29 2020, 14:48:39)
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = '''\
... def a():
...     def b():
...         nonlocal c
... '''
>>> a
'def a():\n    def b():\n        nonlocal c\n'
>>> compile(a, '?', 'single')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "?", line 3
SyntaxError: no binding for nonlocal 'c' found
>>> import ast
>>> ast.dump(ast.parse(a, mode='single'))
"Interactive(body=[FunctionDef(name='a', args=arguments(posonlyargs=[], args=[], kwonlyargs=[], kw_defaults=[], defaults=[]), body=[FunctionDef(name='b', args=arguments(posonlyargs=[], args=[], kwonlyargs=[], kw_defaults=[], defaults=[]), body=[Nonlocal(names=['c'])], decorator_list=[])], decorator_list=[])])"

All in all, I think that Nick's hack is the way to go here, since it's comc's responsibility to correctly check if a statement is incomplete, right?


> When the new parser compiles a function, does it know or could it know whether it is nested?

It currently doesn't, but the tokenizer holds a stack of indent tokens, which can be accessed by the parser. This way we could indeed check if there are two or more pending indent tokens, before generating an AST for nonlocal. However, I don't think that this is generally a good idea. Parsing should not care about statement semantics, in that a nonlocal statement should be like any other statement in the parser's point of view.
History
Date User Action Args
2020-06-29 12:16:25lys.nikolaousetrecipients: + lys.nikolaou, terry.reedy, ncoghlan, r.david.murray, Mark.Shannon, Rosuav, Esa.Peuha, mbussonn, cheryl.sabella, pablogsal, RJ722
2020-06-29 12:16:25lys.nikolaousetmessageid: <1593432985.93.0.454355260959.issue19335@roundup.psfhosted.org>
2020-06-29 12:16:25lys.nikolaoulinkissue19335 messages
2020-06-29 12:16:25lys.nikolaoucreate