Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,20 +564,9 @@ def fail(self, object, name=None, *args):
def getdocloc(self, object, basedir=sysconfig.get_path('stdlib')):
"""Return the location of module docs or None"""

try:
file = inspect.getabsfile(object)
except TypeError:
file = '(built-in)'

docloc = os.environ.get("PYTHONDOCS", self.PYTHONDOCS)

basedir = os.path.normcase(basedir)
if (isinstance(object, type(os)) and
(object.__name__ in ('errno', 'exceptions', 'gc',
'marshal', 'posix', 'signal', 'sys',
'_thread', 'zipimport') or
(file.startswith(basedir) and
not file.startswith(os.path.join(basedir, 'site-packages')))) and
if (self._is_stdlib_module(object, basedir) and
object.__name__ not in ('xml.etree', 'test.test_pydoc.pydoc_mod')):
if docloc.startswith(("http://", "https://")):
docloc = "{}/{}.html".format(docloc.rstrip("/"), object.__name__.lower())
Expand All @@ -587,6 +576,34 @@ def getdocloc(self, object, basedir=sysconfig.get_path('stdlib')):
docloc = None
return docloc

def get_version(self, object):
if self._is_stdlib_module(object):
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
if hasattr(object, '__version__'):
return str(object.__version__)
else:
return None
else:
if hasattr(object, '__version__'):
return str(object.__version__)
else:
return None

def _is_stdlib_module(self, object, basedir=sysconfig.get_path('stdlib')):
try:
file = inspect.getabsfile(object)
except TypeError:
file = '(built-in)'

basedir = os.path.normcase(basedir)
return (isinstance(object, type(os)) and
(object.__name__ in ('errno', 'exceptions', 'gc',
'marshal', 'posix', 'signal', 'sys',
'_thread', 'zipimport')
or (file.startswith(basedir) and
not file.startswith(os.path.join(basedir, 'site-packages')))))

# -------------------------------------------- HTML documentation generator

class HTMLRepr(Repr):
Expand Down Expand Up @@ -846,8 +863,8 @@ def docmodule(self, object, name=None, mod=None, *ignored):
except TypeError:
filelink = '(built-in)'
info = []
if hasattr(object, '__version__'):
version = str(object.__version__)

if (version := self.get_version(object)) is not None:
if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
version = version[11:-1].strip()
info.append('version %s' % self.escape(version))
Expand Down Expand Up @@ -1382,8 +1399,7 @@ def docmodule(self, object, name=None, mod=None, *ignored):
contents.append(self.docother(value, key, name, maxlen=70))
result = result + self.section('DATA', '\n'.join(contents))

if hasattr(object, '__version__'):
version = str(object.__version__)
if (version := self.get_version(object)) is not None:
if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
version = version[11:-1].strip()
result = result + self.section('VERSION', version)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`pydoc`: Fix :exc:`DeprecationWarning` being raised when generating doc for
:term:`stdlib` modules.
Loading