Skip to content

bpo-38026: fix inspect.getattr_static #15676

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

Merged
merged 2 commits into from
Sep 5, 2019
Merged
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
4 changes: 2 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,7 @@ def _shadowed_dict(klass):
except KeyError:
pass
else:
if not (isinstance(class_dict, types.GetSetDescriptorType) and
if not (type(class_dict) is types.GetSetDescriptorType and
class_dict.__name__ == "__dict__" and
class_dict.__objclass__ is entry):
return class_dict
Expand All @@ -1580,7 +1580,7 @@ def getattr_static(obj, attr, default=_sentinel):
klass = type(obj)
dict_attr = _shadowed_dict(klass)
if (dict_attr is _sentinel or
isinstance(dict_attr, types.MemberDescriptorType)):
type(dict_attr) is types.MemberDescriptorType):
Copy link
Contributor

@srinivasreddy srinivasreddy Sep 4, 2019

Choose a reason for hiding this comment

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

I think It helps to comment on why we use type here.

Copy link
Member Author

Choose a reason for hiding this comment

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

If it is required, every line in this function should have same comment. It will be too noisy...

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a link to the issue?

Copy link
Member Author

Choose a reason for hiding this comment

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

Note that I merged because I didn't read the comment / docstring so I didn't notice dynamic lookup should be avoided. It's totally my mistake.

But note that It is not because I didn't notice isinstance cause dynamic lookup.
If we want to avoid this type of regression by adding comment, we need to add
# to avoid dynamic lookup comment on every line. It's nonsense...

instance_result = _check_instance(obj, attr)
else:
klass = obj
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed :func:`inspect.getattr_static` used ``isinstance`` while it should
avoid dynamic lookup.