Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 654d62a

Browse files
gh-95191: IDLE: Include prompts when saving Shell GH-95554 (#95558)
(cherry picked from commit b85411f) Co-authored-by: Terry Jan Reedy <[email protected]>
1 parent b0c3825 commit 654d62a

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

Lib/idlelib/NEWS.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Released 2023-04-03?
44
=========================
55

66

7+
gh-95191: Include prompts when saving Shell (interactive input/output).
8+
79
gh-95511: Fix the Shell context menu copy-with-prompts bug of copying
810
an extra line when one selects whole lines.
911

Lib/idlelib/idle_test/test_iomenu.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"Test , coverage 17%."
22

3-
from idlelib import iomenu, util
3+
from idlelib import iomenu
44
import unittest
55
from test.support import requires
66
from tkinter import Tk
77
from idlelib.editor import EditorWindow
8+
from idlelib import util
9+
from idlelib.idle_test.mock_idle import Func
810

911

1012
class IOBindingTest(unittest.TestCase):
@@ -36,9 +38,14 @@ def test_fixnewlines_end(self):
3638
io = self.io
3739
fix = io.fixnewlines
3840
text = io.editwin.text
41+
42+
# Make the editor temporarily look like Shell.
3943
self.editwin.interp = None
40-
eq(fix(), '')
41-
del self.editwin.interp
44+
shelltext = '>>> if 1'
45+
self.editwin.get_prompt_text = Func(result=shelltext)
46+
eq(fix(), shelltext) # Get... call and '\n' not added.
47+
del self.editwin.interp, self.editwin.get_prompt_text
48+
4249
text.insert(1.0, 'a')
4350
eq(fix(), 'a'+io.eol_convention)
4451
eq(text.get('1.0', 'end-1c'), 'a\n')

Lib/idlelib/iomenu.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,17 @@ def writefile(self, filename):
251251
return False
252252

253253
def fixnewlines(self):
254-
"Return text with final \n if needed and os eols."
255-
if (self.text.get("end-2c") != '\n'
256-
and not hasattr(self.editwin, "interp")): # Not shell.
257-
self.text.insert("end-1c", "\n")
258-
text = self.text.get("1.0", "end-1c")
254+
"""Return text with os eols.
255+
256+
Add prompts if shell else final \n if missing.
257+
"""
258+
259+
if hasattr(self.editwin, "interp"): # Saving shell.
260+
text = self.editwin.get_prompt_text('1.0', self.text.index('end-1c'))
261+
else:
262+
if self.text.get("end-2c") != '\n':
263+
self.text.insert("end-1c", "\n") # Changes 'end-1c' value.
264+
text = self.text.get('1.0', "end-1c")
259265
if self.eol_convention != "\n":
260266
text = text.replace("\n", self.eol_convention)
261267
return text

Lib/idlelib/pyshell.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,23 @@ def replace_event(self, event):
995995
def get_standard_extension_names(self):
996996
return idleConf.GetExtensions(shell_only=True)
997997

998+
def get_prompt_text(self, first, last):
999+
"""Return text between first and last with prompts added."""
1000+
text = self.text.get(first, last)
1001+
lineno_range = range(
1002+
int(float(first)),
1003+
int(float(last))
1004+
)
1005+
prompts = [
1006+
self.shell_sidebar.line_prompts.get(lineno)
1007+
for lineno in lineno_range
1008+
]
1009+
return "\n".join(
1010+
line if prompt is None else f"{prompt} {line}"
1011+
for prompt, line in zip(prompts, text.splitlines())
1012+
) + "\n"
1013+
1014+
9981015
def copy_with_prompts_callback(self, event=None):
9991016
"""Copy selected lines to the clipboard, with prompts.
10001017
@@ -1011,23 +1028,9 @@ def copy_with_prompts_callback(self, event=None):
10111028
sellast = text.index('sel.last')
10121029
if sellast[-1] != '0':
10131030
sellast = text.index("sel.last+1line linestart")
1014-
1015-
selected_text = self.text.get(selfirst, sellast)
1016-
selection_lineno_range = range(
1017-
int(float(selfirst)),
1018-
int(float(sellast))
1019-
)
1020-
prompts = [
1021-
self.shell_sidebar.line_prompts.get(lineno)
1022-
for lineno in selection_lineno_range
1023-
]
1024-
selected_text_with_prompts = "\n".join(
1025-
line if prompt is None else f"{prompt} {line}"
1026-
for prompt, line in zip(prompts, selected_text.splitlines())
1027-
) + "\n"
1028-
10291031
text.clipboard_clear()
1030-
text.clipboard_append(selected_text_with_prompts)
1032+
prompt_text = self.get_prompt_text(selfirst, sellast)
1033+
text.clipboard_append(prompt_text)
10311034

10321035
reading = False
10331036
executing = False
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Include prompts when saving Shell (interactive input and output).

0 commit comments

Comments
 (0)