classification
Title: IDLE: revise doc for control chars sent to Shell
Type: behavior Stage: resolved
Components: IDLE Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: Dude Roast, martin.panter, miss-islington, terry.reedy
Priority: normal Keywords: patch, patch, patch

Created on 2019-01-26 14:52 by Dude Roast, last changed 2019-02-09 04:45 by terry.reedy. This issue is now closed.

Files
File name Uploaded Description Edit
mousenow.py Dude Roast, 2019-01-26 14:52 Code for the program
Pull Requests
URL Status Linked Edit
PR 11799 merged terry.reedy, 2019-02-09 03:16
PR 11800 merged miss-islington, 2019-02-09 03:52
Messages (7)
msg334394 - (view) Author: Dude Roast (Dude Roast) Date: 2019-01-26 14:52
Whenever I try to use Backspace(\b) it always prints square boxes instead of deleting previous string.

Code Down:- 

import pyautogui
print("Press Ctrl+c to quit")

try:
    while True:
        x,y = pyautogui.position();
        positionStr = "X: " + str(x).rjust(4) + " Y: " + str(y).rjust(4)
        print(positionStr,end ='')
        print('\b'*len(positionStr),end='',flush=True)
        
except KeyboardInterrupt:
    print("\nDone")


O/P:- 

Press Ctrl+c to quit
X:  317 Y:  261X:  317 Y:  261X:  317 Y:  261X:  317 Y:  261X:  317 Y:  261X:  317 Y:  261X:  317 Y:  261X:  317 Y:  261X:  317 Y:  261X:  317 Y:  261X:  317 Y:  261X:  317 Y:  261X:  317 Y:  261
Done
msg334400 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2019-01-26 21:12
I suspect Idle just passes control characters directly to an underlying Text or similar TK widget. As far as I know, TK only documents behaviour for tabs and newlines, not other control characters.

Last time this was brought up, Terry added a sentence under <https://docs.python.org/3.7/library/idle.html#user-output-in-shell>, third paragraph, about this:

“Newline characters cause following text to appear on a new line, but other control characters are either replaced with a box or deleted.”
msg334401 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2019-01-26 21:15
Ment to point to previous bug report: Issue 23220
msg334403 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-01-26 22:14
Example code for the tracker should not use 3rd-party modules unless essential.  It should be reduced to the minimum testcase needed to show the behavior.  In this case, "print('a\b')", resulting, on Windows 10, in "a", is enough. (On macOS Mohave, the result is just 'a'.) These results are not a bug. The IDLE chapter of the doc, viewable with Help => IDLE Doc, has a section 'User output in Shell', added for 3.6.8 and 3.7.2, that explains.  The full relevant text with the key line Martin posted is

"When a program outputs text, the result is determined by the corresponding output device.  When IDLE executes user code, sys.stdout and sys.stderr are connected to the display area of IDLE’s Shell.  Some of its features are inherited from the underlying Tk Text widget.  Others are programmed additions.  Where it matters, Shell is designed for development rather than production runs. 
...
Text widgets display a subset of Unicode, the Basic Multilingual Plane (BMP). Which characters get a proper glyph instead of a replacement box depends on the operating system and installed fonts.  Newline characters cause following text to appear on a new line, but other control characters are either replaced with a box or deleted.  However, repr(), which is used for interactive echo of expression values, replaces control characters, some BMP codepoints, and all non-BMP characters with escape codes before they are output."

In reviewing this and doing more experiments, I realized that this needs revision. A. tabs jump to the next 8-char tabstop.  B. Display replacements for control chars can include spaces and other things.  C. Display replacements depend on the operating system (as noted above) and possibly the font. D. Display replacements only occur when the string is displayed, not when stored in the text widget.  Selecting and copying a string on the screen copies what is stored in the widget, not what is displayed on the screen.  E. Trying to insert non-BMP characters ('\U000nnnnn') in a text widget is an error.  (Preventing this even when a user implicitly requests is a separate issue.)  F. Printing null and return characters may cause problems.  Null (\0, \x00) hands IDLE on Mac.  Return (\r, \x0D) is ignored by tk but interpreted and converted to \n when pasted into code.

I want to add a code block example that illustrate some of these points.

>>> s = 'abc\t\a<\x02><\r>\b'
>>> len(s)
12
>>> s
'abc\t\x07<\x02><\r>\x08'  # This is repr(s).
>>> print(s)  # In IDLE, inserts s into text widget.
# Visible result varies; try it.

PS: On hard-copy terminals, where control characters originated, \b only means 'Move the print position back a character.'  On 'soft-copy' terminals and consoles, it may or may not also mean 'erase the previous character'.
msg334405 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-01-26 22:49
Martin, your suspicion is correct, as verified by selecting and copying the string and pasting into code between quotes.  I strongly suspect that tk just sends the string as is to the OS graphics system insert text function, that the latter decides what to display.  I ran
  for i in range(1,33): print(f'<{chr(i)}>')
on Windows console, Windows IDLE, and Mac IDLE (omitting '1' crashes on Mac) and there are multiple inconsistencies.  I believe tcl/tk only documents \t and \n because those are the only two control chars whose behavior is consistent across graphics systems.  Thanks for digging up the previous issue.  I would close as a duplicate if I did not see a need for doc revision.
msg335124 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-02-09 03:51
New changeset 8a03ff2ff4db973c9fe152561f1796e72cb71132 by Terry Jan Reedy in branch 'master':
bpo-35833: Revise IDLE doc for control codes sent to Shell. (GH-11799)
https://github.com/python/cpython/commit/8a03ff2ff4db973c9fe152561f1796e72cb71132
msg335125 - (view) Author: miss-islington (miss-islington) Date: 2019-02-09 04:43
New changeset 3fcfef357e64a25afd52ec6a0d1b5284cb162203 by Miss Islington (bot) in branch '3.7':
bpo-35833: Revise IDLE doc for control codes sent to Shell. (GH-11799)
https://github.com/python/cpython/commit/3fcfef357e64a25afd52ec6a0d1b5284cb162203
History
Date User Action Args
2019-02-09 04:45:12terry.reedysetkeywords: patch, patch, patch
status: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-02-09 04:44:43terry.reedysetpull_requests: - pull_request11811
2019-02-09 04:44:24terry.reedysetpull_requests: - pull_request11808
2019-02-09 04:44:06terry.reedysetpull_requests: - pull_request11809
2019-02-09 04:43:23miss-islingtonsetnosy: + miss-islington
messages: + msg335125
2019-02-09 03:52:05miss-islingtonsetpull_requests: + pull_request11811
2019-02-09 03:52:03miss-islingtonsetpull_requests: + pull_request11810
2019-02-09 03:51:56terry.reedysetmessages: + msg335124
2019-02-09 03:16:32terry.reedysetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request11809
2019-02-09 03:16:24terry.reedysetkeywords: + patch
stage: needs patch -> needs patch
pull_requests: + pull_request11808
2019-02-09 03:16:18terry.reedysetkeywords: + patch
stage: needs patch -> needs patch
pull_requests: + pull_request11807
2019-01-26 22:49:44terry.reedysetnosy: + martin.panter
messages: + msg334405
2019-01-26 22:14:56terry.reedysetnosy: - martin.panter
title: Backspace not working -> IDLE: revise doc for control chars sent to Shell
messages: + msg334403

versions: + Python 3.8
stage: needs patch
2019-01-26 21:15:37martin.pantersetmessages: + msg334401
2019-01-26 21:12:37martin.pantersetnosy: + martin.panter
messages: + msg334400
2019-01-26 14:52:50Dude Roastcreate