8000 gh-103193: Improve `getattr_static` test coverage by AlexWaygood · Pull Request #104286 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@AlexWaygood
Copy link
Member
@AlexWaygood AlexWaygood commented May 8, 2023

Currently, no tests would fail if we were to apply this optimisation to getattr_static, which makes things a fair bit faster:

--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -1780,13 +1780,9 @@ def trace(context=1):


 def _check_instance(obj, attr):
-    instance_dict = {}
-    try:
-        instance_dict = object.__getattribute__(obj, "__dict__")
-    except AttributeError:
-        pass
-    return dict.get(instance_dict, attr, _sentinel)
-
+    if hasattr(obj, "__dict__"):
+        return dict.get(obj.__dict__, attr, _sentinel)
+    return _sentinel

However, the optimisation would lead to incorrect behaviour. test_custom___getattribute__, added in this PR, would correctly fail if the incorrect optimisation were applied.

@miss-islington
Copy link
Contributor

Thanks @AlexWaygood for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry @AlexWaygood, I had trouble checking out the 3.11 backport branch.
Please retry by removing and re-adding the "needs backport to 3.11" label.
Alternatively, you can backport using cherry_picker on the command line.
cherry_picker 921185ed050efbca2f0adeab79f676b7f8cc3660 3.11

@AlexWaygood AlexWaygood deleted the attr_static-coverage branch May 8, 2023 14:18
@AlexWaygood AlexWaygood added needs backport to 3.11 only security fixes and removed needs backport to 3.11 only security fixes labels May 8, 2023
@miss-islington
Copy link
Contributor

Thanks @AlexWaygood for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-104290 is a backport of this pull request to the 3.11 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label May 8, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 8, 2023
)

(cherry picked from commit 921185e)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
AlexWaygood added a commit that referenced this pull request May 8, 2023
…104290)

gh-103193: Improve `getattr_static` test coverage (GH-104286)
(cherry picked from commit 921185e)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>