From 7819bd21ca32fb2b9a32bf124a99e509be3ce136 Mon Sep 17 00:00:00 2001 From: Jordan Wiens Date: Tue, 10 Sep 2019 23:32:41 -0400 Subject: better error handling if inspect fails to generate a function prototype and py2 signature support --- python/bncompleter.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'python') diff --git a/python/bncompleter.py b/python/bncompleter.py index e1dfab1a..d5adfbc1 100644 --- a/python/bncompleter.py +++ b/python/bncompleter.py @@ -41,9 +41,27 @@ Notes: import atexit import __main__ import inspect +import sys __all__ = ["Completer"] +def fnsignature(obj): + if sys.version_info[0:2] >= (3, 5): + try: + sig = str(inspect.signature(obj)) + except: + sig = "()" + return sig + else: + try: + args = inspect.getargspec(obj).args + args.remove('self') + sig = "(" + ','.join(args) + ")" + except: + sig = "()" + return sig + + class Completer: def __init__(self, namespace = None): """Create a new completer for the command line. @@ -100,7 +118,7 @@ class Completer: def _callable_postfix(self, val, word): if callable(val) and not inspect.isclass(val): - word = word + str(inspect.signature(val)) + word = word + fnsignature(val) return word def global_matches(self, text): -- cgit v1.3.1