why are *two* ctrl-D's needed with readline()

Steve Holden sholden at holdenweb.com
Fri Oct 5 14:34:17 EDT 2001


"Wilhelm Fitzpatrick" <rafial at well.com> wrote in message
news:mailman.1002297908.5879.python-list at python.org...
> I was writing a little python script using
>
> for line in sys.stdin.readlines() :
>
> to iterate through input, and I noticed that I had to press ctrl-D
> TWICE to terminate input.  This same behavior appears whether I am in
> interactive mode or running a script, and I confirmed the same
> behavior using both python 2.0.1 and 1.5.2.
>
> Interestingly, enough
>
> for line in sys.stdin.read().splitlines() :
>
> terminates on the first ctrl-D (as I would expect).
>
> Why the strange behavior for readlines()?  is this an implemenation
> limitation?  A bug?  A feature that I do not properly understand?  I
> searched the docs and the mailing list archives but I could not find
> enlightenment.
>
Are you pressing the ^D at the end of a line of input, or at the start of a
blank line?

Technically ^D terminates transmission, and only raises EOF if this results
in an empty input buffer, which it does if entered before inputting any
characters.

Seemed to work as expected on my 2.1.1 cygwin, but 2.0 with Gonnerman's
alternative readline 1.4 for some reason doesn't seem to recognise ^D as an
input terminator in a readlines() loop, instead echoing chr(4) to the
console. In fact, I don't seem to be able to generate an EOF with
Gonnerman's code in a DOS command window.

Haven't got a Linux system to hand to test.

HTH

regards
Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list