Python curses TypeError when exiting wrapper

﹥>﹥吖頭↗ 提交于 2019-12-25 04:24:28

问题


I'm running Mac OS X 10.9.5 and when wrapping my main() function with curses.wrapper I'm getting the following error after my program exits successfully:

Traceback (most recent call last):
  File "test.py", line 42, in <module>
     wrapper(main(SCREEN))
  File     "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/curses/__init__.py", line 94, in wrapper
    return func(stdscr, *args, **kwds)
TypeError: 'NoneType' object is not callable

Some amplifying code:

if __name__ == "__main__":
    # Initialize screen
    SCREEN = curses.initscr()

    # Run program with wrapper in case it fails
    wrapper(main(SCREEN))

    # Terminal cleanup
    curses.nocbreak()
    SCREEN.keypad(False)
    curses.echo()

If I use CTRL + C to attempt to exit the program while it's running, the exception is shown but the terminal remains in a disarrayed state (wrapper doesn't do it's job). Am I missing an something obvious here?

  • EDIT *

I confirmed this also happens on Ubuntu 14.10 server edition over a remote SSH terminal session.


回答1:


As far as I can see, you're calling the curses.wrapper function wrongly. From the documentation:

curses.wrapper(func, ...) Initialize curses and call another callable object, func, which should be the rest of your curses-using application. (...) The callable object func is then passed the main window ‘stdscr’ as its first argument, followed by any other arguments passed to wrapper().

In your example, it should look like this:

def main(SCREEN):
    ... # My program code

if __name__ == "__main__": 
    # The function main gets the stdscr passed by curses itself
    wrapper(main)

If you need to access to stdscr before the main call

I would not use wrapper in that case, but use curses.endwin() to deinitialize the curses library. Untested example:

SCREEN = curses.initscr()
# Modify your curses settings here

try:
    main(SCREEN)
except: # End curses session before raising the error
    curses.endwin()
    raise
else: # End curses session if program terminates normally
    curses.endwin()


来源:https://stackoverflow.com/questions/28751080/python-curses-typeerror-when-exiting-wrapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!