ยปCore Development>Code coverage>Lib/curses/__init__.py

Python code coverage for Lib/curses/__init__.py

#countcontent
1n/a"""curses
2n/a
3n/aThe main package for curses support for Python. Normally used by importing
4n/athe package, and perhaps a particular module inside it.
5n/a
6n/a import curses
7n/a from curses import textpad
8n/a curses.initscr()
9n/a ...
10n/a
11n/a"""
12n/a
13n/afrom _curses import *
14n/aimport os as _os
15n/aimport sys as _sys
16n/a
17n/a# Some constants, most notably the ACS_* ones, are only added to the C
18n/a# _curses module's dictionary after initscr() is called. (Some
19n/a# versions of SGI's curses don't define values for those constants
20n/a# until initscr() has been called.) This wrapper function calls the
21n/a# underlying C initscr(), and then copies the constants from the
22n/a# _curses module to the curses package's dictionary. Don't do 'from
23n/a# curses import *' if you'll be needing the ACS_* constants.
24n/a
25n/adef initscr():
26n/a import _curses, curses
27n/a # we call setupterm() here because it raises an error
28n/a # instead of calling exit() in error cases.
29n/a setupterm(term=_os.environ.get("TERM", "unknown"),
30n/a fd=_sys.__stdout__.fileno())
31n/a stdscr = _curses.initscr()
32n/a for key, value in _curses.__dict__.items():
33n/a if key[0:4] == 'ACS_' or key in ('LINES', 'COLS'):
34n/a setattr(curses, key, value)
35n/a
36n/a return stdscr
37n/a
38n/a# This is a similar wrapper for start_color(), which adds the COLORS and
39n/a# COLOR_PAIRS variables which are only available after start_color() is
40n/a# called.
41n/a
42n/adef start_color():
43n/a import _curses, curses
44n/a retval = _curses.start_color()
45n/a if hasattr(_curses, 'COLORS'):
46n/a curses.COLORS = _curses.COLORS
47n/a if hasattr(_curses, 'COLOR_PAIRS'):
48n/a curses.COLOR_PAIRS = _curses.COLOR_PAIRS
49n/a return retval
50n/a
51n/a# Import Python has_key() implementation if _curses doesn't contain has_key()
52n/a
53n/atry:
54n/a has_key
55n/aexcept NameError:
56n/a from .has_key import has_key
57n/a
58n/a# Wrapper for the entire curses-based application. Runs a function which
59n/a# should be the rest of your curses-based application. If the application
60n/a# raises an exception, wrapper() will restore the terminal to a sane state so
61n/a# you can read the resulting traceback.
62n/a
63n/adef wrapper(func, *args, **kwds):
64n/a """Wrapper function that initializes curses and calls another function,
65n/a restoring normal keyboard/screen behavior on error.
66n/a The callable object 'func' is then passed the main window 'stdscr'
67n/a as its first argument, followed by any other arguments passed to
68n/a wrapper().
69n/a """
70n/a
71n/a try:
72n/a # Initialize curses
73n/a stdscr = initscr()
74n/a
75n/a # Turn off echoing of keys, and enter cbreak mode,
76n/a # where no buffering is performed on keyboard input
77n/a noecho()
78n/a cbreak()
79n/a
80n/a # In keypad mode, escape sequences for special keys
81n/a # (like the cursor keys) will be interpreted and
82n/a # a special value like curses.KEY_LEFT will be returned
83n/a stdscr.keypad(1)
84n/a
85n/a # Start color, too. Harmless if the terminal doesn't have
86n/a # color; user can test with has_color() later on. The try/catch
87n/a # works around a minor bit of over-conscientiousness in the curses
88n/a # module -- the error return from C start_color() is ignorable.
89n/a try:
90n/a start_color()
91n/a except:
92n/a pass
93n/a
94n/a return func(stdscr, *args, **kwds)
95n/a finally:
96n/a # Set everything back to normal
97n/a if 'stdscr' in locals():
98n/a stdscr.keypad(0)
99n/a echo()
100n/a nocbreak()
101n/a endwin()