Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Benedikt's review
  • Loading branch information
StanFromIreland committed Oct 12, 2025
commit d17c6ca2aacc06f09a2063b55438470871f9ab52
16 changes: 9 additions & 7 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,26 +576,28 @@ def getdocloc(self, object, basedir=sysconfig.get_path('stdlib')):
docloc = None
return docloc

def get_version(self, object):
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
return None

def _is_stdlib_module(self, object, basedir=sysconfig.get_path('stdlib')):
Comment thread
picnixz marked this conversation as resolved.
Outdated
try:
file = inspect.getabsfile(object)
except TypeError:
file = '(built-in)'

if sysconfig.is_python_build():
srcdir = sysconfig.get_config_var('srcdir')
if srcdir:
basedir = os.path.join(srcdir, 'Lib')

basedir = os.path.normcase(basedir)
return (isinstance(object, type(os)) and
(object.__name__ in ('errno', 'exceptions', 'gc',
Expand Down Expand Up @@ -864,7 +866,7 @@ def docmodule(self, object, name=None, mod=None, *ignored):
filelink = '(built-in)'
info = []

if (version := self.get_version(object)) is not None:
if version := self._get_version(object):
if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
version = version[11:-1].strip()
info.append('version %s' % self.escape(version))
Expand Down Expand Up @@ -1399,7 +1401,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 (version := self.get_version(object)) is not None:
if version := self._get_version(object):
if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
version = version[11:-1].strip()
result = result + self.section('VERSION', version)
Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_pydoc/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2305,6 +2305,33 @@ def test_sys_path_adjustment_when_curdir_already_included(self):
trailing_argv0dir = trailing_curdir + [self.argv0dir]
self.assertIsNone(self._get_revised_path(trailing_argv0dir))

def test__get_version(self):
import warnings
import json
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated

class Module:
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated
__name__ = 'fauxmod'

def __getattr__(self, name):
if name == "__version__":
from warnings import _deprecated
_deprecated("__version__", remove=(3, 20))
return "1"
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated

module = Module()
doc = pydoc.Doc()
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
version = doc._get_version(json)
self.assertEqual(version, "2.0.9")
Comment thread
StanFromIreland marked this conversation as resolved.
self.assertEqual(len(w), 0)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
version = doc._get_version(module)
self.assertEqual(version, "1")
self.assertEqual(len(w), 2)
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated


def setUpModule():
thread_info = threading_helper.threading_setup()
Expand Down
Loading