0

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.

# 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 > 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)

Here's a Gist link to it.

The problem is that when I press the è key (which has the ASCII code 232), it doesn't print just that character. Instead, the string ăè is printed to the given coordinates. I have tried using self.window.addstr(self.x, self.y, self.string[1]) but that just resulted in gibberish being printed.

I then fired up a Python prompt to see the return value of unichr(232).encode('utf-8') and it is indeed a string of length 2.

The very unexpected behaviour is that, if I put in screen.addstr(4, 4, unichr(232).encode(code)) in main it will correctly display the è character, and only that character. This is also the case if I make the write method of the AddCharCommand class to print the è character no matter what.

The problem, of course, is not limited to è only, it is pretty much the case with all the extended-ASCII characters.

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.

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.

Maybe a bit less important, but I would love it if someone could explain to me why screen.getch() 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.

Thank you very much in advance.

5
  • 1
    On a side note: There is no ASCII 232. ASCII is 7 bit from 0 thru 127. Mar 25, 2014 at 19:09
  • I think it is pretty clear that I mean extended-ASCII. Mar 25, 2014 at 19:55
  • 2
    There are 8 billions of extensions. The basic problem is that unichr(232) is without doubt u'\xe8', but as you call encode on it, it yields unichr(232).encode('utf-8') == '\xc3\xa8' (è) which surely is (a) correct, (b) not what you want. Apparently the part which is displaying the string expects ASCII, and as 'è' is no ASCII... Mar 25, 2014 at 20:04
  • I'm sorry but have you even read the whole thing? screen.addstr(1, 1, unichr(232).encode('utf-8')) correctly adds the character. I do NOT get an error because it is not ASCII. Mar 25, 2014 at 21:01
  • 1
    Python bug (fixed): curses implementation of Unicode is wrong in Python 3 might give you some ideas how to handle è
    – jfs
    Mar 25, 2014 at 21:08

1 Answer 1

2

It's working fine for me:

c=screen.get_wch()
screen.addch(c)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.