summaryrefslogtreecommitdiffstats
path: root/Lib/idlelib/CallTips.py
diff options
context:
space:
mode:
authorTerry Jan Reedy <tjreedy@udel.edu>2014-01-22 01:45:03 (GMT)
committerTerry Jan Reedy <tjreedy@udel.edu>2014-01-22 01:45:03 (GMT)
commit82c48e098aff18ab203ed7a22b17b38ea43992e1 (patch)
tree8c03166064aa7238c03b9f5dbe67ef3279d0c06a /Lib/idlelib/CallTips.py
parentedfd9addf6166cbc7406b95896d48cfdaf82adf7 (diff)
downloadcpython-82c48e098aff18ab203ed7a22b17b38ea43992e1.zip
cpython-82c48e098aff18ab203ed7a22b17b38ea43992e1.tar.gz
cpython-82c48e098aff18ab203ed7a22b17b38ea43992e1.tar.bz2
Issue #16638: Include up to 5 docstring header lines (before first blank) in
Idle calltips. This is needed for builtins, such 3.x bytes (which is why 5). Based on patch by Serhiy Storchaka.
Diffstat (limited to 'Lib/idlelib/CallTips.py')
-rw-r--r--Lib/idlelib/CallTips.py40
1 files changed, 26 insertions, 14 deletions
diff --git a/Lib/idlelib/CallTips.py b/Lib/idlelib/CallTips.py
index 4ac28ed..7197cf5 100644
--- a/Lib/idlelib/CallTips.py
+++ b/Lib/idlelib/CallTips.py
@@ -130,14 +130,24 @@ def _find_constructor(class_ob):
if rc is not None: return rc
return None
+# The following are used in get_arg_text
+_MAX_COLS = 79
+_MAX_LINES = 5 # enough for bytes
+
def get_arg_text(ob):
- """Get a string describing the arguments for the given object,
- only if it is callable."""
- arg_text = ""
+ '''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 first
+ empty line or _MAX_LINES. For builtins, this typically includes
+ the arguments in addition to the return value.
+ '''
+ argspec = ""
try:
ob_call = ob.__call__
except BaseException:
- return arg_text
+ return argspec
arg_offset = 0
if type(ob) in (types.ClassType, types.TypeType):
@@ -171,22 +181,24 @@ def get_arg_text(ob):
items.append("*args")
if fob.func_code.co_flags & 0x8:
items.append("**kwds")
- arg_text = ", ".join(items)
- arg_text = "(%s)" % re.sub("(?<!\d)\.\d+", "<tuple>", arg_text)
+ argspec = ", ".join(items)
+ argspec = "(%s)" % re.sub("(?<!\d)\.\d+", "<tuple>", argspec)
# See if we can use the docstring
if isinstance(ob_call, types.MethodType):
doc = ob_call.__doc__
else:
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
+ lines = [argspec] if argspec else []
+ for line in doc.split('\n', 5)[:_MAX_LINES]:
+ line = line.strip()
+ if not line:
+ break
+ if len(line) > _MAX_COLS:
+ line = line[: _MAX_COLS - 3] + '...'
+ lines.append(line)
+ argspec = '\n'.join(lines)
+ return argspec
if __name__ == '__main__':
from unittest import main