This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Steven.Barker
Recipients Steven.Barker
Date 2013-12-23.22:28:32
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1387837713.58.0.522558553698.issue20058@psf.upfronthosting.co.za>
In-reply-to
Content
Pasting multiple lines of input and then pressing Enter when IDLE is waiting to read a single line (such as when input() or sys.stdin.readline() have been called) will result is a multi-line string being given as the input, rather than a single line.

This may be most easily understood by looking at an example. Run this code in IDLE (either by typing it in the shell, or from a file with F5):

    s = "X"
    while s:
        s = input()
        print(repr(s))

First, try typing in several lines. Each will be printed separately, with no newlines inside the strings (since input() strips a trailing newline).

    foo
    'foo'
    bar
    'bar'
    baz
    'baz'

Next, copy several lines of text from somewhere. It doesn't matter what the lines' contents are. Here I grabbed a list of Python version numbers, as I was on the download page after grabbing 3.4.0b1 for testing this bug:

    3.1.5
    3.0.1
    2.7.6
    2.6.9
    2.5.6
    2.4.6
    2.3.7
    2.2.3 <all the preceding lines were pasted in one go, followed by enter pressed here>
    '3.1.5\n3.0.1\n2.7.6\n2.6.9\n2.5.6\n2.4.6\n2.3.7\n2.2.3'

This behavior is different than what the Python interpreter does in a regular console shell. When running in cmd.exe on Windows, Python treats a multi-line paste just like typed input:

    3.1.5
    '3.1.5'
    3.0.1
    '3.0.1'
    2.7.6
    '2.7.6'
    2.6.9
    '2.6.9'
    2.5.6
    '2.5.6'
    2.4.6
    '2.4.6'
    2.3.7
    '2.3.7'
    2.2.3 <enter typed here>
    '2.2.3'

I expect the same behavior will be common in other kinds of terminals on other platforms.

This issue makes testing certain kinds of programs very frustrating. If your program needs to read certain text from STDIN, and you want to paste that text in quickly, you need to update your code with special logic just for use in IDLE's console. As an example of the kind of pain you may experience, try copying and pasting a block of text with a blank line into the input loop above. On a regular console session it will exit the loop after the blank line. In IDLE, it will keep running.

I've traced the source of this issue through IDLE's sys.stdin file object and an RPC call, and found it probably is located in the idlelib.PyShell.PyShell.readline method (or the surrounding code). This grabs a string from the Text object in the shell window and returns it to the Python code running in the subprocess.

Probably it should have some extra steps added to check if it got multiple lines. If so, it should split the string on newlines and return just one line of text for each readline call. I'm not sure exactly what should be done with the rest of the lines, but perhaps they could be queued up (or somehow "put back" by moving the markers in the Text object) so later lines would be grabbed by later input requests.

Or alternatively, maybe the event where the multi-line paste arrives should be handled differently, as several single-line input events, rather than a single multiple-line one.
History
Date User Action Args
2013-12-23 22:28:33Steven.Barkersetrecipients: + Steven.Barker
2013-12-23 22:28:33Steven.Barkersetmessageid: <1387837713.58.0.522558553698.issue20058@psf.upfronthosting.co.za>
2013-12-23 22:28:33Steven.Barkerlinkissue20058 messages
2013-12-23 22:28:32Steven.Barkercreate