Skip to content

Commit a47bbf5

Browse files
committed
Issue #10446: Several changes to module documentation generated by pydoc:
1. Online reference manual link is now version-specific and the 'MODULE DOCS' section renamed to 'MODULE REFERENCE'. 2. 'FILE' section is moved to the end of the file. 3. Special names processed by pydoc such as __version__ or __credits__ are now excluded from the DATA section. 4. Defined __all__ to prevent pydoc from exposing undocumented details about itself. 5. Removed Python 2.3 compatibility code.
1 parent f609654 commit a47bbf5

File tree

4 files changed

+39
-42
lines changed

4 files changed

+39
-42
lines changed

Doc/library/pydoc.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ documents precisely the version of the module you would get if you started the
6363
Python interpreter and typed ``import spam``.
6464

6565
Module docs for core modules are assumed to reside in
66-
http://docs.python.org/library/. This can be overridden by setting the
67-
:envvar:`PYTHONDOCS` environment variable to a different URL or to a local
68-
directory containing the Library Reference Manual pages.
66+
``http://docs.python.org/X.Y/library/`` where ``X`` and ``Y`` are the
67+
major and minor version numbers of the Python interpreter. This can
68+
be overridden by setting the :envvar:`PYTHONDOCS` environment variable
69+
to a different URL or to a local directory containing the Library
70+
Reference Manual pages.
6971

Lib/pydoc.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ class or function within a module or module in a package. If the
2626
2727
Module docs for core modules are assumed to be in
2828
29-
http://docs.python.org/library/
29+
http://docs.python.org/X.Y/library/
3030
3131
This can be overridden by setting the PYTHONDOCS environment variable
3232
to a different URL or to a local directory containing the Library
3333
Reference Manual pages.
3434
"""
35-
35+
__all__ = ['help']
3636
__author__ = "Ka-Ping Yee <ping@lfw.org>"
3737
__date__ = "26 February 2001"
3838

@@ -54,14 +54,7 @@ class or function within a module or module in a package. If the
5454
import sys, imp, os, re, inspect, builtins, pkgutil
5555
from reprlib import Repr
5656
from traceback import extract_tb as _extract_tb
57-
try:
58-
from collections import deque
59-
except ImportError:
60-
# Python 2.3 compatibility
61-
class deque(list):
62-
def popleft(self):
63-
return self.pop(0)
64-
57+
from collections import deque
6558
# --------------------------------------------------------- common routines
6659

6760
def pathdirs():
@@ -159,7 +152,8 @@ def visiblename(name, all=None):
159152
# Certain special names are redundant.
160153
_hidden_names = ('__builtins__', '__doc__', '__file__', '__path__',
161154
'__module__', '__name__', '__slots__', '__package__',
162-
'__cached__')
155+
'__cached__', '__author__', '__credits__', '__date__',
156+
'__version__')
163157
if name in _hidden_names: return 0
164158
# Private names are hidden, but special names are displayed.
165159
if name.startswith('__') and name.endswith('__'): return 1
@@ -306,6 +300,11 @@ def safeimport(path, forceload=0, cache={}):
306300
# ---------------------------------------------------- formatter base class
307301

308302
class Doc:
303+
304+
PYTHONDOCS = os.environ.get("PYTHONDOCS",
305+
"http://docs.python.org/%d.%d/library"
306+
% sys.version_info[:2])
307+
309308
def document(self, object, name=None, *args):
310309
"""Generate documentation for an object."""
311310
args = (object, name) + args
@@ -340,10 +339,10 @@ def getdocloc(self, object):
340339
except TypeError:
341340
file = '(built-in)'
342341

343-
docloc = os.environ.get("PYTHONDOCS",
344-
"http://docs.python.org/library")
342+
docloc = os.environ.get("PYTHONDOCS", self.PYTHONDOCS)
343+
345344
basedir = os.path.join(sys.exec_prefix, "lib",
346-
"python"+sys.version[0:3])
345+
"python%d.%d" % sys.version_info[:2])
347346
if (isinstance(object, type(os)) and
348347
(object.__name__ in ('errno', 'exceptions', 'gc', 'imp',
349348
'marshal', 'posix', 'signal', 'sys',
@@ -607,7 +606,7 @@ def docmodule(self, object, name=None, mod=None, *ignored):
607606
head = head + ' (%s)' % ', '.join(info)
608607
docloc = self.getdocloc(object)
609608
if docloc is not None:
610-
docloc = '<br><a href="%(docloc)s">Module Docs</a>' % locals()
609+
docloc = '<br><a href="%(docloc)s">Module Reference</a>' % locals()
611610
else:
612611
docloc = ''
613612
result = self.heading(
@@ -1016,21 +1015,16 @@ def docmodule(self, object, name=None, mod=None):
10161015
name = object.__name__ # ignore the passed-in name
10171016
synop, desc = splitdoc(getdoc(object))
10181017
result = self.section('NAME', name + (synop and ' - ' + synop))
1019-
1020-
try:
1021-
all = object.__all__
1022-
except AttributeError:
1023-
all = None
1024-
1025-
try:
1026-
file = inspect.getabsfile(object)
1027-
except TypeError:
1028-
file = '(built-in)'
1029-
result = result + self.section('FILE', file)
1030-
1018+
all = getattr(object, '__all__', None)
10311019
docloc = self.getdocloc(object)
10321020
if docloc is not None:
1033-
result = result + self.section('MODULE DOCS', docloc)
1021+
result = result + self.section('MODULE REFERENCE', docloc + """
1022+
1023+
The following documentation is automatically generated from the Python source
1024+
files. It may be incomplete, incorrect or include features that are considered
1025+
implementation detail and may vary between Python implementations. When in
1026+
doubt, consult the module reference at the location listed above.
1027+
""")
10341028

10351029
if desc:
10361030
result = result + self.section('DESCRIPTION', desc)
@@ -1109,6 +1103,11 @@ def docmodule(self, object, name=None, mod=None):
11091103
result = result + self.section('AUTHOR', str(object.__author__))
11101104
if hasattr(object, '__credits__'):
11111105
result = result + self.section('CREDITS', str(object.__credits__))
1106+
try:
1107+
file = inspect.getabsfile(object)
1108+
except TypeError:
1109+
file = '(built-in)'
1110+
result = result + self.section('FILE', file)
11121111
return result
11131112

11141113
def docclass(self, object, name=None, mod=None):

Lib/test/pydoc_mod.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
__author__ = "Benjamin Peterson"
44
__credits__ = "Nobody"
55
__version__ = "1.2.3.4"
6-
6+
__xyz__ = "X, Y and Z"
77

88
class A:
99
"""Hello and goodbye"""

Lib/test/test_pydoc.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
expected_text_pattern = """
2323
NAME
2424
test.pydoc_mod - This is a test module for test_pydoc
25-
26-
FILE
27-
%s
2825
%s
2926
CLASSES
3027
builtins.object
@@ -72,9 +69,7 @@ class B(builtins.object)
7269
nodoc_func()
7370
7471
DATA
75-
__author__ = 'Benjamin Peterson'
76-
__credits__ = 'Nobody'
77-
__version__ = '1.2.3.4'
72+
__xyz__ = 'X, Y and Z'
7873
7974
VERSION
8075
1.2.3.4
@@ -84,6 +79,9 @@ class B(builtins.object)
8479
8580
CREDITS
8681
Nobody
82+
83+
FILE
84+
%s
8785
""".strip()
8886

8987
expected_html_pattern = """
@@ -167,9 +165,7 @@ class B(builtins.object)
167165
<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
168166
\x20\x20\x20\x20
169167
<tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
170-
<td width="100%%"><strong>__author__</strong> = 'Benjamin Peterson'<br>
171-
<strong>__credits__</strong> = 'Nobody'<br>
172-
<strong>__version__</strong> = '1.2.3.4'</td></tr></table><p>
168+
<td width="100%%"><strong>__xyz__</strong> = 'X, Y and Z'</td></tr></table><p>
173169
<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
174170
<tr bgcolor="#7799ee">
175171
<td colspan=3 valign=bottom>&nbsp;<br>
@@ -259,7 +255,7 @@ def test_html_doc(self):
259255
def test_text_doc(self):
260256
result, doc_loc = get_pydoc_text(pydoc_mod)
261257
expected_text = expected_text_pattern % \
262-
(inspect.getabsfile(pydoc_mod), doc_loc)
258+
(doc_loc, inspect.getabsfile(pydoc_mod))
263259
if result != expected_text:
264260
print_diffs(expected_text, result)
265261
self.fail("outputs are not equal, see diff above")

0 commit comments

Comments
 (0)