*** pydoc.py.old Fri Oct 1 19:14:54 2004 --- pydoc.py Fri Oct 1 21:29:42 2004 *************** *** 72,78 **** return dirs def getdoc(object): """Get the doc string or comments for an object.""" ! result = inspect.getdoc(object) or inspect.getcomments(object) return result and re.sub('^ *\n', '', rstrip(result)) or '' --- 72,103 ---- return dirs + _method_types = (staticmethod, classmethod, types.MethodType, + types.BuiltinMethodType, types.FunctionType) + def _getdoc_helper(thing): + """Like `inspect.getdoc` but for methods with no documentation it + retrieves the documentation from a baseclasses corresponding method, if + possible.""" + if inspect.getdoc(thing): + return inspect.getdoc(thing) + else: + def find_method_doc(c, name): + v = c.__dict__.get(name) + return isinstance(v, _method_types) and inspect.getdoc(v) + if isinstance(thing, _method_types): + try: + the_class = thing.im_class + func_name = thing.im_func.func_name + for c in inspect.getmro(the_class)[1:]: + if find_method_doc(c, func_name): + return find_method_doc(c, func_name) + return None + except AttributeError, msg: + print >> sys.stderr,\ + "Couldn't get doc from baseclass for %s: %s" % (thing, msg) + return None + def getdoc(object): """Get the doc string or comments for an object.""" ! result = _getdoc_helper(object) or inspect.getcomments(object) return result and re.sub('^ *\n', '', rstrip(result)) or ''