Pasting code into the cmdline interpreter

eryk sun eryksun at gmail.com
Thu Sep 22 09:45:48 EDT 2016


On Thu, Sep 22, 2016 at 12:40 PM, Gregory Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> eryk sun wrote:
>>
>> Actually in a Unix terminal the cursor can also be at
>> the end of a line, but a bug in Python requires pressing Ctrl+D twice
>> in that case.
>
> I wouldn't call that a bug, rather it's a consequence of
> what Ctrl-D does. It doesn't really mean EOF, it means to
> send whatever the terminal driver has in its input buffer.
> If the buffer is empty at the time, the process gets a
> zero-length read which is taken as EOF. But if the buffer
> is not empty, it just gets whatever is in the buffer.
>
> There's nothing Python can do about that, because it
> never sees the Ctrl-D -- that's handled entirely by the
> terminal driver.

Yes, FileIO.readall continues making read() system calls until it sees
an empty read. But if we know we're reading from a terminal, we should
be able to assume that a read either consumes an entire line up to a
newline character or the entire buffer, no? In other words, if we see
a read that returns less than the buffer size but doesn't end on a
newline, then for a terminal, and only a terminal, I think we can
infer Ctrl+D was typed and handle it as EOF.



More information about the Python-list mailing list