-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
bpo-34441: Fix ABC.__subclasscheck__ crash on a class with invalid __subclasses__ #8835
Conversation
…le __subclasses__ The missing NULL check was reported by Svace static analyzer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! LGTM
Lib/test/test_abc.py
Outdated
@@ -410,6 +410,13 @@ class C: | |||
with self.assertRaises(TypeError): | |||
issubclass(C(), A) | |||
|
|||
# bpo-34441: Check that issubclass() doesn't crash on bogus classes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to add also tests for following cases:
__subclasses__
is a callable with wrong signature.__subclasses__
raises an exception explicitly.__subclasses__
returns not a list.__subclasses__
returns a list containing not a type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I've added those cases.
Lib/test/test_abc.py
Outdated
lambda: [42], | ||
] | ||
|
||
for bs in bogus_subclasses: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: bs -> cls
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And it would be nice to use a subTest()
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: bs -> cls
Why? bs
denotes (would-be) callables, not classes.
And it would be nice to use a subTest() here.
Thank you, fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then may be use func
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK!
Lib/test/test_abc.py
Outdated
issubclass(int, S) | ||
|
||
# Also check that issubclass() propagates exceptions raised by | ||
# __subclasses__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Please end all sentences with a full stop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
@@ -0,0 +1,2 @@ | |||
Fix crash when an ``ABC``-derived class with non-callable ``__subclasses__`` is passed as the | |||
second argument to ``issubclass()``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
``issubclass()`` -> :func:`issubclass`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add "Patch by Your Name." and wrap lines at 80 chars.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, thank you.
Lib/test/test_abc.py
Outdated
# bpo-34441: Check that issubclass() doesn't crash on bogus classes | ||
bogus_subclasses = [ | ||
None, | ||
lambda x: None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make it returning a valid result: []
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
@@ -1,2 +1,3 @@ | |||
Fix crash when an ``ABC``-derived class with non-callable ``__subclasses__`` is passed as the | |||
second argument to ``issubclass()``. | |||
Fix crash when an ``ABC``-derived class with non-callable ``__subclasses__`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-callable __subclasses__
is not the only cause of a crash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. I've also updated the PR name.
Lib/test/test_abc.py
Outdated
__subclasses__ = func | ||
|
||
with self.subTest(i=i), \ | ||
self.assertRaises(TypeError): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is mostly a cosmetic change, I find the following style more readable (and we usually try to avoid using a backslash if it's possible to write a readable code):
with self.subTest(i=i):
self.assertRaises(TypeError, issubclass, int, S)
Or:
with self.subTest(i=i):
with self.assertRaises(TypeError):
issubclass(int, S)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I went for the second one for consistency with other checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you! :)
Thanks @izbyshev for the PR, and @berkerpeksag for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7. |
…subclasses__ (pythonGH-8835) The missing NULL check was reported by Svace static analyzer. (cherry picked from commit cdbf50c) Co-authored-by: Alexey Izbyshev <izbyshev@ispras.ru>
GH-8840 is a backport of this pull request to the 3.7 branch. |
The missing NULL check was reported by Svace static analyzer.
https://bugs.python.org/issue34441