classification
Title: Document the need to pass the prompt to raw_input() with readline
Type: behavior Stage: needs patch
Components: Documentation Versions: Python 3.6, Python 3.5, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: berker.peksag, dhanamp, docs@python, eric.araujo, idank, kiilerix, martin.panter, nadeem.vawda, pitrou
Priority: normal Keywords: patch

Created on 2011-08-24 17:00 by idank, last changed 2016-06-21 02:10 by martin.panter.

Files
File name Uploaded Description Edit
issue12833.patch dhanamp, 2014-06-07 19:18 Documentation patch for readline module review
Repositories containing patches
http://hg.python.org/cpython
Messages (11)
msg142887 - (view) Author: Idan Kamara (idank) Date: 2011-08-24 17:00
import sys, readline

sys.stdout.write('foo ')
raw_input()

When trying the above on Debian, 2.6.6 using gnome-terminal, typing a character then hitting backspace deletes "foo " as well.

I'm not sure if this is a bug or the expected behavior when writing to stdout directly rather than passing the string to raw_input() (for my particular use case that's not an option though).

One possible workaround seems to be to delete the trailing space from write() and move it to raw_input:

sys.stdout.write('foo')
raw_input(' ')

Then backspace seems to work properly. This has something to do with readline because when it's not imported, it also works as expected (but other things break obviously).
msg143015 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-08-26 15:15
Maybe you need to call sys.stdin.flush() before raw_input?

In any way, 2.6 is in security mode, so we need to reproduce this with current versions: 2.7, 3.2 or 3.3.
msg143025 - (view) Author: Idan Kamara (idank) Date: 2011-08-26 18:57
Reproduced on 2.7.

(flushing stdin/out doesn't help)
msg143058 - (view) Author: Nadeem Vawda (nadeem.vawda) * (Python committer) Date: 2011-08-27 10:45
Reproduced on 3.3 head. Looking at the documentation of the C readline
library, it needs to know the length of the prompt in order to display
properly, so this seems to be an acknowledged limitation of the underlying
library rather than a bug on our side.

Still, this behavior is surprising and undesirable. I would suggest adding
a note to the docs for the readline module, directing users to write:

    input("foo> ")

instead of:

    sys.stdout.write("foo> ")
    input()
msg143059 - (view) Author: Idan Kamara (idank) Date: 2011-08-27 11:03
You're right, as this little C program verifies:

#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>

int main() {
   printf("foo ");
   char* buf = readline("");
   free(buf);

   return 0;
}

Passing ' ' seems to be a suitable workaround for those who can't pass the text directly to raw_input though (such is the case where you have special classes who handle output).
msg143157 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-08-29 16:18
> Still, this behavior is surprising and undesirable. I would suggest
> adding a note to the docs for the readline module
+1.
msg219962 - (view) Author: Dhanam Prakash (dhanamp) Date: 2014-06-07 19:18
Hi,
submitting a patch for the documentation.
Thanks
msg245160 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-06-11 08:57
The patch looks find for Python 3. The sample code should probably be adapted to raw_input() for Python 2.
msg245161 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-06-11 09:07
Actually, there should either be a space before the double-colons, or the full stops should be removed. So either of these options:

. . . when a backspace is typed. ::
. . . when a backspace is typed::
msg247686 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2015-07-30 16:07
Also, I'd change the patch to use a note directive.
msg249382 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-08-31 04:41
I wonder if this information would be better off under the input() function, rather than under the Readline module itself.

Also see Issue 17337, about control codes in the Readline prompt. Since these issues both relate to the prompt, it might be worth documenting both in the same section or paragraph.
History
Date User Action Args
2016-06-21 02:10:31martin.pantersettitle: raw_input misbehaves when readline is imported -> Document the need to pass the prompt to raw_input() with readline
stage: patch review -> needs patch
versions: - Python 3.4
2015-08-31 04:41:07martin.pantersetmessages: + msg249382
2015-07-30 16:07:37berker.peksagsetnosy: + berker.peksag

messages: + msg247686
stage: commit review -> patch review
2015-06-11 09:07:39martin.pantersetmessages: + msg245161
2015-06-11 08:57:50martin.pantersetversions: + Python 3.4, Python 3.5, Python 3.6, - Python 3.2, Python 3.3
nosy: + martin.panter

messages: + msg245160

stage: needs patch -> commit review
2014-06-07 19:18:11dhanampsetfiles: + issue12833.patch

nosy: + dhanamp
messages: + msg219962

hgrepos: + hgrepo254
keywords: + patch
2011-08-29 16:18:28eric.araujosetassignee: docs@python
components: + Documentation, - Interpreter Core, IO
versions: + Python 3.2, Python 3.3
nosy: + docs@python

messages: + msg143157
stage: test needed -> needs patch
2011-08-27 11:03:07idanksetmessages: + msg143059
2011-08-27 10:45:47nadeem.vawdasetnosy: + nadeem.vawda
messages: + msg143058
2011-08-26 18:57:45idanksetmessages: + msg143025
versions: + Python 2.7
2011-08-26 15:15:33eric.araujosetversions: - Python 2.6
nosy: + eric.araujo, pitrou

messages: + msg143015

components: + Interpreter Core, IO, - Library (Lib)
stage: test needed
2011-08-25 22:54:44kiilerixsetnosy: + kiilerix
2011-08-24 17:00:11idankcreate