patch-nad0018 wrong error from curses.wrapper if curses initialization fails One way this happens is trying under IDLE.app in OS X: import curses def scr(a): a.getch() curses.wrapper(scr) Traceback before patch: UnboundLocalError: local variable 'stdscr' referenced before assignment Traceback after patch: _curses.error: setupterm: could not find terminal APPLIES 2.6, 2.7, 3.0, 3.1 diff -r bf28283300bf Lib/curses/wrapper.py --- Lib/curses/wrapper.py Tue Feb 10 18:00:24 2009 -0800 +++ Lib/curses/wrapper.py Tue Feb 10 18:01:18 2009 -0800 @@ -44,7 +44,8 @@ return func(stdscr, *args, **kwds) finally: # Set everything back to normal - stdscr.keypad(0) - curses.echo() - curses.nocbreak() - curses.endwin() + if 'stdscr' in locals(): + stdscr.keypad(0) + curses.echo() + curses.nocbreak() + curses.endwin()