diff --git a/Lib/pyclbr.py b/Lib/pyclbr.py index 2c798df..d53988b 100644 --- a/Lib/pyclbr.py +++ b/Lib/pyclbr.py @@ -65,8 +65,9 @@ class _Object: class Function(_Object): "Information about a Python function, including methods." - def __init__(self, module, name, file, lineno, parent=None): + def __init__(self, module, name, args, file, lineno, parent=None): _Object.__init__(self, module, name, file, lineno, parent) + self.args = [] if args is None else args class Class(_Object): @@ -80,9 +81,9 @@ class Class(_Object): self.methods[name] = lineno -def _nest_function(ob, func_name, lineno): +def _nest_function(ob, func_name, lineno, args): "Return a Function after nesting within ob." - newfunc = Function(ob.module, func_name, ob.file, lineno, ob) + newfunc = Function(ob.module, func_name, args, ob.file, lineno, ob) ob._addchild(func_name, newfunc) if isinstance(ob, Class): ob._addmethod(func_name, lineno) @@ -208,13 +209,15 @@ def _create_tree(fullmodule, path, fname, source, tree, inpackage): tokentype, func_name, start = next(g)[0:3] if tokentype != NAME: continue # Skip def with syntax error. + # Parse what follows the class name. + args = _parse_args('def', tree, g, token) cur_func = None if stack: cur_obj = stack[-1][0] - cur_func = _nest_function(cur_obj, func_name, lineno) + cur_func = _nest_function(cur_obj, func_name, lineno, args) else: # It is just a function. - cur_func = Function(fullmodule, func_name, fname, lineno) + cur_func = Function(fullmodule, func_name, args, fname, lineno) tree[func_name] = cur_func stack.append((cur_func, thisindent)) elif token == 'class': @@ -226,45 +229,7 @@ def _create_tree(fullmodule, path, fname, source, tree, inpackage): if tokentype != NAME: continue # Skip class with syntax error. # Parse what follows the class name. - tokentype, token, start = next(g)[0:3] - inherit = None - if token == '(': - names = [] # Initialize list of superclasses. - level = 1 - super = [] # Tokens making up current superclass. - while True: - tokentype, token, start = next(g)[0:3] - if token in (')', ',') and level == 1: - n = "".join(super) - if n in tree: - # We know this super class. - n = tree[n] - else: - c = n.split('.') - if len(c) > 1: - # Super class form is module.class: - # look in module for class. - m = c[-2] - c = c[-1] - if m in _modules: - d = _modules[m] - if c in d: - n = d[c] - names.append(n) - super = [] - if token == '(': - level += 1 - elif token == ')': - level -= 1 - if level == 0: - break - elif token == ',' and level == 1: - pass - # Only use NAME and OP (== dot) tokens for type name. - elif tokentype in (NAME, OP) and level == 1: - super.append(token) - # Expressions in the base list are not supported. - inherit = names + inherit = _parse_args('class', tree, g, token) if stack: cur_obj = stack[-1][0] cur_class = _nest_class( @@ -319,6 +284,50 @@ def _create_tree(fullmodule, path, fname, source, tree, inpackage): return tree +def _parse_args(object_type, tree, g, token): + tokentype, token, start = next(g)[0:3] + arg_list = None + if token == '(': + names = [] # Initialize list of arguments. + level = 1 + args = [] # Tokens making up current superclass/parm list. + while True: + tokentype, token, start = next(g)[0:3] + if token in (')', ',') and level == 1: + n = "".join(args) + if object_type == 'class': + if n in tree: + # We know this super class. + n = tree[n] + else: + c = n.split('.') + if len(c) > 1: + # Super class form is module.class: + # look in module for class. + m = c[-2] + c = c[-1] + if m in _modules: + d = _modules[m] + if c in d: + n = d[c] + names.append(n) + args = [] + if token == '(': + level += 1 + elif token == ')': + level -= 1 + if level == 0: + break + elif token == ',' and level == 1: + pass + # Only use NAME and OP (== dot) tokens for type name. + elif tokentype in (NAME, OP) and level == 1: + args.append(token) + # Expressions in the base list are not supported. + arg_list = names + return arg_list + + def _getnamelist(g): """Return list of (dotted-name, as-name or None) tuples for token source g. @@ -395,7 +404,8 @@ def _main(): print("{}class {} {} {}" .format(' ' * obj.indent, obj.name, obj.super, obj.lineno)) elif isinstance(obj, Function): - print("{}def {} {}".format(' ' * obj.indent, obj.name, obj.lineno)) + print("{}def {} {} {}" + .format(' ' * obj.indent, obj.name, obj.args, obj.lineno)) if __name__ == "__main__": _main()