1 | n/a | """CallTips.py - An IDLE Extension to Jog Your Memory |
---|
2 | n/a | |
---|
3 | n/a | Call Tips are floating windows which display function, class, and method |
---|
4 | n/a | parameter and docstring information when you type an opening parenthesis, and |
---|
5 | n/a | which disappear when you type a closing parenthesis. |
---|
6 | n/a | |
---|
7 | n/a | """ |
---|
8 | n/a | import re |
---|
9 | n/a | import sys |
---|
10 | n/a | import types |
---|
11 | n/a | import inspect |
---|
12 | n/a | |
---|
13 | n/a | from idlelib import CallTipWindow |
---|
14 | n/a | from idlelib.HyperParser import HyperParser |
---|
15 | n/a | |
---|
16 | n/a | import __main__ |
---|
17 | n/a | |
---|
18 | n/a | class CallTips: |
---|
19 | n/a | |
---|
20 | n/a | menudefs = [ |
---|
21 | n/a | ('edit', [ |
---|
22 | n/a | ("Show call tip", "<<force-open-calltip>>"), |
---|
23 | n/a | ]) |
---|
24 | n/a | ] |
---|
25 | n/a | |
---|
26 | n/a | def __init__(self, editwin=None): |
---|
27 | n/a | if editwin is None: # subprocess and test |
---|
28 | n/a | self.editwin = None |
---|
29 | n/a | else: |
---|
30 | n/a | self.editwin = editwin |
---|
31 | n/a | self.text = editwin.text |
---|
32 | n/a | self.active_calltip = None |
---|
33 | n/a | self._calltip_window = self._make_tk_calltip_window |
---|
34 | n/a | |
---|
35 | n/a | def close(self): |
---|
36 | n/a | self._calltip_window = None |
---|
37 | n/a | |
---|
38 | n/a | def _make_tk_calltip_window(self): |
---|
39 | n/a | # See __init__ for usage |
---|
40 | n/a | return CallTipWindow.CallTip(self.text) |
---|
41 | n/a | |
---|
42 | n/a | def _remove_calltip_window(self, event=None): |
---|
43 | n/a | if self.active_calltip: |
---|
44 | n/a | self.active_calltip.hidetip() |
---|
45 | n/a | self.active_calltip = None |
---|
46 | n/a | |
---|
47 | n/a | def force_open_calltip_event(self, event): |
---|
48 | n/a | "The user selected the menu entry or hotkey, open the tip." |
---|
49 | n/a | self.open_calltip(True) |
---|
50 | n/a | |
---|
51 | n/a | def try_open_calltip_event(self, event): |
---|
52 | n/a | """Happens when it would be nice to open a CallTip, but not really |
---|
53 | n/a | necessary, for example after an opening bracket, so function calls |
---|
54 | n/a | won't be made. |
---|
55 | n/a | """ |
---|
56 | n/a | self.open_calltip(False) |
---|
57 | n/a | |
---|
58 | n/a | def refresh_calltip_event(self, event): |
---|
59 | n/a | if self.active_calltip and self.active_calltip.is_active(): |
---|
60 | n/a | self.open_calltip(False) |
---|
61 | n/a | |
---|
62 | n/a | def open_calltip(self, evalfuncs): |
---|
63 | n/a | self._remove_calltip_window() |
---|
64 | n/a | |
---|
65 | n/a | hp = HyperParser(self.editwin, "insert") |
---|
66 | n/a | sur_paren = hp.get_surrounding_brackets('(') |
---|
67 | n/a | if not sur_paren: |
---|
68 | n/a | return |
---|
69 | n/a | hp.set_index(sur_paren[0]) |
---|
70 | n/a | expression = hp.get_expression() |
---|
71 | n/a | if not expression: |
---|
72 | n/a | return |
---|
73 | n/a | if not evalfuncs and (expression.find('(') != -1): |
---|
74 | n/a | return |
---|
75 | n/a | argspec = self.fetch_tip(expression) |
---|
76 | n/a | if not argspec: |
---|
77 | n/a | return |
---|
78 | n/a | self.active_calltip = self._calltip_window() |
---|
79 | n/a | self.active_calltip.showtip(argspec, sur_paren[0], sur_paren[1]) |
---|
80 | n/a | |
---|
81 | n/a | def fetch_tip(self, expression): |
---|
82 | n/a | """Return the argument list and docstring of a function or class. |
---|
83 | n/a | |
---|
84 | n/a | If there is a Python subprocess, get the calltip there. Otherwise, |
---|
85 | n/a | either this fetch_tip() is running in the subprocess or it was |
---|
86 | n/a | called in an IDLE running without the subprocess. |
---|
87 | n/a | |
---|
88 | n/a | The subprocess environment is that of the most recently run script. If |
---|
89 | n/a | two unrelated modules are being edited some calltips in the current |
---|
90 | n/a | module may be inoperative if the module was not the last to run. |
---|
91 | n/a | |
---|
92 | n/a | To find methods, fetch_tip must be fed a fully qualified name. |
---|
93 | n/a | |
---|
94 | n/a | """ |
---|
95 | n/a | try: |
---|
96 | n/a | rpcclt = self.editwin.flist.pyshell.interp.rpcclt |
---|
97 | n/a | except AttributeError: |
---|
98 | n/a | rpcclt = None |
---|
99 | n/a | if rpcclt: |
---|
100 | n/a | return rpcclt.remotecall("exec", "get_the_calltip", |
---|
101 | n/a | (expression,), {}) |
---|
102 | n/a | else: |
---|
103 | n/a | return get_argspec(get_entity(expression)) |
---|
104 | n/a | |
---|
105 | n/a | def get_entity(expression): |
---|
106 | n/a | """Return the object corresponding to expression evaluated |
---|
107 | n/a | in a namespace spanning sys.modules and __main.dict__. |
---|
108 | n/a | """ |
---|
109 | n/a | if expression: |
---|
110 | n/a | namespace = sys.modules.copy() |
---|
111 | n/a | namespace.update(__main__.__dict__) |
---|
112 | n/a | try: |
---|
113 | n/a | return eval(expression, namespace) |
---|
114 | n/a | except BaseException: |
---|
115 | n/a | # An uncaught exception closes idle, and eval can raise any |
---|
116 | n/a | # exception, especially if user classes are involved. |
---|
117 | n/a | return None |
---|
118 | n/a | |
---|
119 | n/a | # The following are used in both get_argspec and tests |
---|
120 | n/a | _first_param = re.compile('(?<=\()\w*\,?\s*') |
---|
121 | n/a | _default_callable_argspec = "No docstring, see docs." |
---|
122 | n/a | |
---|
123 | n/a | def get_argspec(ob): |
---|
124 | n/a | '''Return a string describing the arguments and return of a callable object. |
---|
125 | n/a | |
---|
126 | n/a | For Python-coded functions and methods, the first line is introspected. |
---|
127 | n/a | Delete 'self' parameter for classes (.__init__) and bound methods. |
---|
128 | n/a | The last line is the first line of the doc string. For builtins, this typically |
---|
129 | n/a | includes the arguments in addition to the return value. |
---|
130 | n/a | |
---|
131 | n/a | ''' |
---|
132 | n/a | argspec = "" |
---|
133 | n/a | if hasattr(ob, '__call__'): |
---|
134 | n/a | if isinstance(ob, type): |
---|
135 | n/a | fob = getattr(ob, '__init__', None) |
---|
136 | n/a | elif isinstance(ob.__call__, types.MethodType): |
---|
137 | n/a | fob = ob.__call__ |
---|
138 | n/a | else: |
---|
139 | n/a | fob = ob |
---|
140 | n/a | if isinstance(fob, (types.FunctionType, types.MethodType)): |
---|
141 | n/a | argspec = inspect.formatargspec(*inspect.getfullargspec(fob)) |
---|
142 | n/a | if (isinstance(ob, (type, types.MethodType)) or |
---|
143 | n/a | isinstance(ob.__call__, types.MethodType)): |
---|
144 | n/a | argspec = _first_param.sub("", argspec) |
---|
145 | n/a | |
---|
146 | n/a | if isinstance(ob.__call__, types.MethodType): |
---|
147 | n/a | doc = ob.__call__.__doc__ |
---|
148 | n/a | else: |
---|
149 | n/a | doc = getattr(ob, "__doc__", "") |
---|
150 | n/a | if doc: |
---|
151 | n/a | doc = doc.lstrip() |
---|
152 | n/a | pos = doc.find("\n") |
---|
153 | n/a | if pos < 0 or pos > 70: |
---|
154 | n/a | pos = 70 |
---|
155 | n/a | if argspec: |
---|
156 | n/a | argspec += "\n" |
---|
157 | n/a | argspec += doc[:pos] |
---|
158 | n/a | if not argspec: |
---|
159 | n/a | argspec = _default_callable_argspec |
---|
160 | n/a | return argspec |
---|
161 | n/a | |
---|
162 | n/a | ################################################# |
---|
163 | n/a | # |
---|
164 | n/a | # Test code tests CallTips.fetch_tip, get_entity, and get_argspec |
---|
165 | n/a | |
---|
166 | n/a | def main(): |
---|
167 | n/a | # Putting expected in docstrings results in doubled tips for test |
---|
168 | n/a | def t1(): "()" |
---|
169 | n/a | def t2(a, b=None): "(a, b=None)" |
---|
170 | n/a | def t3(a, *args): "(a, *args)" |
---|
171 | n/a | def t4(*args): "(*args)" |
---|
172 | n/a | def t5(a, b=None, *args, **kw): "(a, b=None, *args, **kw)" |
---|
173 | n/a | |
---|
174 | n/a | class TC(object): |
---|
175 | n/a | "(ai=None, *b)" |
---|
176 | n/a | def __init__(self, ai=None, *b): "(self, ai=None, *b)" |
---|
177 | n/a | def t1(self): "(self)" |
---|
178 | n/a | def t2(self, ai, b=None): "(self, ai, b=None)" |
---|
179 | n/a | def t3(self, ai, *args): "(self, ai, *args)" |
---|
180 | n/a | def t4(self, *args): "(self, *args)" |
---|
181 | n/a | def t5(self, ai, b=None, *args, **kw): "(self, ai, b=None, *args, **kw)" |
---|
182 | n/a | def t6(no, self): "(no, self)" |
---|
183 | n/a | @classmethod |
---|
184 | n/a | def cm(cls, a): "(cls, a)" |
---|
185 | n/a | @staticmethod |
---|
186 | n/a | def sm(b): "(b)" |
---|
187 | n/a | def __call__(self, ci): "(self, ci)" |
---|
188 | n/a | |
---|
189 | n/a | tc = TC() |
---|
190 | n/a | |
---|
191 | n/a | # Python classes that inherit builtin methods |
---|
192 | n/a | class Int(int): "Int(x[, base]) -> integer" |
---|
193 | n/a | class List(list): "List() -> new empty list" |
---|
194 | n/a | # Simulate builtin with no docstring for default argspec test |
---|
195 | n/a | class SB: __call__ = None |
---|
196 | n/a | |
---|
197 | n/a | __main__.__dict__.update(locals()) # required for get_entity eval() |
---|
198 | n/a | |
---|
199 | n/a | num_tests = num_fail = 0 |
---|
200 | n/a | tip = CallTips().fetch_tip |
---|
201 | n/a | |
---|
202 | n/a | def test(expression, expected): |
---|
203 | n/a | nonlocal num_tests, num_fail |
---|
204 | n/a | num_tests += 1 |
---|
205 | n/a | argspec = tip(expression) |
---|
206 | n/a | if argspec != expected: |
---|
207 | n/a | num_fail += 1 |
---|
208 | n/a | fmt = "%s - expected\n%r\n - but got\n%r" |
---|
209 | n/a | print(fmt % (expression, expected, argspec)) |
---|
210 | n/a | |
---|
211 | n/a | def test_builtins(): |
---|
212 | n/a | # if first line of a possibly multiline compiled docstring changes, |
---|
213 | n/a | # must change corresponding test string |
---|
214 | n/a | test('int', "int(x=0) -> integer") |
---|
215 | n/a | test('Int', Int.__doc__) |
---|
216 | n/a | test('types.MethodType', "method(function, instance)") |
---|
217 | n/a | test('list', "list() -> new empty list") |
---|
218 | n/a | test('List', List.__doc__) |
---|
219 | n/a | test('list.__new__', |
---|
220 | n/a | 'T.__new__(S, ...) -> a new object with type S, a subtype of T') |
---|
221 | n/a | test('list.__init__', |
---|
222 | n/a | 'x.__init__(...) initializes x; see help(type(x)) for signature') |
---|
223 | n/a | append_doc = "L.append(object) -> None -- append object to end" |
---|
224 | n/a | test('list.append', append_doc) |
---|
225 | n/a | test('[].append', append_doc) |
---|
226 | n/a | test('List.append', append_doc) |
---|
227 | n/a | test('SB()', _default_callable_argspec) |
---|
228 | n/a | |
---|
229 | n/a | def test_funcs(): |
---|
230 | n/a | for func in (t1, t2, t3, t4, t5, TC,): |
---|
231 | n/a | fdoc = func.__doc__ |
---|
232 | n/a | test(func.__name__, fdoc + "\n" + fdoc) |
---|
233 | n/a | for func in (TC.t1, TC.t2, TC.t3, TC.t4, TC.t5, TC.t6, TC.sm, |
---|
234 | n/a | TC.__call__): |
---|
235 | n/a | fdoc = func.__doc__ |
---|
236 | n/a | test('TC.'+func.__name__, fdoc + "\n" + fdoc) |
---|
237 | n/a | fdoc = TC.cm.__func__.__doc__ |
---|
238 | n/a | test('TC.cm.__func__', fdoc + "\n" + fdoc) |
---|
239 | n/a | |
---|
240 | n/a | def test_methods(): |
---|
241 | n/a | # test that first parameter is correctly removed from argspec |
---|
242 | n/a | # using _first_param re to calculate expected masks re errors |
---|
243 | n/a | for meth, mdoc in ((tc.t1, "()"), (tc.t4, "(*args)"), (tc.t6, "(self)"), |
---|
244 | n/a | (TC.cm, "(a)"),): |
---|
245 | n/a | test('tc.'+meth.__name__, mdoc + "\n" + meth.__doc__) |
---|
246 | n/a | test('tc', "(ci)" + "\n" + tc.__call__.__doc__) |
---|
247 | n/a | # directly test that re works to delete unicode parameter name |
---|
248 | n/a | uni = "(A\u0391\u0410\u05d0\u0627\u0905\u1e00\u3042, a)" # various As |
---|
249 | n/a | assert _first_param.sub('', uni) == '(a)' |
---|
250 | n/a | |
---|
251 | n/a | def test_non_callables(): |
---|
252 | n/a | # expression evaluates, but not to a callable |
---|
253 | n/a | for expr in ('0', '0.0' 'num_tests', b'num_tests', '[]', '{}'): |
---|
254 | n/a | test(expr, '') |
---|
255 | n/a | # expression does not evaluate, but raises an exception |
---|
256 | n/a | for expr in ('1a', 'xyx', 'num_tests.xyz', '[int][1]', '{0:int}[1]'): |
---|
257 | n/a | test(expr, '') |
---|
258 | n/a | |
---|
259 | n/a | test_builtins() |
---|
260 | n/a | test_funcs() |
---|
261 | n/a | test_non_callables() |
---|
262 | n/a | test_methods() |
---|
263 | n/a | |
---|
264 | n/a | print("%d of %d tests failed" % (num_fail, num_tests)) |
---|
265 | n/a | |
---|
266 | n/a | if __name__ == '__main__': |
---|
267 | n/a | #main() |
---|
268 | n/a | from unittest import main |
---|
269 | n/a | main('idlelib.idle_test.test_calltips', verbosity=2, exit=False) |
---|