Lysandros Nikolaou <lisandros...@gmail.com> added the comment:

> 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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue19335>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to