homepage

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.

classification
Title: Blank line inconsistency between InteractiveConsole and standard interpreter
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Malcolm Smith, iritkatriel, tomviner
Priority: normal Keywords:

Created on 2017年08月13日 21:12 by Malcolm Smith, last changed 2022年04月11日 14:58 by admin.

Messages (5)
msg300230 - (view) Author: Malcolm Smith (Malcolm Smith) Date: 2017年08月13日 21:12
The standard interpreter is more eager to give an error on incomplete input, while the InteractiveConsole will keep on prompting for more:
Python 3.5.3 (default, Apr 10 2017, 07:53:16) [GCC 6.3.0 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> try:
...
 File "<stdin>", line 2
 ^
IndentationError: expected an indented block
>>> import code
>>> code.interact()
Python 3.5.3 (default, Apr 10 2017, 07:53:16) [GCC 6.3.0 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> try:
...
...
...
...
The InteractiveConsole isn't totally wrong here, because after all, a blank line in this position is syntactically correct. But the inconsistency is confusing.
msg407369 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021年11月30日 12:14
I'm pretty sure both behaviours are by design. Can you explain why this is a problem?
msg407446 - (view) Author: Malcolm Smith (Malcolm Smith) Date: 2021年12月01日 12:12
I agree that both behaviors are reasonable. However, the InteractiveConsole documentation says it should "closely emulate the behavior of the interactive Python interpreter". Since people are familiar with the native interpreter, any difference in behavior is potentially annoying and could throw off somebody's flow. So I think the InteractiveConsole should be changed to match the native interpreter.
Like the native interpreter, InteractiveConsole allows other multi-line blocks to be terminated with a blank line:
>>> def foo(x):
... pass
...
>>> for x in [1,2,3]:
... pass
...
>>>
I guess the reason why "try" is different is that a "try" block isn't a complete statement on its own. If you follow it with an "except" block, then that can indeed be terminated with a blank line.
msg407454 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021年12月01日 12:54
> any difference in behavior is potentially annoying and could throw off somebody's flow. So I think the InteractiveConsole should be changed to match the native interpreter.
That would make InteractiveConsole more restrictive and break code that is currently working. It could also be annoying and throw off somebody's flow.
msg407464 - (view) Author: Malcolm Smith (Malcolm Smith) Date: 2021年12月01日 16:06
I think it's unlikely that anyone is depending on the ability to enter blank lines in a "try" block in an InteractiveConsole, especially when blank lines terminate the input in almost every other context.
Conversely, everyone who's ever entered a multi-line statement into a Python console knows that blank lines usually terminate the input. And they may have gotten into the habit, as I did, of using a blank line to abandon an incomplete input, and they'll be surprised if it doesn't work in this context. 
On further experimentation, this also affects "def" statements, but only when the blank line is at the start of the block, causing the statement to be syntactically incomplete.
Native interpreter:
 >>> def f():
 ...
 File "<stdin>", line 2
 ^
 IndentationError: expected an indented block after function definition on line 1
InteractiveConsole:
 >>> def f():
 ...
 ...
 ... pass
 ...
 >>>
So I think the current behavior is likely to annoy a much larger number of people, but whoever's responsible for this part of the standard library will have to judge that for themselves.
History
Date User Action Args
2022年04月11日 14:58:50adminsetgithub: 75379
2021年12月01日 16:37:57iritkatrielsetversions: - Python 3.5
2021年12月01日 16:07:25Malcolm Smithsetversions: + Python 3.10
2021年12月01日 16:06:23Malcolm Smithsetmessages: + msg407464
2021年12月01日 12:54:05iritkatrielsetmessages: + msg407454
2021年12月01日 12:12:41Malcolm Smithsetstatus: pending -> open

messages: + msg407446
2021年11月30日 12:14:34iritkatrielsetstatus: open -> pending

nosy: + iritkatriel
messages: + msg407369

resolution: not a bug
2017年08月13日 23:19:18tomvinersetnosy: + tomviner
2017年08月13日 21:12:37Malcolm Smithcreate

AltStyle によって変換されたページ (->オリジナル) /