mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
bpo-40257: Revert changes to inspect.getdoc() (GH-20073)
This commit is contained in:
parent
98e42d1f88
commit
08b47c367a
6 changed files with 115 additions and 40 deletions
|
@ -543,6 +543,17 @@ def _findclass(func):
|
|||
return cls
|
||||
|
||||
def _finddoc(obj):
|
||||
if isclass(obj):
|
||||
for base in obj.__mro__:
|
||||
if base is not object:
|
||||
try:
|
||||
doc = base.__doc__
|
||||
except AttributeError:
|
||||
continue
|
||||
if doc is not None:
|
||||
return doc
|
||||
return None
|
||||
|
||||
if ismethod(obj):
|
||||
name = obj.__func__.__name__
|
||||
self = obj.__self__
|
||||
|
@ -586,35 +597,23 @@ def _finddoc(obj):
|
|||
return None
|
||||
for base in cls.__mro__:
|
||||
try:
|
||||
doc = _getowndoc(getattr(base, name))
|
||||
doc = getattr(base, name).__doc__
|
||||
except AttributeError:
|
||||
continue
|
||||
if doc is not None:
|
||||
return doc
|
||||
return None
|
||||
|
||||
def _getowndoc(obj):
|
||||
"""Get the documentation string for an object if it is not
|
||||
inherited from its class."""
|
||||
try:
|
||||
doc = object.__getattribute__(obj, '__doc__')
|
||||
if doc is None:
|
||||
return None
|
||||
if obj is not type:
|
||||
typedoc = type(obj).__doc__
|
||||
if isinstance(typedoc, str) and typedoc == doc:
|
||||
return None
|
||||
return doc
|
||||
except AttributeError:
|
||||
return None
|
||||
|
||||
def getdoc(object):
|
||||
"""Get the documentation string for an object.
|
||||
|
||||
All tabs are expanded to spaces. To clean up docstrings that are
|
||||
indented to line up with blocks of code, any whitespace than can be
|
||||
uniformly removed from the second line onwards is removed."""
|
||||
doc = _getowndoc(object)
|
||||
try:
|
||||
doc = object.__doc__
|
||||
except AttributeError:
|
||||
return None
|
||||
if doc is None:
|
||||
try:
|
||||
doc = _finddoc(object)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue