]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-95511: IDLE - fix Shell context menu copy-with-prompts bug (GH-95512)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 1 Aug 2022 05:25:56 +0000 (22:25 -0700)
committerGitHub <noreply@github.com>
Mon, 1 Aug 2022 05:25:56 +0000 (22:25 -0700)
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.)
(cherry picked from commit fc31a13dc1799b8d972c1f4ea49f27090aed7f48)

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Lib/idlelib/NEWS.txt
Lib/idlelib/idle_test/test_sidebar.py
Lib/idlelib/pyshell.py
Misc/NEWS.d/next/IDLE/2022-07-31-22-15-14.gh-issue-95511.WX6PmB.rst [new file with mode: 0644]

index 3adf98257952169d2cb1664bff2241c692252b66..add0d8c6bead2c6ff7e89578ab38ece98bfbf806 100644 (file)
@@ -4,6 +4,9 @@ Released 2023-04-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.
index 01fd6a04d0de3021c235ace8efd7293ea4a8f4b4..290e037fe727a1a7290f9862332427f7c78e9284 100644 (file)
@@ -733,7 +733,7 @@ class ShellSidebarTest(unittest.TestCase):
         first_line = get_end_linenumber(text)
         self.do_input(dedent('''\
             if True:
-            print(1)
+                print(1)
 
             '''))
         yield
@@ -744,9 +744,10 @@ class ShellSidebarTest(unittest.TestCase):
 
         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,
index 6c333b0bc3b81836a8af8a7a8e2a011fce1d3f02..ba33f581ea30f09da4ad3d73bb5e4c8c7f937cff 100755 (executable)
@@ -1005,19 +1005,17 @@ class PyShell(OutputWindow):
         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 (file)
index 0000000..803fa5f
--- /dev/null
@@ -0,0 +1,2 @@
+Fix the Shell context menu copy-with-prompts bug of copying an extra line
+when one selects whole lines.