From 152b3c2170f7575b13b516d017f950ee50d83a8f Mon Sep 17 00:00:00 2001 From: "Kurt B. Kaiser" Date: Thu, 30 Aug 2007 06:35:15 +0000 Subject: Use inspect.py and clean up. --- Lib/idlelib/CallTips.py | 138 +++++++++++++++++++++--------------------------- 1 file changed, 61 insertions(+), 77 deletions(-) diff --git a/Lib/idlelib/CallTips.py b/Lib/idlelib/CallTips.py index f166867..aee7e61 100644 --- a/Lib/idlelib/CallTips.py +++ b/Lib/idlelib/CallTips.py @@ -8,6 +8,7 @@ which disappear when you type a closing parenthesis. import re import sys import types +import inspect from idlelib import CallTipWindow from idlelib.HyperParser import HyperParser @@ -25,42 +26,37 @@ class CallTips: def __init__(self, editwin=None): if editwin is None: # subprocess and test self.editwin = None - return - self.editwin = editwin - self.text = editwin.text - self.calltip = None - self._make_calltip_window = self._make_tk_calltip_window + else: + self.editwin = editwin + self.text = editwin.text + self.active_calltip = None + self._calltip_window = self._make_tk_calltip_window def close(self): - self._make_calltip_window = None + self._calltip_window = None def _make_tk_calltip_window(self): # See __init__ for usage return CallTipWindow.CallTip(self.text) def _remove_calltip_window(self, event=None): - if self.calltip: - self.calltip.hidetip() - self.calltip = None + if self.active_calltip: + self.active_calltip.hidetip() + self.active_calltip = None def force_open_calltip_event(self, event): - """Happens when the user really wants to open a CallTip, even if a - function call is needed. - """ + "The user selected the menu entry or hotkey, open the tip." self.open_calltip(True) def try_open_calltip_event(self, event): """Happens when it would be nice to open a CallTip, but not really - neccesary, for example after an opening bracket, so function calls + necessary, for example after an opening bracket, so function calls won't be made. """ self.open_calltip(False) def refresh_calltip_event(self, event): - """If there is already a calltip window, check if it is still needed, - and if so, reload it. - """ - if self.calltip and self.calltip.is_active(): + if self.active_calltip and self.active_calltip.is_active(): self.open_calltip(False) def open_calltip(self, evalfuncs): @@ -72,20 +68,22 @@ class CallTips: return hp.set_index(sur_paren[0]) name = hp.get_expression() - if not name or (not evalfuncs and name.find('(') != -1): + if not name: + return + if not evalfuncs and (name.find('(') != -1): return - arg_text = self.fetch_tip(name) - if not arg_text: + argspec = self.fetch_tip(name) + if not argspec: return - self.calltip = self._make_calltip_window() - self.calltip.showtip(arg_text, sur_paren[0], sur_paren[1]) + self.active_calltip = self._calltip_window() + self.active_calltip.showtip(argspec, sur_paren[0], sur_paren[1]) def fetch_tip(self, name): - """Return the argument list and docstring of a function or class + """Return the argument list and docstring of a function or class. If there is a Python subprocess, get the calltip there. Otherwise, - either fetch_tip() is running in the subprocess itself or it was called - in an IDLE EditorWindow before any script had been run. + either this fetch_tip() is running in the subprocess or it was + called in an IDLE running without the subprocess. The subprocess environment is that of the most recently run script. If two unrelated modules are being edited some calltips in the current @@ -103,10 +101,10 @@ class CallTips: (name,), {}) else: entity = self.get_entity(name) - return get_arg_text(entity) + return get_argspec(entity) def get_entity(self, name): - "Lookup name in a namespace spanning sys.modules and __main.dict__" + "Lookup name in a namespace spanning sys.modules and __main.dict__." if name: namespace = sys.modules.copy() namespace.update(__main__.__dict__) @@ -116,83 +114,66 @@ class CallTips: return None def _find_constructor(class_ob): - # Given a class object, return a function object used for the - # constructor (ie, __init__() ) or None if we can't find one. + "Find the nearest __init__() in the class tree." try: return class_ob.__init__.im_func except AttributeError: for base in class_ob.__bases__: - rc = _find_constructor(base) - if rc is not None: return rc - return None - -def get_arg_text(ob): - """Get a string describing the arguments for the given object""" - arg_text = "" + init = _find_constructor(base) + if init: + return init + return None + +def get_argspec(ob): + """Get a string describing the arguments for the given object.""" + argspec = "" if ob is not None: - arg_offset = 0 if isinstance(ob, type): - # Look for the highest __init__ in the class chain. fob = _find_constructor(ob) if fob is None: fob = lambda: None - else: - arg_offset = 1 elif isinstance(ob, types.MethodType): - # bit of a hack for methods - turn it into a function - # but we drop the "self" param. fob = ob.im_func - arg_offset = 1 else: fob = ob - # Try to build one for Python defined functions if isinstance(fob, (types.FunctionType, types.LambdaType)): - argcount = fob.__code__.co_argcount - real_args = fob.__code__.co_varnames[arg_offset:argcount] - defaults = fob.__defaults__ or [] - defaults = list(map(lambda name: "=%s" % repr(name), defaults)) - defaults = [""] * (len(real_args) - len(defaults)) + defaults - items = list(map(lambda arg, dflt: arg + dflt, real_args, defaults)) - if fob.__code__.co_flags & 0x4: - items.append("...") - if fob.__code__.co_flags & 0x8: - items.append("***") - arg_text = ", ".join(items) - arg_text = "(%s)" % re.sub("\.\d+", "", arg_text) - # See if we can use the docstring + argspec = inspect.formatargspec(*inspect.getfullargspec(fob)) + pat = re.compile('self\,?\s*') + argspec = pat.sub("", argspec) doc = getattr(ob, "__doc__", "") if doc: doc = doc.lstrip() pos = doc.find("\n") if pos < 0 or pos > 70: pos = 70 - if arg_text: - arg_text += "\n" - arg_text += doc[:pos] - return arg_text + if argspec: + argspec += "\n" + argspec += doc[:pos] + return argspec ################################################# # # Test code # -if __name__=='__main__': - +def main(): def t1(): "()" def t2(a, b=None): "(a, b=None)" - def t3(a, *args): "(a, ...)" - def t4(*args): "(...)" - def t5(a, *args): "(a, ...)" - def t6(a, b=None, *args, **kw): "(a, b=None, ..., ***)" + def t3(a, *args): "(a, *args)" + def t4(*args): "(*args)" + def t5(a, *args): "(a, *args)" + def t6(a, b=None, *args, **kw): "(a, b=None, *args, **kw)" class TC(object): - "(ai=None, ...)" - def __init__(self, ai=None, *b): "(ai=None, ...)" + "(ai=None, *b)" + def __init__(self, ai=None, *b): "(ai=None, *b)" def t1(self): "()" def t2(self, ai, b=None): "(ai, b=None)" - def t3(self, ai, *args): "(ai, ...)" - def t4(self, *args): "(...)" - def t5(self, ai, *args): "(ai, ...)" - def t6(self, ai, b=None, *args, **kw): "(ai, b=None, ..., ***)" + def t3(self, ai, *args): "(ai, *args)" + def t4(self, *args): "(*args)" + def t5(self, ai, *args): "(ai, *args)" + def t6(self, ai, b=None, *args, **kw): "(ai, b=None, *args, **kw)" + + __main__.__dict__.update(locals()) def test(tests): ct = CallTips() @@ -200,16 +181,16 @@ if __name__=='__main__': for t in tests: expected = t.__doc__ + "\n" + t.__doc__ name = t.__name__ - # exercise fetch_tip(), not just get_arg_text() + # exercise fetch_tip(), not just get_argspec() try: qualified_name = "%s.%s" % (t.im_class.__name__, name) except AttributeError: qualified_name = name - arg_text = ct.fetch_tip(qualified_name) - if arg_text != expected: + argspec = ct.fetch_tip(qualified_name) + if argspec != expected: failed.append(t) fmt = "%s - expected %s, but got %s" - print(fmt % (t.__name__, expected, get_arg_text(t))) + print(fmt % (t.__name__, expected, get_argspec(t))) print("%d of %d tests failed" % (len(failed), len(tests))) tc = TC() @@ -217,3 +198,6 @@ if __name__=='__main__': TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6) test(tests) + +if __name__ == '__main__': + main() -- cgit v0.12