From 1b3ddbec718f8ac7448cb106da08b67d4a4b4daf Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sun, 31 Jul 2022 22:18:10 -0400 Subject: [PATCH 1/2] gh-95511: IDLE - fix Shell context menu copy-with-prompts bug If one selects whole lines, as the sidebar makes easy, do not add an extra line. Only move the end of a selection to the beginning of the next line when not already at the beginning of a line. (Also improve the surrounding code.) --- Lib/idlelib/NEWS.txt | 3 +++ Lib/idlelib/pyshell.py | 22 +++++++++---------- ...2-07-31-22-15-14.gh-issue-95511.WX6PmB.rst | 2 ++ 3 files changed, 15 insertions(+), 12 deletions(-) create mode 100644 Misc/NEWS.d/next/IDLE/2022-07-31-22-15-14.gh-issue-95511.WX6PmB.rst diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt index dc506dccfe99aa..0e3a50bb639918 100644 --- a/Lib/idlelib/NEWS.txt +++ b/Lib/idlelib/NEWS.txt @@ -4,6 +4,9 @@ Released on 2022-10-03 ========================= +gh-95511: Fix the Shell context menu copy-with-prompts bug of copying +an extra line when one selects whole lines. + gh-95471: Tweak Edit menu. Move 'Select All' above 'Cut' as it is used with 'Cut' and 'Copy' but not 'Paste'. Add a separator between 'Replace' and 'Go to Line' to help IDLE issue triagers. diff --git a/Lib/idlelib/pyshell.py b/Lib/idlelib/pyshell.py index 2e54a81a1d38dd..38061229622885 100755 --- a/Lib/idlelib/pyshell.py +++ b/Lib/idlelib/pyshell.py @@ -996,19 +996,17 @@ def copy_with_prompts_callback(self, event=None): and/or last lines is selected. """ text = self.text - - selection_indexes = ( - self.text.index("sel.first linestart"), - self.text.index("sel.last +1line linestart"), - ) - if selection_indexes[0] is None: - # There is no selection, so do nothing. - return - - selected_text = self.text.get(*selection_indexes) + selfirst = text.index('sel.first linestart') + if selfirst is None: # Should not be possible. + return # No selection, do nothing. + sellast = text.index('sel.last') + if sellast[-1] != '0': + sellast = text.index("sel.last+1line linestart") + + selected_text = self.text.get(selfirst, sellast) selection_lineno_range = range( - int(float(selection_indexes[0])), - int(float(selection_indexes[1])) + int(float(selfirst)), + int(float(sellast)) ) prompts = [ self.shell_sidebar.line_prompts.get(lineno) diff --git a/Misc/NEWS.d/next/IDLE/2022-07-31-22-15-14.gh-issue-95511.WX6PmB.rst b/Misc/NEWS.d/next/IDLE/2022-07-31-22-15-14.gh-issue-95511.WX6PmB.rst new file mode 100644 index 00000000000000..803fa5f2a2ab0d --- /dev/null +++ b/Misc/NEWS.d/next/IDLE/2022-07-31-22-15-14.gh-issue-95511.WX6PmB.rst @@ -0,0 +1,2 @@ +Fix the Shell context menu copy-with-prompts bug of copying an extra line +when one selects whole lines. From 53c573a3de330786e2e7c8bf6652b3f26494498e Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Mon, 1 Aug 2022 00:19:26 -0400 Subject: [PATCH 2/2] Fix test_copy_with_prompts. --- Lib/idlelib/idle_test/test_sidebar.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/idlelib/idle_test/test_sidebar.py b/Lib/idlelib/idle_test/test_sidebar.py index 01fd6a04d0de30..290e037fe727a1 100644 --- a/Lib/idlelib/idle_test/test_sidebar.py +++ b/Lib/idlelib/idle_test/test_sidebar.py @@ -733,7 +733,7 @@ def test_copy_with_prompts(self): first_line = get_end_linenumber(text) self.do_input(dedent('''\ if True: - print(1) + print(1) ''')) yield @@ -744,9 +744,10 @@ def test_copy_with_prompts(self): selected_lines_text = text.get('sel.first linestart', 'sel.last') selected_lines = selected_lines_text.split('\n') - # Expect a block of input, a single output line, and a new prompt + selected_lines.pop() # Final '' is a split artifact, not a line. + # Expect a block of input and a single output line. expected_prompts = \ - ['>>>'] + ['...'] * (len(selected_lines) - 3) + [None, '>>>'] + ['>>>'] + ['...'] * (len(selected_lines) - 2) + [None] selected_text_with_prompts = '\n'.join( line if prompt is None else prompt + ' ' + line for prompt, line in zip(expected_prompts,