Author mic_e
Recipients mic_e
Date 2013-03-02.15:09:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1362236964.85.0.134127148195.issue17337@psf.upfronthosting.co.za>
In-reply-to
Content
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@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@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@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.
History
Date User Action Args
2013-03-02 15:09:24mic_esetrecipients: + mic_e
2013-03-02 15:09:24mic_esetmessageid: <1362236964.85.0.134127148195.issue17337@psf.upfronthosting.co.za>
2013-03-02 15:09:24mic_elinkissue17337 messages
2013-03-02 15:09:23mic_ecreate