Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
gh-87729: add LOAD_SUPER_ATTR instruction for faster super()#103497
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
0a0ebe2f14a3bf5625e730775efef229b5b626999d938410692c943b775ed0f2077f1a64da49f4759ad994399c2513645982945b23a3cb74e4466a75c0a21cf161268df442c019b80250de5bc6dbe1665File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class super: | ||
| msg = "truly super" | ||
| class C: | ||
| def method(self): | ||
| return super().msg |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| """Unit tests for zero-argument super() & related machinery.""" | ||
| import unittest | ||
| from unittest.mock import patch | ||
| from test import shadowed_super | ||
| class A: | ||
| @@ -283,17 +285,28 @@ def f(self): | ||
| def test_obscure_super_errors(self): | ||
| def f(): | ||
| super() | ||
| with self.assertRaisesRegex(RuntimeError, r"no arguments"): | ||
| f() | ||
| class C: | ||
| def f(): | ||
| super() | ||
| with self.assertRaisesRegex(RuntimeError, r"no arguments"): | ||
| C.f() | ||
| def f(x): | ||
| del x | ||
| super() | ||
| with self.assertRaisesRegex(RuntimeError, r"arg\[0\] deleted"): | ||
| f(None) | ||
| class X: | ||
| def f(x): | ||
| nonlocal __class__ | ||
| del __class__ | ||
| super() | ||
| with self.assertRaisesRegex(RuntimeError, r"empty __class__ cell"): | ||
| X().f() | ||
markshannon marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| def test_cell_as_self(self): | ||
| class X: | ||
| @@ -325,6 +338,78 @@ def test_super_argtype(self): | ||
| with self.assertRaisesRegex(TypeError, "argument 1 must be a type"): | ||
| super(1, int) | ||
| def test_shadowed_global(self): | ||
| self.assertEqual(shadowed_super.C().method(), "truly super") | ||
| def test_shadowed_local(self): | ||
| class super: | ||
| msg = "quite super" | ||
| class C: | ||
| def method(self): | ||
| return super().msg | ||
| self.assertEqual(C().method(), "quite super") | ||
| def test_shadowed_dynamic(self): | ||
| class MySuper: | ||
| msg = "super super" | ||
| class C: | ||
| def method(self): | ||
| return super().msg | ||
| with patch("test.test_super.super", MySuper) as m: | ||
| self.assertEqual(C().method(), "super super") | ||
| def test_shadowed_dynamic_two_arg(self): | ||
| call_args = [] | ||
| class MySuper: | ||
| def __init__(self, *args): | ||
| call_args.append(args) | ||
| msg = "super super" | ||
| class C: | ||
| def method(self): | ||
| return super(1, 2).msg | ||
| with patch("test.test_super.super", MySuper) as m: | ||
| self.assertEqual(C().method(), "super super") | ||
| self.assertEqual(call_args, [(1, 2)]) | ||
| def test_attribute_error(self): | ||
| class C: | ||
| def method(self): | ||
| return super().msg | ||
| with self.assertRaisesRegex(AttributeError, "'super' object has no attribute 'msg'"): | ||
| C().method() | ||
| def test_bad_first_arg(self): | ||
| class C: | ||
| def method(self): | ||
| return super(1, self).method() | ||
| with self.assertRaisesRegex(TypeError, "argument 1 must be a type"): | ||
carljm marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| C().method() | ||
| def test_super___class__(self): | ||
| class C: | ||
| def method(self): | ||
| return super().__class__ | ||
| self.assertEqual(C().method(), super) | ||
| def test_super_subclass___class__(self): | ||
| class mysuper(super): | ||
| pass | ||
| class C: | ||
| def method(self): | ||
| return mysuper(C, self).__class__ | ||
| self.assertEqual(C().method(), mysuper) | ||
| if __name__ == "__main__": | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add :opcode:`LOAD_SUPER_ATTR` to speed up ``super().meth()`` and ``super().attr`` calls. |
Uh oh!
There was an error while loading.Please reload this page.