classification
Title: IDLE shell adds newline after print even when `end=''` is specificied
Type: behavior Stage: resolved
Components: IDLE Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: hugonobrega, terry.reedy
Priority: normal Keywords:

Created on 2021-03-04 18:14 by hugonobrega, last changed 2021-03-04 21:15 by terry.reedy. This issue is now closed.

Messages (4)
msg388115 - (view) Author: Hugo Nobrega (hugonobrega) Date: 2021-03-04 18:14
When evaluting a call to the `print` function with argument `end=''` in the IDLE shell, a newline is unexpectedly added at the end, before the next shell prompt.

The expected behavior is to have the shell prompt next to the last printed line. The expected behavior is seen when evaluting the same expression in an interactive python shell from a terminal (`python -i`)

Example:

IDLE shell (not expected):
>>> print('a',end='')
a
>>> 

Interactive python shell (expected): 
>>> print('a',end='')
a>>>

I could not find any settings in IDLE that might be governing this behavior, not any other issues mentioning this same thing.


Tested on Python 3.9.1 on Manjaro Linux.
msg388127 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-03-04 20:04
Starting interactive code input on a new line is intentional and not a bug.  IDLE has other code to keep user program output separate from user code input, as when there is delayed asynchonous output. I plan to do more work to keep the two separate.
msg388128 - (view) Author: Hugo Nobrega (hugonobrega) Date: 2021-03-04 20:17
I see, thank you. But, in that case, shouldn't the interactive `python -i` shell have the same (now seen as desired) behavior as the IDLE shell?
msg388130 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-03-04 21:15
I don't know how many core devs would agree that inserting '\n' would be preferable.  They might say that users who want clean interaction should not use the 'end' parameter the way you did in interactive mode.

IDLE is more aimed at beginners and can be more protective and/or opinionated.  In any case, I agree with whoever wrote or edited this part of IDLE before me as to what IDLE should do.

There are also technical  issues.  Interactive python uses the common features of the various OS text and line-oriented terminal/consoles.  These do not know and do not care that they are displaying python input and output.  They display chars received from the executing program and have no way of knowing that the last n chars match python's sys.ps1 or sys.ps2.  The prompts comes from python executing "input(prompt)" after user code finishes.  To conditionally insert a newline, python would have to keep track of whether or not the last char sent by user code was a newline or not.

IDLE, on the other hand, is customized to run python code.  It *emulates* interactive mode with exec() calls.  IDLE, not python, displays prompts however programmed to do so.  It is trivial to check whether the last char inserted in the text widget is '\n' or not.

Side note: you only need '-i' for interactive mode when you also run a file because 'python -i' by itself is the same as 'python'.

'python -i somefile' is different from 'python somefile' because in the latter case, python exits after executing somefile, whereas in the former case, python executes 'input(sys.ps1)' and possibly 'input(sys.ps2)' until told to quit.  'Run module' in an IDLE editor emulates 'python -i <filename>'.
History
Date User Action Args
2021-03-04 21:15:30terry.reedysetmessages: + msg388130
2021-03-04 20:17:39hugonobregasetmessages: + msg388128
2021-03-04 20:04:09terry.reedysetstatus: open -> closed
resolution: not a bug
messages: + msg388127

stage: resolved
2021-03-04 18:14:22hugonobregacreate