[Python-checkins] IDLE -- Restrict shell prompt manipulaton to the shell. (#4143)

Terry Jan Reedy webhook-mailer at python.org
Fri Oct 27 20:26:15 EDT 2017


https://github.com/python/cpython/commit/e86172d63af5827a3c2b55b80351cb38a26190eb
commit: e86172d63af5827a3c2b55b80351cb38a26190eb
branch: master
author: Terry Jan Reedy <tjreedy at udel.edu>
committer: GitHub <noreply at github.com>
date: 2017-10-27T20:26:12-04:00
summary:

IDLE -- Restrict shell prompt manipulaton to the shell. (#4143)

Editor and output windows only see an empty last prompt line.
This simplifies the code and fixes a minor bug when newline is inserted.
Sys.ps1, if present, is read on Shell start-up, but is not set or changed.

files:
A Misc/NEWS.d/next/IDLE/2017-10-26-20-20-19.bpo-31858.VuSA_e.rst
M Lib/idlelib/editor.py
M Lib/idlelib/pyshell.py

diff --git a/Lib/idlelib/editor.py b/Lib/idlelib/editor.py
index 87d1eef01ec..68450c921f2 100644
--- a/Lib/idlelib/editor.py
+++ b/Lib/idlelib/editor.py
@@ -99,10 +99,6 @@ def __init__(self, flist=None, filename=None, key=None, root=None):
         self.flist = flist
         root = root or flist.root
         self.root = root
-        try:
-            sys.ps1
-        except AttributeError:
-            sys.ps1 = '>>> '
         self.menubar = Menu(root)
         self.top = top = windows.ListedToplevel(root, menu=self.menubar)
         if flist:
@@ -116,6 +112,8 @@ def __init__(self, flist=None, filename=None, key=None, root=None):
             self.top.instance_dict = {}
         self.recent_files_path = os.path.join(
                 idleConf.userdir, 'recent-files.lst')
+
+        self.prompt_last_line = ''  # Override in PyShell
         self.text_frame = text_frame = Frame(top)
         self.vbar = vbar = Scrollbar(text_frame, name='vbar')
         self.width = idleConf.GetOption('main', 'EditorWindow',
@@ -1213,13 +1211,9 @@ def smart_backspace_event(self, event):
         assert have > 0
         want = ((have - 1) // self.indentwidth) * self.indentwidth
         # Debug prompt is multilined....
-        if self.context_use_ps1:
-            last_line_of_prompt = sys.ps1.split('\n')[-1]
-        else:
-            last_line_of_prompt = ''
         ncharsdeleted = 0
         while 1:
-            if chars == last_line_of_prompt:
+            if chars == self.prompt_last_line:  # '' unless PyShell
                 break
             chars = chars[:-1]
             ncharsdeleted = ncharsdeleted + 1
@@ -1288,8 +1282,7 @@ def newline_and_indent_event(self, event):
             indent = line[:i]
             # strip whitespace before insert point unless it's in the prompt
             i = 0
-            last_line_of_prompt = sys.ps1.split('\n')[-1]
-            while line and line[-1] in " \t" and line != last_line_of_prompt:
+            while line and line[-1] in " \t" and line != self.prompt_last_line:
                 line = line[:-1]
                 i = i+1
             if i:
diff --git a/Lib/idlelib/pyshell.py b/Lib/idlelib/pyshell.py
index 168eeae9ad8..8b07d52cc48 100755
--- a/Lib/idlelib/pyshell.py
+++ b/Lib/idlelib/pyshell.py
@@ -857,15 +857,17 @@ def __init__(self, flist=None):
             fixwordbreaks(root)
             root.withdraw()
             flist = PyShellFileList(root)
-        #
+
         OutputWindow.__init__(self, flist, None, None)
-        #
-##        self.config(usetabs=1, indentwidth=8, context_use_ps1=1)
+
         self.usetabs = True
         # indentwidth must be 8 when using tabs.  See note in EditorWindow:
         self.indentwidth = 8
-        self.context_use_ps1 = True
-        #
+
+        self.sys_ps1 = sys.ps1 if hasattr(sys, 'ps1') else '>>> '
+        self.prompt_last_line = self.sys_ps1.split('\n')[-1]
+        self.prompt = self.sys_ps1  # Changes when debug active
+
         text = self.text
         text.configure(wrap="char")
         text.bind("<<newline-and-indent>>", self.enter_callback)
@@ -878,7 +880,7 @@ def __init__(self, flist=None):
         if use_subprocess:
             text.bind("<<view-restart>>", self.view_restart_mark)
             text.bind("<<restart-shell>>", self.restart_shell)
-        #
+
         self.save_stdout = sys.stdout
         self.save_stderr = sys.stderr
         self.save_stdin = sys.stdin
@@ -951,7 +953,7 @@ def close_debugger(self):
                 debugger_r.close_remote_debugger(self.interp.rpcclt)
             self.resetoutput()
             self.console.write("[DEBUG OFF]\n")
-            sys.ps1 = ">>> "
+            self.prompt = self.sys_ps1
             self.showprompt()
         self.set_debugger_indicator()
 
@@ -963,7 +965,7 @@ def open_debugger(self):
             dbg_gui = debugger.Debugger(self)
         self.interp.setdebugger(dbg_gui)
         dbg_gui.load_breakpoints()
-        sys.ps1 = "[DEBUG ON]\n>>> "
+        self.prompt = "[DEBUG ON]\n" + self.sys_ps1
         self.showprompt()
         self.set_debugger_indicator()
 
@@ -1248,11 +1250,7 @@ def restart_shell(self, event=None):
 
     def showprompt(self):
         self.resetoutput()
-        try:
-            s = str(sys.ps1)
-        except:
-            s = ""
-        self.console.write(s)
+        self.console.write(self.prompt)
         self.text.mark_set("insert", "end-1c")
         self.set_line_and_column()
         self.io.reset_undo()
diff --git a/Misc/NEWS.d/next/IDLE/2017-10-26-20-20-19.bpo-31858.VuSA_e.rst b/Misc/NEWS.d/next/IDLE/2017-10-26-20-20-19.bpo-31858.VuSA_e.rst
new file mode 100644
index 00000000000..2ad9dc9662f
--- /dev/null
+++ b/Misc/NEWS.d/next/IDLE/2017-10-26-20-20-19.bpo-31858.VuSA_e.rst
@@ -0,0 +1,4 @@
+IDLE -- Restrict shell prompt manipulaton to the shell. Editor and output
+windows only see an empty last prompt line.  This simplifies the code and
+fixes a minor bug when newline is inserted. Sys.ps1, if present, is read on
+Shell start-up, but is not set or changed.



More information about the Python-checkins mailing list