"""Pop up a reminder of how to call a function.Call Tips are floating windows which display function, class, and methodparameter and docstring information when you type an opening parenthesis, andwhich disappear when you type a closing parenthesis."""import __main__import inspectimport reimport sysimport textwrapimport typesfrom idlelib import calltip_wfrom idlelib.hyperparser import HyperParserclass Calltip:def __init__(self, editwin=None):if editwin is None: # subprocess and testself.editwin = Noneelse:self.editwin = editwinself.text = editwin.textself.active_calltip = Noneself._calltip_window = self._make_tk_calltip_windowdef close(self):self._calltip_window = Nonedef _make_tk_calltip_window(self):# See __init__ for usagereturn calltip_w.CalltipWindow(self.text)def _remove_calltip_window(self, event=None):if self.active_calltip:self.active_calltip.hidetip()self.active_calltip = Nonedef force_open_calltip_event(self, event):"The user selected the menu entry or hotkey, open the tip."self.open_calltip(True)return "break"def try_open_calltip_event(self, event):"""Happens when it would be nice to open a calltip, but not reallynecessary, for example after an opening bracket, so function callswon't be made."""self.open_calltip(False)def refresh_calltip_event(self, event):if self.active_calltip and self.active_calltip.tipwindow:self.open_calltip(False)def open_calltip(self, evalfuncs):self._remove_calltip_window()hp = HyperParser(self.editwin, "insert")sur_paren = hp.get_surrounding_brackets('(')if not sur_paren:returnhp.set_index(sur_paren[0])expression = hp.get_expression()if not expression:returnif not evalfuncs and (expression.find('(') != -1):returnargspec = self.fetch_tip(expression)if not argspec:returnself.active_calltip = self._calltip_window()self.active_calltip.showtip(argspec, sur_paren[0], sur_paren[1])def fetch_tip(self, expression):"""Return the argument list and docstring of a function or class.If there is a Python subprocess, get the calltip there. Otherwise,either this fetch_tip() is running in the subprocess or it wascalled in an IDLE running without the subprocess.The subprocess environment is that of the most recently run script. Iftwo unrelated modules are being edited some calltips in the currentmodule may be inoperative if the module was not the last to run.To find methods, fetch_tip must be fed a fully qualified name."""try:rpcclt = self.editwin.flist.pyshell.interp.rpccltexcept AttributeError:rpcclt = Noneif rpcclt:return rpcclt.remotecall("exec", "get_the_calltip",(expression,), {})else:return get_argspec(get_entity(expression))def get_entity(expression):"""Return the object corresponding to expression evaluatedin a namespace spanning sys.modules and __main.dict__."""if expression:namespace = {**sys.modules, **__main__.__dict__}try:return eval(expression, namespace) # Only protect user code.except BaseException:# An uncaught exception closes idle, and eval can raise any# exception, especially if user classes are involved.return None# The following are used in get_argspec and some in tests_MAX_COLS = 85_MAX_LINES = 5 # enough for bytes_INDENT = ' '*4 # for wrapped signatures_first_param = re.compile(r'(?<=\()\w*,円?\s*')_default_callable_argspec = "See source or doc"_invalid_method = "invalid method signature"_argument_positional = " # '/' marks preceding args as positional-only."def get_argspec(ob):'''Return a string describing the signature of a callable object, or ''.For Python-coded functions and methods, the first line is introspected.Delete 'self' parameter for classes (.__init__) and bound methods.The next lines are the first lines of the doc string up to the firstempty line or _MAX_LINES. For builtins, this typically includesthe arguments in addition to the return value.'''argspec = default = ""try:ob_call = ob.__call__except BaseException:return defaultfob = ob_call if isinstance(ob_call, types.MethodType) else obtry:argspec = str(inspect.signature(fob))except ValueError as err:msg = str(err)if msg.startswith(_invalid_method):return _invalid_methodif '/' in argspec and len(argspec) < _MAX_COLS - len(_argument_positional):# Add explanation TODO remove after 3.7, before 3.9.argspec += _argument_positionalif isinstance(fob, type) and argspec == '()':# If fob has no argument, use default callable argspec.argspec = _default_callable_argspeclines = (textwrap.wrap(argspec, _MAX_COLS, subsequent_indent=_INDENT)if len(argspec) > _MAX_COLS else [argspec] if argspec else [])if isinstance(ob_call, types.MethodType):doc = ob_call.__doc__else:doc = getattr(ob, "__doc__", "")if doc:for line in doc.split('\n', _MAX_LINES)[:_MAX_LINES]:line = line.strip()if not line:breakif len(line) > _MAX_COLS:line = line[: _MAX_COLS - 3] + '...'lines.append(line)argspec = '\n'.join(lines)if not argspec:argspec = _default_callable_argspecreturn argspecif __name__ == '__main__':from unittest import mainmain('idlelib.idle_test.test_calltip', verbosity=2)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。