diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 65a90193b66..0e6999c5d4d 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -44,7 +44,7 @@ Mynd you, m # the current directory is changed with os.chdir(), an incorrect # path will be displayed. -import sys, imp, os, re, types, inspect +import sys, imp, os, re, types, inspect, __builtin__ from repr import Repr from string import expandtabs, find, join, lower, split, strip, rfind, rstrip @@ -142,6 +142,15 @@ def _split_list(s, predicate): no.append(x) return yes, no +def visiblename(name): + """Decide whether to show documentation on a variable.""" + # Certain special names are redundant. + if name in ['__builtins__', '__doc__', '__file__', '__path__', + '__module__', '__name__']: return 0 + # Private names are hidden, but special names are displayed. + if name.startswith('__') and name.endswith('__'): return 1 + return not name.startswith('_') + # ----------------------------------------------------- module manipulation def ispackage(path): @@ -337,9 +346,7 @@ class HTMLDoc(Doc): return '''
@@ -529,8 +536,9 @@ TT { font-family: lucidatypewriter, lucida console, courier } classes, cdict = [], {} for key, value in inspect.getmembers(object, inspect.isclass): if (inspect.getmodule(value) or object) is object: - classes.append((key, value)) - cdict[key] = cdict[value] = '#' + key + if visiblename(key): + classes.append((key, value)) + cdict[key] = cdict[value] = '#' + key for key, value in classes: for base in value.__bases__: key, modname = base.__name__, base.__module__ @@ -542,12 +550,13 @@ TT { font-family: lucidatypewriter, lucida console, courier } funcs, fdict = [], {} for key, value in inspect.getmembers(object, inspect.isroutine): if inspect.isbuiltin(value) or inspect.getmodule(value) is object: - funcs.append((key, value)) - fdict[key] = '#-' + key - if inspect.isfunction(value): fdict[value] = fdict[key] + if visiblename(key): + funcs.append((key, value)) + fdict[key] = '#-' + key + if inspect.isfunction(value): fdict[value] = fdict[key] data = [] for key, value in inspect.getmembers(object, isdata): - if key not in ['__builtins__', '__doc__']: + if visiblename(key): data.append((key, value)) doc = self.markup(getdoc(object), self.preformat, fdict, cdict) @@ -560,11 +569,12 @@ TT { font-family: lucidatypewriter, lucida console, courier } for file in os.listdir(object.__path__[0]): path = os.path.join(object.__path__[0], file) modname = inspect.getmodulename(file) - if modname and modname not in modnames: - modpkgs.append((modname, name, 0, 0)) - modnames.append(modname) - elif ispackage(path): - modpkgs.append((file, name, 1, 0)) + if modname != '__init__': + if modname and modname not in modnames: + modpkgs.append((modname, name, 0, 0)) + modnames.append(modname) + elif ispackage(path): + modpkgs.append((file, name, 1, 0)) modpkgs.sort() contents = self.multicolumn(modpkgs, self.modpkglink) result = result + self.bigsection( @@ -658,12 +668,12 @@ TT { font-family: lucidatypewriter, lucida console, courier } doc = self.markup(value.__doc__, self.preformat, funcs, classes, mdict) push(' \n' # Sort attrs by name. attrs.sort(lambda t1, t2: cmp(t1[0], t2[0])) # Pump out the attrs, segregated by kind. - attrs = spill("Methods %s" % tag, attrs, + attrs = spill('Methods %s' % tag, attrs, lambda t: t[1] == 'method') - attrs = spill("Class methods %s" % tag, attrs, + attrs = spill('Class methods %s' % tag, attrs, lambda t: t[1] == 'class method') - attrs = spill("Static methods %s" % tag, attrs, + attrs = spill('Static methods %s' % tag, attrs, lambda t: t[1] == 'static method') - attrs = spillproperties("Properties %s" % tag, attrs, + attrs = spillproperties('Properties %s' % tag, attrs, lambda t: t[1] == 'property') - attrs = spilldata("Data and non-method functions %s" % tag, attrs, + attrs = spilldata('Data and other attributes %s' % tag, attrs, lambda t: t[1] == 'data') assert attrs == [] attrs = inherited @@ -747,9 +761,9 @@ TT { font-family: lucidatypewriter, lucida console, courier } parents.append(self.classlink(base, object.__module__)) title = title + '(%s)' % join(parents, ', ') doc = self.markup(getdoc(object), self.preformat, funcs, classes, mdict) - doc = doc and '%s ' % doc or ' ' + doc = doc and '%s ' % doc - return self.section(title, '#000000', '#ffc8d8', contents, 5, doc) + return self.section(title, '#000000', '#ffc8d8', contents, 3, doc) def formatvalue(self, object): """Format an argument default value as text.""" @@ -935,14 +949,16 @@ class TextDoc(Doc): classes = [] for key, value in inspect.getmembers(object, inspect.isclass): if (inspect.getmodule(value) or object) is object: - classes.append((key, value)) + if visiblename(key): + classes.append((key, value)) funcs = [] for key, value in inspect.getmembers(object, inspect.isroutine): if inspect.isbuiltin(value) or inspect.getmodule(value) is object: - funcs.append((key, value)) + if visiblename(key): + funcs.append((key, value)) data = [] for key, value in inspect.getmembers(object, isdata): - if key not in ['__builtins__', '__doc__']: + if visiblename(key): data.append((key, value)) if hasattr(object, '__path__'): @@ -950,10 +966,11 @@ class TextDoc(Doc): for file in os.listdir(object.__path__[0]): path = os.path.join(object.__path__[0], file) modname = inspect.getmodulename(file) - if modname and modname not in modpkgs: - modpkgs.append(modname) - elif ispackage(path): - modpkgs.append(file + ' (package)') + if modname != '__init__': + if modname and modname not in modpkgs: + modpkgs.append(modname) + elif ispackage(path): + modpkgs.append(file + ' (package)') modpkgs.sort() result = result + self.section( 'PACKAGE CONTENTS', join(modpkgs, '\n')) @@ -1052,17 +1069,16 @@ class TextDoc(Doc): if doc: push(self.indent(doc)) need_blank_after_doc = 1 - for attr, tag in [("fget", " getter"), - ("fset", " setter"), - ("fdel", " deleter")]: + for attr, tag in [('fget', ' |