title: Python curses prints two characters when adding a utf-8 encoded string
body: <p>I have come across a very weird problem when trying to print UTF-8 encoded strings to a curses window. Here's the code, I'll talk about the exact problem and the things I have tried below.</p>
  
  <pre><code># coding=UTF-8
  import curses
  import locale
  import time
  locale.setlocale(locale.LC_ALL, '')
  code = locale.getpreferredencoding()
  
  
  
  class AddCharCommand(object):
      def __init__(self, window, line_start, y, x, character):
          """
          Command class for adding the specified character, to the specified
          window, at the specified coordinates.
          """
          self.window = window
          self.line_start = line_start
          self.x = x
          self.y = y
          self.character = character
  
  
      def write(self):
          if self.character &gt; 127:
              # curses somehow returns a keycode that is 64 lower than what it
              # should be, this takes care of the problem.
              self.character += 64
              self.string = unichr(self.character).encode(code)
              self.window.addstr(self.y, self.x, self.string)
          else:
               self.window.addch(self.y, self.x, self.character)
  
  
      def delete(self):
          """
          Erase characters usually print two characters to the curses window.
          As such both the character at these coordinates and the one next to it
          (that is the one self.x + 1) must be replaced with the a blank space.
          Move to cursor the original coordinates when done.
          """
          for i in xrange(2):
              self.window.addch(self.y, self.x + i, ord(' '))
          self.window.move(self.y, self.x)
  
  def main(screen):
      maxy, maxx = screen.getmaxyx()
      q = 0
      commands = list()
      x = 0
      erase = ord(curses.erasechar())
      while q != 27:
          q = screen.getch()
          if q == erase:
              command = commands.pop(-1).delete()
              x -= 1
              continue
          command = AddCharCommand(screen, 0, maxy/2, x, q)
          commands.append(command)
          command.write()
          x += 1
  
  curses.wrapper(main)
  </code></pre>
  
  <p>Here's a <a href="https://gist.github.com/canibanoglu/9767656" rel="nofollow" title="commands.py">Gist link</a> to it. </p>
  
  <p>The problem is that when I press the <code>è</code> key (which has the ASCII code 232), it doesn't print just that character. Instead, the string <code>ăè</code> is printed to the given coordinates. I have tried using <code>self.window.addstr(self.x, self.y, self.string[1])</code> but that just resulted in gibberish being printed.</p>
  
  <p>I then fired up a Python prompt to see the return value of <code>unichr(232).encode('utf-8')</code> and it is indeed a string of length 2. </p>
  
  <p>The <em>very</em> unexpected behaviour is that, if I put in <code>screen.addstr(4, 4, unichr(232).encode(code))</code> in <code>main</code> it will correctly display the <code>è</code> character, and only that character. This is also the case if I make the <code>write</code> method of the <code>AddCharCommand</code> class to print the <code>è</code> character no matter what. </p>
  
  <p>The problem, of course, is not limited to <code>è</code> only, it is pretty much the case with all the extended-ASCII characters. </p>
  
  <p>I know that extended-ASCII with curses is a bit flaky but I just can't understand this behaviour at all. It doesn't make any sense (to me) that the code works as expected if I hardcode the ASCII code, but it adds another character if I don't. </p>
  
  <p>I have looked around and read quite a lot of stuff on curses, but I haven't been able to find a solution to this. I would greatly appreciate any help on this matter, it is driving me mad.</p>
  
  <p>Maybe a bit less important, but I would love it if someone could explain to me why <code>screen.getch()</code> returns the incorrect ASCII code for characters above 127 and why the difference between the real ASCII code and the one returned by curses is 64.</p>
  
  <p>Thank you very much in advance.</p>