Skip to content

Commit 38f056a

Browse files
Commit
1 parent a18843d commit 38f056a

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

Lib/pydoc.py

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -564,20 +564,9 @@ def fail(self, object, name=None, *args):
564564
def getdocloc(self, object, basedir=sysconfig.get_path('stdlib')):
565565
"""Return the location of module docs or None"""
566566

567-
try:
568-
file = inspect.getabsfile(object)
569-
except TypeError:
570-
file = '(built-in)'
571-
572567
docloc = os.environ.get("PYTHONDOCS", self.PYTHONDOCS)
573568

574-
basedir = os.path.normcase(basedir)
575-
if (isinstance(object, type(os)) and
576-
(object.__name__ in ('errno', 'exceptions', 'gc',
577-
'marshal', 'posix', 'signal', 'sys',
578-
'_thread', 'zipimport') or
579-
(file.startswith(basedir) and
580-
not file.startswith(os.path.join(basedir, 'site-packages')))) and
569+
if (self._is_stdlib_module(object, basedir) and
581570
object.__name__ not in ('xml.etree', 'test.test_pydoc.pydoc_mod')):
582571
if docloc.startswith(("http://", "https://")):
583572
docloc = "{}/{}.html".format(docloc.rstrip("/"), object.__name__.lower())
@@ -587,6 +576,20 @@ def getdocloc(self, object, basedir=sysconfig.get_path('stdlib')):
587576
docloc = None
588577
return docloc
589578

579+
def _is_stdlib_module(self, object, basedir=sysconfig.get_path('stdlib')):
580+
try:
581+
file = inspect.getabsfile(object)
582+
except TypeError:
583+
file = '(built-in)'
584+
585+
basedir = os.path.normcase(basedir)
586+
return (isinstance(object, type(os)) and
587+
(object.__name__ in ('errno', 'exceptions', 'gc',
588+
'marshal', 'posix', 'signal', 'sys',
589+
'_thread', 'zipimport')
590+
or (file.startswith(basedir) and
591+
not file.startswith(os.path.join(basedir, 'site-packages')))))
592+
590593
# -------------------------------------------- HTML documentation generator
591594

592595
class HTMLRepr(Repr):
@@ -846,8 +849,17 @@ def docmodule(self, object, name=None, mod=None, *ignored):
846849
except TypeError:
847850
filelink = '(built-in)'
848851
info = []
849-
if hasattr(object, '__version__'):
850-
version = str(object.__version__)
852+
853+
if self._is_stdlib_module(object):
854+
with warnings.catch_warnings():
855+
warnings.simplefilter("ignore", DeprecationWarning)
856+
if has_version := hasattr(object, '__version__'):
857+
version = str(object.__version__)
858+
else:
859+
if has_version := hasattr(object, '__version__'):
860+
version = str(object.__version__)
861+
862+
if has_version:
851863
if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
852864
version = version[11:-1].strip()
853865
info.append('version %s' % self.escape(version))
@@ -1382,8 +1394,16 @@ def docmodule(self, object, name=None, mod=None, *ignored):
13821394
contents.append(self.docother(value, key, name, maxlen=70))
13831395
result = result + self.section('DATA', '\n'.join(contents))
13841396

1385-
if hasattr(object, '__version__'):
1386-
version = str(object.__version__)
1397+
if self._is_stdlib_module(object):
1398+
with warnings.catch_warnings():
1399+
warnings.simplefilter("ignore", DeprecationWarning)
1400+
if has_version := hasattr(object, '__version__'):
1401+
version = str(object.__version__)
1402+
else:
1403+
if has_version := hasattr(object, '__version__'):
1404+
version = str(object.__version__)
1405+
1406+
if has_version:
13871407
if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
13881408
version = version[11:-1].strip()
13891409
result = result + self.section('VERSION', version)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`pydoc`: Fix :exc:`DeprecationWarnings` when generating doc for
2+
:term:`stdlib` modules.

0 commit comments

Comments
 (0)