Skip to content

bpo-38337: Change getattr to inspect.getattr_static #16521

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

Closed
wants to merge 8 commits into from
Closed
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
5 changes: 4 additions & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,10 @@ def getmembers(object, predicate=None):
# like calling their __get__ (see bug #1785), so fall back to
# looking in the __dict__.
try:
value = getattr(object, key)
if isinstance(getattr_static(object, key), property):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we always use getattr_static? If there's a reason for not using it always, please describe that in a comment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is related to my latest comment. Sorry, I didn't make this clear:

getattr_static(...,'__class__') returns <attribute '__class__' of 'object' objects> whereas getattr would return <class '__main__.Foo'>. What should getmembers return? Using getattr_static for every attribute doesn't seem to be the solution. It works fine with methods, functions and properties, but some edge cases seem to require getattr (such as __class__). It is not clear what the expected behaviour is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, well, describe this in a comment :)

value = getattr_static(object, key)
else:
value = getattr(object, key)
# handle the duplicate key
if key in processed:
raise AttributeError
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,27 @@ def f(self):
self.assertIn(('f', b.f), inspect.getmembers(b))
self.assertIn(('f', b.f), inspect.getmembers(b, inspect.ismethod))

def test_getmembers_static(self):
class Foo:
def __init__(self, bar):
self._bar = bar

@property
def bar(self):
# This property should not be called by getmembers
raise NotImplementedError

foobar = Foo(42)
try:
members = inspect.getmembers(foobar)
property_member = ('bar', inspect.getattr_static(foobar, 'bar'))
self.assertIn(property_member, members)
class_members = inspect.getmembers(Foo)
property_class_member = ('bar', inspect.getattr_static(Foo, 'bar'))
self.assertIn(property_member, class_members)
except NotImplementedError:
self.fail('getmembers() called property!')

def test_getmembers_VirtualAttribute(self):
class M(type):
def __getattr__(cls, name):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prevent :func:`inspect.getmembers` from calling descriptors.