ยปCore Development>Code coverage>Lib/_sitebuiltins.py

Python code coverage for Lib/_sitebuiltins.py

#countcontent
1n/a"""
2n/aThe objects used by the site module to add custom builtins.
3n/a"""
4n/a
5n/a# Those objects are almost immortal and they keep a reference to their module
6n/a# globals. Defining them in the site module would keep too many references
7n/a# alive.
8n/a# Note this means this module should also avoid keep things alive in its
9n/a# globals.
10n/a
11n/aimport sys
12n/a
13n/aclass Quitter(object):
14n/a def __init__(self, name, eof):
15n/a self.name = name
16n/a self.eof = eof
17n/a def __repr__(self):
18n/a return 'Use %s() or %s to exit' % (self.name, self.eof)
19n/a def __call__(self, code=None):
20n/a # Shells like IDLE catch the SystemExit, but listen when their
21n/a # stdin wrapper is closed.
22n/a try:
23n/a sys.stdin.close()
24n/a except:
25n/a pass
26n/a raise SystemExit(code)
27n/a
28n/a
29n/aclass _Printer(object):
30n/a """interactive prompt objects for printing the license text, a list of
31n/a contributors and the copyright notice."""
32n/a
33n/a MAXLINES = 23
34n/a
35n/a def __init__(self, name, data, files=(), dirs=()):
36n/a import os
37n/a self.__name = name
38n/a self.__data = data
39n/a self.__lines = None
40n/a self.__filenames = [os.path.join(dir, filename)
41n/a for dir in dirs
42n/a for filename in files]
43n/a
44n/a def __setup(self):
45n/a if self.__lines:
46n/a return
47n/a data = None
48n/a for filename in self.__filenames:
49n/a try:
50n/a with open(filename, "r") as fp:
51n/a data = fp.read()
52n/a break
53n/a except OSError:
54n/a pass
55n/a if not data:
56n/a data = self.__data
57n/a self.__lines = data.split('\n')
58n/a self.__linecnt = len(self.__lines)
59n/a
60n/a def __repr__(self):
61n/a self.__setup()
62n/a if len(self.__lines) <= self.MAXLINES:
63n/a return "\n".join(self.__lines)
64n/a else:
65n/a return "Type %s() to see the full %s text" % ((self.__name,)*2)
66n/a
67n/a def __call__(self):
68n/a self.__setup()
69n/a prompt = 'Hit Return for more, or q (and Return) to quit: '
70n/a lineno = 0
71n/a while 1:
72n/a try:
73n/a for i in range(lineno, lineno + self.MAXLINES):
74n/a print(self.__lines[i])
75n/a except IndexError:
76n/a break
77n/a else:
78n/a lineno += self.MAXLINES
79n/a key = None
80n/a while key is None:
81n/a key = input(prompt)
82n/a if key not in ('', 'q'):
83n/a key = None
84n/a if key == 'q':
85n/a break
86n/a
87n/a
88n/aclass _Helper(object):
89n/a """Define the builtin 'help'.
90n/a
91n/a This is a wrapper around pydoc.help that provides a helpful message
92n/a when 'help' is typed at the Python interactive prompt.
93n/a
94n/a Calling help() at the Python prompt starts an interactive help session.
95n/a Calling help(thing) prints help for the python object 'thing'.
96n/a """
97n/a
98n/a def __repr__(self):
99n/a return "Type help() for interactive help, " \
100n/a "or help(object) for help about object."
101n/a def __call__(self, *args, **kwds):
102n/a import pydoc
103n/a return pydoc.help(*args, **kwds)