classification
Title: IDLE: cleanup use of sys.ps1 and never set it.
Type: behavior Stage: resolved
Components: IDLE Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: terry.reedy
Priority: normal Keywords: patch, patch

Created on 2017-10-24 05:58 by terry.reedy, last changed 2018-12-29 04:48 by terry.reedy. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 4143 merged terry.reedy, 2017-10-27 00:25
PR 4155 merged python-dev, 2017-10-28 01:01
Messages (3)
msg304860 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-10-24 05:58
This issue is about cleaning up IDLE's use of sys.ps1 for its prompt (sys.ps2 is not used).  A. This will make for cleaner code and fix some bugs.  B. This will be better for testing.  (Some possible changes to pyshell might make sys.ps1 irrelevant, but that is for the future.)

Part1. editor.EditorWindow.__init__ sets sys.ps1 to '>>> ' if not set.
        try:
            sys.ps1
        except AttributeError:
            sys.ps1 = '>>> '
IDLE initially respects a user setting of sys.ps1 in a startup file.

pyshell.PyShell.open_debugger hasand .close_debugger has these lines
        sys.ps1 = "[DEBUG ON]\n>>> "
            sys.ps1 = ">>> "
These overwrite any user setting of sys.ps1.  As long as IDLE pays attention to the initial value of sys.ps1, I consider this a bug.

pyshell.PyShell.show_prompt starts with
        try:
            s = str(sys.ps1)
        except:
            s = ""
        self.console.write(s)

I suspect that this is a holdover from when IDLE executed user code in its own process, as it still does with the deprecated '-n' option.  However, if a -n user deletes sys.ps1, the replacement should be '>>> ', not '' (bug 2).

In the current default subprocess mode, users cannot change the IDLE process sys module (see #13657), so rereading sys.ps1 for every editor window and prompt is nonsensical.

Patch 1: replace the EditorWindow.__init__ code with module level code
sys_ps1 = sys.ps1 if hasattr(sys, 'ps1') else '>>> '
prompt = sys_ps1

Fix pyshell to import editor rather than two of its objects.  In its debugger methods, set editor.prompt, using editor.sys_ps1, thus preserving any user setting.  In print_prompt, print the current editor.prompt.  (This will disable users resetting ps1 in deprecated -n mode, but I consider that okay as it matches the normal mode.)

Part 2. The reason the prompt is set in EditorWindow, instead of where is it obviously needed, the PyShell subclass of the OutputWindow subclass of EditorWindow, is that it is currently used in EditorWindow.smart_backspace_event and .newline_and_indent_event, while   pyshell imports editor (and must), and not the other way around.

Treating the prompt text as special in an editor lead to a bug in smart_backspace that was fixed in #13039 by guarding it use with self.context_use_ps1.  There is still a nearly inconsequential bug in the newline method where the prompt use is not guarded.  (Hitting newline with the cursor before the 't' in '>>> test' leaves the preceding space instead of deleting it.)

Patch 2: Only the last line of the prompt is relevant in either method.  I believe that replacing self.context_use_ps1 = False in an editor, = True in Shell with self.last_prompt_line = '' in an editor, = whatever it is in Shell, will allow moving sys_ps1 and prompt to pyshell.  This would simplify patch 1.
msg305095 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-10-27 00:35
I put the new shell variables into PyShell itself.  There is usually only one instance created in a session.

I tested the patch manually in both shell and editor with both the default prompt and with sys.ps1 set before importing idlelib.idle.  Beginning to automate tests for editor and shell is a project in itself.
msg332699 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-12-29 04:48
By mistake, I deleted the setting of context_use_ps1 in PyShell while still in use in several places.  Fixed in #34055.
History
Date User Action Args
2018-12-29 04:48:34terry.reedysetkeywords: patch, patch

messages: + msg332699
2017-10-28 01:43:38terry.reedysetkeywords: patch, patch
status: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-10-28 01:42:52terry.reedysetpull_requests: - pull_request4108
2017-10-28 01:42:43terry.reedysetpull_requests: - pull_request4109
2017-10-28 01:01:43python-devsetpull_requests: + pull_request4124
2017-10-27 01:06:53terry.reedysetstage: commit review -> patch review
pull_requests: + pull_request4109
2017-10-27 00:35:23terry.reedysetmessages: + msg305095
stage: patch review -> commit review
2017-10-27 00:25:10terry.reedysetkeywords: + patch
stage: test needed -> patch review
pull_requests: + pull_request4108
2017-10-27 00:25:09terry.reedysetkeywords: + patch
stage: test needed -> test needed
pull_requests: + pull_request4107
2017-10-24 05:58:28terry.reedycreate