Skip to content

bpo-45356: fix incorrect access of class property in pydoc and inspect #29239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,14 @@ def getmembers(object, predicate=None):

Attribute = namedtuple('Attribute', 'name kind defining_class object')

def _is_class_property(cls, name):
for base in cls.__mro__:
o = base.__dict__.get(name)
if isinstance(o, classmethod) and \
isinstance(o.__func__, property):
return True
return False

def classify_class_attrs(cls):
"""Return list of attribute-descriptor tuples.

Expand Down Expand Up @@ -546,6 +554,8 @@ def classify_class_attrs(cls):
try:
if name == '__dict__':
raise Exception("__dict__ is special, don't want the proxy")
if _is_class_property(cls, name):
raise Exception("class property")
get_obj = getattr(cls, name)
except Exception as exc:
pass
Expand Down
4 changes: 4 additions & 0 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,8 @@ def spill(msg, attrs, predicate):
push(msg)
for name, kind, homecls, value in ok:
try:
if inspect._is_class_property(object, name):
raise Exception("class property")
value = getattr(object, name)
except Exception:
# Some descriptors may meet a failure in their __get__.
Expand Down Expand Up @@ -1368,6 +1370,8 @@ def spill(msg, attrs, predicate):
push(msg)
for name, kind, homecls, value in ok:
try:
if inspect._is_class_property(object, name):
raise Exception("class property")
value = getattr(object, name)
except Exception:
# Some descriptors may meet a failure in their __get__.
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,19 @@ class object
" | ... and \\d+ other subclasses")
self.assertRegex(text, snip)

def test_classmethod_property(self):
# Issue 45356
class A:
@classmethod
@property
def a(self):
# BaseException because accessor of attributes in pydoc and in
# inspect catches Exception
raise BaseException
class B(A): pass
pydoc.render_doc(A)
pydoc.render_doc(B)

def test_builtin_with_child(self):
"""Tests help on builtin object which have only child classes.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:mod:`pydoc` ``help`` command, :meth:`~pydoc.HTMLDoc.docclass`,
:meth:`~pydoc.TextDoc.docclass`, and :func:`~inspect.classify_class_attrs`
will no longer execute the logic of a class property when examining a class
object that contains such property.