title: How to fix column calculation in Python readline if using color prompt
body: <p>I use standard tips for customizing interactive Python session:</p>
  
  <pre>
  $ cat ~/.bashrc
  export PYTHONSTARTUP=~/.pystartup
  
  $ cat ~/.pystartup
  import os
  import sys
  import atexit
  import readline
  import rlcompleter
  
  historyPath = os.path.expanduser("~/.pyhistory")
  
  def save_history(historyPath=historyPath):
      import readline
      readline.write_history_file(historyPath)
  
  if os.path.exists(historyPath):
      readline.read_history_file(historyPath)
  
  term_with_colors = ['xterm', 'xterm-color', 'xterm-256color', 'linux', 'screen', 'screen-256color', 'screen-bce']
  if os.environ.get('TERM') in term_with_colors:
      green='\033[32m'
      red='\033[31m'
      reset='\033[0m'
      sys.ps1 = red + '>>> ' + reset
      sys.ps2 = green + '... ' + reset
  del term_with_colors
  
  atexit.register(save_history)
  del os, sys, atexit, readline, rlcompleter, save_history, historyPath
  </pre>
  
  <p>Now I get context sensitive completion and color prompt.</p>
  
  <p>The problem comes from color prompt - when I invoke <strong>history-search-backward</strong> (by pressing <kbd>UP</kbd>) in an interactive Python session, Readline takes in account terminal escape sequences, so the cursor position is wrongly calculated and the text is wrongly displayed.</p>
  
  <p>In Bash man page this problem is mentioned and fixed by special markers:</p>
  
  <pre>
      \[     begin a sequence of non-printing characters,
             which could be used to embed a
             terminal control sequence into the prompt
      \]     end a sequence of non-printing characters
  </pre>
  
  <p>How to fix this issue for Python prompt?</p>