Issue 8862: curses.wrapper does not restore terminal if curses.getkey() gets KeyboardInterrupt (original) (raw)

Run test.py (below) in terminal, and interrupt it with Ctrl-C.

Result: terminal settings are not restored (checked with linux console and xterm, with Python 2.7 and 3.2).

test.py

Broke it with KeyboardInterrupt

import curses def main(screen): k = screen.getkey() curses.wrapper(main)

Results are hardly readable due to the broken terminal.

Something about KeyboardInterrupt

However, if getkey() is surrounded by try-except, behavior changes:

test2.py

Broke it with KeyboardInterrupt

import curses def main2(screen): try: k = screen.getkey() except KeyboardInterrupt: raise curses.wrapper(main2)

Terminal is restored to its normal state.

In python3.2 test2.py results in traceback:

Traceback (most recent call last): File "test2.py", line 4, in main2 k = screen.getkey() _curses.error: no input

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "test2.py", line 7, in curses.wrapper(main2) File "/usr/local/lib/python3.2/curses/wrapper.py", line 44, in wrapper return func(stdscr, *args, **kwds) File "test2.py", line 4, in main2 k = screen.getkey() KeyboardInterrupt

In 2.7 it results only in the latest part of traceback:

Traceback (most recent call last): File "test2.py", line 7, in curses.wrapper(main2) File "/usr/local/lib/python2.7/curses/wrapper.py", line 44, in wrapper return func(stdscr, *args, **kwds) File "test2.py", line 4, in main2 k = screen.getkey() KeyboardInterrupt

The problem is that instead of a single KeyboardInterrupt, two exceptions are raised: KeyboardInterrupt and _curses.error('no input').

Possible solution is to suppress _curses.error in this case (since it is less relevant than KeyboardInterrupt, IMO).