[New-bugs-announce] [issue17337] input() and raw_input() do not work correctly with colored prompts

mic_e report at bugs.python.org
Sat Mar 2 16:09:24 CET 2013


New submission from mic_e:

With a prompt that uses ANSI color escape codes, python3 input() and python2 raw_input() behave incorrectly when it comes to the wrapping of the first line - almost certainly due to the wrong string length calculation.
The line breaking occurs k characters early, where k is the number of characters in the ANSI escape sequence.
Even worse, the line break does not switch to the next line, but instead jumps back to the beginning, overwriting the prompt and the previously written input.

How to reproduce:
Call input() with a color-coded string as argument, and type until you would expect the usual line break.

Example:

mic at mic-nb ~ $ python3
Python 3.3.0 (default, Dec 22 2012, 21:02:07) 
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> prompt="\x1b[31;1mthis is a bold red prompt> \x1b[m"
>>> len(prompt)
37
>>> input(prompt)
uvwxyzs a bold red prompt> abcdefghijklmnopqrst 
'abcdefghijklmnopqrstuvwxyz'
>>> 
mic at mic-nb ~ $ python2
Python 2.7.3 (default, Dec 22 2012, 21:14:12) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> prompt="\x1b[31;1mthis is a bold red prompt> \x1b[m"
>>> len(prompt)
37
>>> raw_input(prompt)
uvwxyzs a bold red prompt> abcdefghijklmnopqrst 
'abcdefghijklmnopqrstuvwxyz'
>>> 
mic at mic-nb ~ $ tput cols
57

I have typed directly after the prompt the string 'abcdefghijklmnopqrstuvwxyz',
so the expected result would be be
this is a bold red prompt> abcdefghijklmnopqrstuvwxyz
with four more characters of space before the line break

Note that the break occurs exactly 8 characters early, which is the total amount of ANSI escape sequence characters.
Also note that the readline module is impored in my .pyrc file.

----------
components: Library (Lib)
messages: 183323
nosy: mic_e
priority: normal
severity: normal
status: open
title: input() and raw_input() do not work correctly with colored prompts
type: behavior
versions: Python 2.7, Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17337>
_______________________________________


More information about the New-bugs-announce mailing list