From fc31a13dc1799b8d972c1f4ea49f27090aed7f48 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Mon, 1 Aug 2022 01:06:13 -0400 Subject: gh-95511: IDLE - fix Shell context menu copy-with-prompts bug (#95512) 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/idle_test/test_sidebar.py | 7 ++++--- Lib/idlelib/pyshell.py | 22 ++++++++++------------ .../2022-07-31-22-15-14.gh-issue-95511.WX6PmB.rst | 2 ++ 4 files changed, 19 insertions(+), 15 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 dc506dc..0e3a50b 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/idle_test/test_sidebar.py b/Lib/idlelib/idle_test/test_sidebar.py index 01fd6a0..290e037 100644 --- a/Lib/idlelib/idle_test/test_sidebar.py +++ b/Lib/idlelib/idle_test/test_sidebar.py @@ -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, diff --git a/Lib/idlelib/pyshell.py b/Lib/idlelib/pyshell.py index 2e54a81..3806122 100755 --- a/Lib/idlelib/pyshell.py +++ b/Lib/idlelib/pyshell.py @@ -996,19 +996,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 index 0000000..803fa5f --- /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. -- cgit v0.12 36' href='#n36'>36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
"""
Functions to convert between Python values and C structs.
Python strings are used to hold the data representing the C struct
and also as format strings to describe the layout of data in the C struct.

The optional first format char indicates byte order, size and alignment:
 @: native order, size & alignment (default)
 =: native order, std. size & alignment
 <: little-endian, std. size & alignment
 >: big-endian, std. size & alignment
 !: same as >

The remaining chars indicate types of args and must match exactly;
these can be preceded by a decimal repeat count:
 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;
 h:short; H:unsigned short; i:int; I:unsigned int;
 l:long; L:unsigned long; f:float; d:double.
Special cases (preceding decimal count indicates length):
 s:string (array of char); p: pascal string (with count byte).
Special case (only available in native format):
 P:an integer type that is wide enough to hold a pointer.
Special case (not in native mode unless 'long long' in platform C):
 q:long long; Q:unsigned long long
Whitespace between formats is ignored.

The variable struct.error is an exception raised on errors.
"""
__version__ = '0.1'

from _struct import Struct, error

_MAXCACHE = 100
_cache = {}

def _compile(fmt):
    # Internal: compile struct pattern
    if len(_cache) >= _MAXCACHE:
        _cache.clear()
    s = Struct(fmt)
    _cache[fmt] = s
    return s

def calcsize(fmt):
    """
    Return size of C struct described by format string fmt.
    See struct.__doc__ for more on format strings.
    """
    try:
        o = _cache[fmt]
    except KeyError:
        o = _compile(fmt)
    return o.size

def pack(fmt, *args):
    """
    Return string containing values v1, v2, ... packed according to fmt.
    See struct.__doc__ for more on format strings.
    """
    try:
        o = _cache[fmt]
    except KeyError:
        o = _compile(fmt)
    return o.pack(*args)

def pack_into(fmt, buf, offset, *args):
    """
    Pack the values v2, v2, ... according to fmt, write
    the packed bytes into the writable buffer buf starting at offset.
    See struct.__doc__ for more on format strings.
    """
    try:
        o = _cache[fmt]
    except KeyError:
        o = _compile(fmt)
    return o.pack_into(buf, offset, *args)

def unpack(fmt, s):
    """
    Unpack the string, containing packed C structure data, according
    to fmt.  Requires len(string)==calcsize(fmt).
    See struct.__doc__ for more on format strings.
    """
    try:
        o = _cache[fmt]
    except KeyError:
        o = _compile(fmt)
    return o.unpack(s)

def unpack_from(fmt, buf, offset=0):
    """
    Unpack the buffer, containing packed C structure data, according to
    fmt starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).
    See struct.__doc__ for more on format strings.
    """
    try:
        o = _cache[fmt]
    except KeyError:
        o = _compile(fmt)
    return o.unpack_from(buf, offset)