annotate Lib/curses/wrapper.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents b347fc5daeb7
children bbf8cf5ee9f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12911
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1 """curses.wrapper
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
2
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
3 Contains one function, wrapper(), which runs another function which
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
4 should be the rest of your curses-based application. If the
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
5 application raises an exception, wrapper() will restore the terminal
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
6 to a sane state so you can read the resulting traceback.
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
7
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
8 """
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
9
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
10 import sys, curses
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
11
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
12 def wrapper(func, *rest):
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
13 """Wrapper function that initializes curses and calls another function,
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
14 restoring normal keyboard/screen behavior on error.
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
15 The callable object 'func' is then passed the main window 'stdscr'
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
16 as its first argument, followed by any other arguments passed to
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
17 wrapper().
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
18 """
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
19
13027
bf2ee8567201 Drop back to old version of wrapper(); ESR reports that it broke things,
Andrew M. Kuchling <amk@amk.ca>
parents: 12914
diff changeset
20 res = None
12911
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
21 try:
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
22 # Initialize curses
13415
2ba76e315016 fix inconsistent use of tabs and spaces
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13027
diff changeset
23 stdscr=curses.initscr()
13027
bf2ee8567201 Drop back to old version of wrapper(); ESR reports that it broke things,
Andrew M. Kuchling <amk@amk.ca>
parents: 12914
diff changeset
24
12911
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
25 # Turn off echoing of keys, and enter cbreak mode,
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
26 # where no buffering is performed on keyboard input
13415
2ba76e315016 fix inconsistent use of tabs and spaces
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13027
diff changeset
27 curses.noecho()
2ba76e315016 fix inconsistent use of tabs and spaces
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13027
diff changeset
28 curses.cbreak()
12911
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
29
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
30 # In keypad mode, escape sequences for special keys
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
31 # (like the cursor keys) will be interpreted and
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
32 # a special value like curses.KEY_LEFT will be returned
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
33 stdscr.keypad(1)
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
34
13909
b347fc5daeb7 Tweak curses.wrapper so it initializes colors if they are available.
Eric S. Raymond <esr@thyrsus.com>
parents: 13415
diff changeset
35 # Start color, too. Harmless if the terminal doesn't have
b347fc5daeb7 Tweak curses.wrapper so it initializes colors if they are available.
Eric S. Raymond <esr@thyrsus.com>
parents: 13415
diff changeset
36 # color; user can test with has_color() later on. The try/catch
b347fc5daeb7 Tweak curses.wrapper so it initializes colors if they are available.
Eric S. Raymond <esr@thyrsus.com>
parents: 13415
diff changeset
37 # works around a minor bit of over-conscientiousness in the curses
b347fc5daeb7 Tweak curses.wrapper so it initializes colors if they are available.
Eric S. Raymond <esr@thyrsus.com>
parents: 13415
diff changeset
38 # module -- the error return from C start_color() is ignorable.
b347fc5daeb7 Tweak curses.wrapper so it initializes colors if they are available.
Eric S. Raymond <esr@thyrsus.com>
parents: 13415
diff changeset
39 try:
b347fc5daeb7 Tweak curses.wrapper so it initializes colors if they are available.
Eric S. Raymond <esr@thyrsus.com>
parents: 13415
diff changeset
40 curses.start_color()
b347fc5daeb7 Tweak curses.wrapper so it initializes colors if they are available.
Eric S. Raymond <esr@thyrsus.com>
parents: 13415
diff changeset
41 except:
b347fc5daeb7 Tweak curses.wrapper so it initializes colors if they are available.
Eric S. Raymond <esr@thyrsus.com>
parents: 13415
diff changeset
42 pass
b347fc5daeb7 Tweak curses.wrapper so it initializes colors if they are available.
Eric S. Raymond <esr@thyrsus.com>
parents: 13415
diff changeset
43
13027
bf2ee8567201 Drop back to old version of wrapper(); ESR reports that it broke things,
Andrew M. Kuchling <amk@amk.ca>
parents: 12914
diff changeset
44 res = apply(func, (stdscr,) + rest)
bf2ee8567201 Drop back to old version of wrapper(); ESR reports that it broke things,
Andrew M. Kuchling <amk@amk.ca>
parents: 12914
diff changeset
45 except:
bf2ee8567201 Drop back to old version of wrapper(); ESR reports that it broke things,
Andrew M. Kuchling <amk@amk.ca>
parents: 12914
diff changeset
46 # In the event of an error, restore the terminal
bf2ee8567201 Drop back to old version of wrapper(); ESR reports that it broke things,
Andrew M. Kuchling <amk@amk.ca>
parents: 12914
diff changeset
47 # to a sane state.
13415
2ba76e315016 fix inconsistent use of tabs and spaces
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13027
diff changeset
48 stdscr.keypad(0)
2ba76e315016 fix inconsistent use of tabs and spaces
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13027
diff changeset
49 curses.echo()
2ba76e315016 fix inconsistent use of tabs and spaces
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13027
diff changeset
50 curses.nocbreak()
2ba76e315016 fix inconsistent use of tabs and spaces
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13027
diff changeset
51 curses.endwin()
13027
bf2ee8567201 Drop back to old version of wrapper(); ESR reports that it broke things,
Andrew M. Kuchling <amk@amk.ca>
parents: 12914
diff changeset
52
bf2ee8567201 Drop back to old version of wrapper(); ESR reports that it broke things,
Andrew M. Kuchling <amk@amk.ca>
parents: 12914
diff changeset
53 # Pass the exception upwards
bf2ee8567201 Drop back to old version of wrapper(); ESR reports that it broke things,
Andrew M. Kuchling <amk@amk.ca>
parents: 12914
diff changeset
54 (exc_type, exc_value, exc_traceback) = sys.exc_info()
bf2ee8567201 Drop back to old version of wrapper(); ESR reports that it broke things,
Andrew M. Kuchling <amk@amk.ca>
parents: 12914
diff changeset
55 raise exc_type, exc_value, exc_traceback
bf2ee8567201 Drop back to old version of wrapper(); ESR reports that it broke things,
Andrew M. Kuchling <amk@amk.ca>
parents: 12914
diff changeset
56 else:
bf2ee8567201 Drop back to old version of wrapper(); ESR reports that it broke things,
Andrew M. Kuchling <amk@amk.ca>
parents: 12914
diff changeset
57 # Set everything back to normal
13415
2ba76e315016 fix inconsistent use of tabs and spaces
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13027
diff changeset
58 stdscr.keypad(0)
2ba76e315016 fix inconsistent use of tabs and spaces
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13027
diff changeset
59 curses.echo()
2ba76e315016 fix inconsistent use of tabs and spaces
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13027
diff changeset
60 curses.nocbreak()
2ba76e315016 fix inconsistent use of tabs and spaces
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13027
diff changeset
61 curses.endwin() # Terminate curses
12911
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
62
13027
bf2ee8567201 Drop back to old version of wrapper(); ESR reports that it broke things,
Andrew M. Kuchling <amk@amk.ca>
parents: 12914
diff changeset
63 return res