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

Skip to content

Commit 8570f6d

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

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 on 2022-10-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
@@ -986,6 +986,23 @@ def replace_event(self, event):
986986
def get_standard_extension_names(self):
987987
return idleConf.GetExtensions(shell_only=True)
988988

989+
def get_prompt_text(self, first, last):
990+
"""Return text between first and last with prompts added."""
991+
text = self.text.get(first, last)
992+
lineno_range = range(
993+
int(float(first)),
994+
int(float(last))
995+
)
996+
prompts = [
997+
self.shell_sidebar.line_prompts.get(lineno)
998+
for lineno in lineno_range
999+
]
1000+
return "\n".join(
1001+
line if prompt is None else f"{prompt} {line}"
1002+
for prompt, line in zip(prompts, text.splitlines())
1003+
) + "\n"
1004+
1005+
9891006
def copy_with_prompts_callback(self, event=None):
9901007
"""Copy selected lines to the clipboard, with prompts.
9911008
@@ -1002,23 +1019,9 @@ def copy_with_prompts_callback(self, event=None):
10021019
sellast = text.index('sel.last')
10031020
if sellast[-1] != '0':
10041021
sellast = text.index("sel.last+1line linestart")
1005-
1006-
selected_text = self.text.get(selfirst, sellast)
1007-
selection_lineno_range = range(
1008-
int(float(selfirst)),
1009-
int(float(sellast))
1010-
)
1011-
prompts = [
1012-
self.shell_sidebar.line_prompts.get(lineno)
1013-
for lineno in selection_lineno_range
1014-
]
1015-
selected_text_with_prompts = "\n".join(
1016-
line if prompt is None else f"{prompt} {line}"
1017-
for prompt, line in zip(prompts, selected_text.splitlines())
1018-
) + "\n"
1019-
10201022
text.clipboard_clear()
1021-
text.clipboard_append(selected_text_with_prompts)
1023+
prompt_text = self.get_prompt_text(selfirst, sellast)
1024+
text.clipboard_append(prompt_text)
10221025

10231026
reading = False
10241027
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)