views:

50

answers:

1

I have this code:

def getch(self):
    if os.name == 'posix':
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(fd)
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    elif os.name == 'nt':
        ch = msvcrt.getch()
    return ch

This runs just fine on python 2.6 and 2.7 but whenever I try and test it on python 3.0 and up there is a new line printed out by the stdin.read call, I think this may because the python 3 changes to sys.stdin, stdout, and stderr but I'm not sure how to fix it

EDIT: running on OS X 10.6.4 python 3.1 and Ubuntu 9.04 python 2.6 this happened for me.

+1  A: 

This might be a platform-specific problem. Have you tried the code on different POSIX-based operating systems (e.g. Linux, BSD, Darwin, etc.). Are your results the same? They all handle terminal operations a little differently, so you might need to account for more than just posix vs. nt and go a little deeper.

jathanism
I tried to run the code on ubuntu 9.04 and the problem is exactly the same except it also occurs on python 2.6 on ubuntu.