Mercurial > cpython-lru_cache-weakref
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 |
| rev | line source |
|---|---|
| 12911 | 1 """curses.wrapper |
| 2 | |
| 3 Contains one function, wrapper(), which runs another function which | |
| 4 should be the rest of your curses-based application. If the | |
| 5 application raises an exception, wrapper() will restore the terminal | |
| 6 to a sane state so you can read the resulting traceback. | |
| 7 | |
| 8 """ | |
| 9 | |
| 10 import sys, curses | |
| 11 | |
| 12 def wrapper(func, *rest): | |
| 13 """Wrapper function that initializes curses and calls another function, | |
| 14 restoring normal keyboard/screen behavior on error. | |
| 15 The callable object 'func' is then passed the main window 'stdscr' | |
| 16 as its first argument, followed by any other arguments passed to | |
| 17 wrapper(). | |
| 18 """ | |
| 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 | 21 try: |
| 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 | 25 # Turn off echoing of keys, and enter cbreak mode, |
| 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 | 29 |
| 30 # In keypad mode, escape sequences for special keys | |
| 31 # (like the cursor keys) will be interpreted and | |
| 32 # a special value like curses.KEY_LEFT will be returned | |
| 33 stdscr.keypad(1) | |
| 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 | 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 |
