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.

classification
Title: PyOS_Readline: treatment of "1234EOF"; behavior of builtin_raw_input
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: lucaspmelo, ncoghlan, vstinner
Priority: normal Keywords: needs review, patch

Created on 2009-07-12 11:57 by lucaspmelo, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
consider_1234EOF_empty_line.patch skrah, 2010-04-12 15:02
Messages (4)
msg90443 - (view) Author: Lucas Prado Melo (lucaspmelo) Date: 2009-07-12 11:57
----------------------------------
$ cat raw_input_test.py
s = ''
try:
    while True:
        c = raw_input()
	print c
	s += c
except EOFError:
    pass
$ python raw_input_test.py
test^D^Dtes
^D
$ python --version
Python 2.7a0
$ bash --version
GNU bash, version 3.2.48(1)-release (i486-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
----------------------------------

Surprisingly, though:

----------------------------------
$ python raw_input_test.py > output
test^D^D^D
$ cat output
test
----------------------------------
^D = Press Ctrl+D

I am using Ubuntu 9.04 (Jaunty Jackalope).
msg90644 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2009-07-17 22:39
Confirmed - it's actually pressing "Ctrl-D" after entering text on the
line that seems to cause strange behaviour. For example, in the
following, the only letters I typed were "test" and then I just pressed
"Ctrl-D" 4 times and got the output seen below:

$ ./python raw_input_test.py
testtes
Traceback (most recent call last):
  File "raw_input_test.py", line 3, in <module>
    c = raw_input()
EOFError

The "test" appeared as typed, the "tes\n" appeared after pressing Ctrl-D
three times, then the 4th Ctrl-D on its own line correctly triggered
EOFError.

(This with 2.7a0 on Ubuntu 8.04)
msg102950 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2010-04-12 15:02
There are two cases:

1) stdin and stdout go to a tty:

my_fgets() from Parser/myreadline.c does not handle the case
where fgets() reads some characters followed by an EOF. One
way to deal with this is to consider a sequence like 1234EOF
an empty line.

This is what the patch consider_1234EOF_empty_line.patch
does and it fixes problem 1.

[Another option would be to consider 1234EOF a line and
insert a newline.]


2) stdout is not a tty:

In this case, builtin_raw_input() uses PyFile_GetLine() to
read from stdin. I'm not sure why this is the case.

Perhaps 2) should be a separate issue.
msg372331 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-06-25 09:47
No activity for 10 years, I close the issue as out of date.
History
Date User Action Args
2022-04-11 14:56:50adminsetgithub: 50716
2020-06-25 09:47:19vstinnersetstatus: open -> closed

nosy: + vstinner
messages: + msg372331

resolution: out of date
stage: patch review -> resolved
2014-05-13 21:44:58skrahsetnosy: - skrah
2010-04-12 15:02:49skrahsetfiles: + consider_1234EOF_empty_line.patch


keywords: + needs review, patch
stage: patch review
title: raw_input() doesn't work as expected when it gets multiple ^D -> PyOS_Readline: treatment of "1234EOF"; behavior of builtin_raw_input
nosy: + skrah
messages: + msg102950
priority: normal
components: + Interpreter Core, - IO
type: behavior
2009-07-17 22:39:41ncoghlansetnosy: + ncoghlan
messages: + msg90644
2009-07-12 11:57:24lucaspmelocreate