[issue43366] Unclosed bracket bug in code.interact prevents identifying syntax errors

Terry J. Reedy report at bugs.python.org
Fri Mar 5 19:43:53 EST 2021


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

#43163 was about the opposite problem of raising SyntaxError too soon, when a valid continuation to imcomplete code is possible.  As with that issue, IDLE has the same problem, which is not in code.interact() itself but in codeop._maybe_compile.. 

Calling compile with, gives message, resulting in user seeing
'[def'                unclosed [      
'[def\n'              bad syntax     Syntax Error
'[\ndef'              unclosed [
'[\ndef\n'            unclosed [     prompt for more input

In the last line, the added \n after [ changes the compile SyntaxError message and, after PR 24483 for #43163, results in fruitlessly waiting for non-existent correct input until the user enters garbage or hits ^C. This is at best a codeop-specific regression from better behavior in 3.9

Changing 'def' to 'def f():' only changes the first message to 'invalid syntax' but not the user-visible result.

As near as I can tell, compile('[\n1,', ...) and compile('[\ndef', ...) give the same unclosed message pointing to the opening [.  How does the regular REPL know to prompt for input for the first and raise SyntaxError for the second?  Some unobvious flag combination?  (It is not 'exec' versus 'single'.)

>>> [
... 1,
... def
  File "<stdin>", line 3
    def
    ^
SyntaxError: invalid syntax

Guido, do you have any idea how python decides this or where the code is or who might know better?

----------
nosy: +gvanrossum, terry.reedy

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


More information about the Python-bugs-list mailing list