ยปCore Development>Code coverage>Lib/idlelib/calltips.py

Python code coverage for Lib/idlelib/calltips.py

#countcontent
1n/a"""calltips.py - An IDLE Extension to Jog Your Memory
2n/a
3n/aCall Tips are floating windows which display function, class, and method
4n/aparameter and docstring information when you type an opening parenthesis, and
5n/awhich disappear when you type a closing parenthesis.
6n/a
7n/a"""
8n/aimport inspect
9n/aimport re
10n/aimport sys
11n/aimport textwrap
12n/aimport types
13n/a
14n/afrom idlelib import calltip_w
15n/afrom idlelib.hyperparser import HyperParser
16n/aimport __main__
17n/a
18n/aclass CallTips:
19n/a
20n/a menudefs = [
21n/a ('edit', [
22n/a ("Show call tip", "<<force-open-calltip>>"),
23n/a ])
24n/a ]
25n/a
26n/a def __init__(self, editwin=None):
27n/a if editwin is None: # subprocess and test
28n/a self.editwin = None
29n/a else:
30n/a self.editwin = editwin
31n/a self.text = editwin.text
32n/a self.active_calltip = None
33n/a self._calltip_window = self._make_tk_calltip_window
34n/a
35n/a def close(self):
36n/a self._calltip_window = None
37n/a
38n/a def _make_tk_calltip_window(self):
39n/a # See __init__ for usage
40n/a return calltip_w.CallTip(self.text)
41n/a
42n/a def _remove_calltip_window(self, event=None):
43n/a if self.active_calltip:
44n/a self.active_calltip.hidetip()
45n/a self.active_calltip = None
46n/a
47n/a def force_open_calltip_event(self, event):
48n/a "The user selected the menu entry or hotkey, open the tip."
49n/a self.open_calltip(True)
50n/a
51n/a def try_open_calltip_event(self, event):
52n/a """Happens when it would be nice to open a CallTip, but not really
53n/a necessary, for example after an opening bracket, so function calls
54n/a won't be made.
55n/a """
56n/a self.open_calltip(False)
57n/a
58n/a def refresh_calltip_event(self, event):
59n/a if self.active_calltip and self.active_calltip.is_active():
60n/a self.open_calltip(False)
61n/a
62n/a def open_calltip(self, evalfuncs):
63n/a self._remove_calltip_window()
64n/a
65n/a hp = HyperParser(self.editwin, "insert")
66n/a sur_paren = hp.get_surrounding_brackets('(')
67n/a if not sur_paren:
68n/a return
69n/a hp.set_index(sur_paren[0])
70n/a expression = hp.get_expression()
71n/a if not expression:
72n/a return
73n/a if not evalfuncs and (expression.find('(') != -1):
74n/a return
75n/a argspec = self.fetch_tip(expression)
76n/a if not argspec:
77n/a return
78n/a self.active_calltip = self._calltip_window()
79n/a self.active_calltip.showtip(argspec, sur_paren[0], sur_paren[1])
80n/a
81n/a def fetch_tip(self, expression):
82n/a """Return the argument list and docstring of a function or class.
83n/a
84n/a If there is a Python subprocess, get the calltip there. Otherwise,
85n/a either this fetch_tip() is running in the subprocess or it was
86n/a called in an IDLE running without the subprocess.
87n/a
88n/a The subprocess environment is that of the most recently run script. If
89n/a two unrelated modules are being edited some calltips in the current
90n/a module may be inoperative if the module was not the last to run.
91n/a
92n/a To find methods, fetch_tip must be fed a fully qualified name.
93n/a
94n/a """
95n/a try:
96n/a rpcclt = self.editwin.flist.pyshell.interp.rpcclt
97n/a except AttributeError:
98n/a rpcclt = None
99n/a if rpcclt:
100n/a return rpcclt.remotecall("exec", "get_the_calltip",
101n/a (expression,), {})
102n/a else:
103n/a return get_argspec(get_entity(expression))
104n/a
105n/adef get_entity(expression):
106n/a """Return the object corresponding to expression evaluated
107n/a in a namespace spanning sys.modules and __main.dict__.
108n/a """
109n/a if expression:
110n/a namespace = sys.modules.copy()
111n/a namespace.update(__main__.__dict__)
112n/a try:
113n/a return eval(expression, namespace)
114n/a except BaseException:
115n/a # An uncaught exception closes idle, and eval can raise any
116n/a # exception, especially if user classes are involved.
117n/a return None
118n/a
119n/a# The following are used in get_argspec and some in tests
120n/a_MAX_COLS = 85
121n/a_MAX_LINES = 5 # enough for bytes
122n/a_INDENT = ' '*4 # for wrapped signatures
123n/a_first_param = re.compile(r'(?<=\()\w*\,?\s*')
124n/a_default_callable_argspec = "See source or doc"
125n/a
126n/a
127n/adef get_argspec(ob):
128n/a '''Return a string describing the signature of a callable object, or ''.
129n/a
130n/a For Python-coded functions and methods, the first line is introspected.
131n/a Delete 'self' parameter for classes (.__init__) and bound methods.
132n/a The next lines are the first lines of the doc string up to the first
133n/a empty line or _MAX_LINES. For builtins, this typically includes
134n/a the arguments in addition to the return value.
135n/a '''
136n/a argspec = ""
137n/a try:
138n/a ob_call = ob.__call__
139n/a except BaseException:
140n/a return argspec
141n/a if isinstance(ob, type):
142n/a fob = ob.__init__
143n/a elif isinstance(ob_call, types.MethodType):
144n/a fob = ob_call
145n/a else:
146n/a fob = ob
147n/a if isinstance(fob, (types.FunctionType, types.MethodType)):
148n/a argspec = inspect.formatargspec(*inspect.getfullargspec(fob))
149n/a if (isinstance(ob, (type, types.MethodType)) or
150n/a isinstance(ob_call, types.MethodType)):
151n/a argspec = _first_param.sub("", argspec)
152n/a
153n/a lines = (textwrap.wrap(argspec, _MAX_COLS, subsequent_indent=_INDENT)
154n/a if len(argspec) > _MAX_COLS else [argspec] if argspec else [])
155n/a
156n/a if isinstance(ob_call, types.MethodType):
157n/a doc = ob_call.__doc__
158n/a else:
159n/a doc = getattr(ob, "__doc__", "")
160n/a if doc:
161n/a for line in doc.split('\n', _MAX_LINES)[:_MAX_LINES]:
162n/a line = line.strip()
163n/a if not line:
164n/a break
165n/a if len(line) > _MAX_COLS:
166n/a line = line[: _MAX_COLS - 3] + '...'
167n/a lines.append(line)
168n/a argspec = '\n'.join(lines)
169n/a if not argspec:
170n/a argspec = _default_callable_argspec
171n/a return argspec
172n/a
173n/aif __name__ == '__main__':
174n/a from unittest import main
175n/a main('idlelib.idle_test.test_calltips', verbosity=2)