[issue19335] codeop misclassifies incomplete code with 'nonlocal'

Terry J. Reedy report at bugs.python.org
Sun Jun 28 12:01:16 EDT 2020


Terry J. Reedy <tjreedy at udel.edu> added the comment:

Pablo and Lysandros: this issue is about a corner-case bug in either compile(,,'single') or its use by REPL and codeop._maybe_compile (comc).  What do either of you think?  Can the new parser handle it better?

Should we just add the hack suggested by Nick in msg200936 or is there a better solution?  Is interactive mode python calling compile() differently?  Or is it doing a check that could be incorporated into compile?  (Perhap after correction, see below.)

The issue above is about interactive entry of

def a():
  def b():
    nonlocal c

REPL does not raise SyntaxError, comc does.  Not raising is correct here because additional lines added to the nonlocal context may make the code valid.

Additional experiment: the same is true (comc raises, REPL not) for

def a():
  nonlocal c

Here, REPL not raising (until a blank is entered) could be considered a glitch because there is no pending nonlocal context to be completed.  Though raising later than necessary is better than raising too soon.  Nick's hack (and the REPL) could check that there are at least 2 pending indents.

When the new parser compiles a function, does it know or could it know whether it is nested?  It should in that the legal grammer (use of nonlocal) is different.

----------
nosy: +lys.nikolaou, pablogsal
versions: +Python 3.10 -Python 2.7, Python 3.3, Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue19335>
_______________________________________


More information about the Python-bugs-list mailing list