annotate Lib/curses/wrapper.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents c731e02472c5
children
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
45468
1d84bf5f9478 Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents: 32995
diff changeset
10 import curses
12911
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
11
32994
252c65469114 This is patch
Michael W. Hudson <mwh@python.net>
parents: 28513
diff changeset
12 def wrapper(func, *args, **kwds):
12911
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 """
25479
bbf8cf5ee9f9 Whitespace normalization (get rid of tabs).
Guido van Rossum <guido@python.org>
parents: 13909
diff changeset
19
12911
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
20 try:
25479
bbf8cf5ee9f9 Whitespace normalization (get rid of tabs).
Guido van Rossum <guido@python.org>
parents: 13909
diff changeset
21 # Initialize curses
63608
595676e466c0 Merged revisions 83536,83546-83548,83550,83554-83555,83558,83563,83565,83571,83574-83575 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
22 stdscr = curses.initscr()
25479
bbf8cf5ee9f9 Whitespace normalization (get rid of tabs).
Guido van Rossum <guido@python.org>
parents: 13909
diff changeset
23
bbf8cf5ee9f9 Whitespace normalization (get rid of tabs).
Guido van Rossum <guido@python.org>
parents: 13909
diff changeset
24 # Turn off echoing of keys, and enter cbreak mode,
bbf8cf5ee9f9 Whitespace normalization (get rid of tabs).
Guido van Rossum <guido@python.org>
parents: 13909
diff changeset
25 # 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
26 curses.noecho()
2ba76e315016 fix inconsistent use of tabs and spaces
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13027
diff changeset
27 curses.cbreak()
12911
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
28
25479
bbf8cf5ee9f9 Whitespace normalization (get rid of tabs).
Guido van Rossum <guido@python.org>
parents: 13909
diff changeset
29 # In keypad mode, escape sequences for special keys
bbf8cf5ee9f9 Whitespace normalization (get rid of tabs).
Guido van Rossum <guido@python.org>
parents: 13909
diff changeset
30 # (like the cursor keys) will be interpreted and
bbf8cf5ee9f9 Whitespace normalization (get rid of tabs).
Guido van Rossum <guido@python.org>
parents: 13909
diff changeset
31 # a special value like curses.KEY_LEFT will be returned
12911
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
32 stdscr.keypad(1)
3cee0d75191e Add contents of curses package
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
33
13909
b347fc5daeb7 Tweak curses.wrapper so it initializes colors if they are available.
Eric S. Raymond <esr@thyrsus.com>
parents: 13415
diff changeset
34 # 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
35 # 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
36 # 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
37 # 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
38 try:
b347fc5daeb7 Tweak curses.wrapper so it initializes colors if they are available.
Eric S. Raymond <esr@thyrsus.com>
parents: 13415
diff changeset
39 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
40 except:
b347fc5daeb7 Tweak curses.wrapper so it initializes colors if they are available.
Eric S. Raymond <esr@thyrsus.com>
parents: 13415
diff changeset
41 pass
b347fc5daeb7 Tweak curses.wrapper so it initializes colors if they are available.
Eric S. Raymond <esr@thyrsus.com>
parents: 13415
diff changeset
42
32995
1aa40a08982f Somehow (no idea how!) I missed half of patch #1005008. Sorry about that.
Michael W. Hudson <mwh@python.net>
parents: 32994
diff changeset
43 return func(stdscr, *args, **kwds)
32994
252c65469114 This is patch
Michael W. Hudson <mwh@python.net>
parents: 28513
diff changeset
44 finally:
252c65469114 This is patch
Michael W. Hudson <mwh@python.net>
parents: 28513
diff changeset
45 # Set everything back to normal
68338
c731e02472c5 Issue #5622: Fix curses.wrapper to raise correct exception if curses
Ned Deily <nad@acm.org>
parents: 63608
diff changeset
46 if 'stdscr' in locals():
c731e02472c5 Issue #5622: Fix curses.wrapper to raise correct exception if curses
Ned Deily <nad@acm.org>
parents: 63608
diff changeset
47 stdscr.keypad(0)
c731e02472c5 Issue #5622: Fix curses.wrapper to raise correct exception if curses
Ned Deily <nad@acm.org>
parents: 63608
diff changeset
48 curses.echo()
c731e02472c5 Issue #5622: Fix curses.wrapper to raise correct exception if curses
Ned Deily <nad@acm.org>
parents: 63608
diff changeset
49 curses.nocbreak()
c731e02472c5 Issue #5622: Fix curses.wrapper to raise correct exception if curses
Ned Deily <nad@acm.org>
parents: 63608
diff changeset
50 curses.endwin()