bpo-34441: Fix ABC.__subclasscheck__ crash on a class with invalid __subclasses__#8835
bpo-34441: Fix ABC.__subclasscheck__ crash on a class with invalid __subclasses__#8835berkerpeksag merged 9 commits intopython:masterfrom
Conversation
…le __subclasses__ The missing NULL check was reported by Svace static analyzer.
Lib/test/test_abc.py
Outdated
| 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.
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.
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.
And it would be nice to use a subTest() here.
There was a problem hiding this comment.
Nit: bs -> cls
Why? bs denotes (would-be) callables, not classes.
And it would be nice to use a subTest() here.
Thank you, fixed.
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.
Nit: Please end all sentences with a full stop.
| @@ -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.
``issubclass()`` -> :func:`issubclass`
There was a problem hiding this comment.
Please add "Patch by Your Name." and wrap lines at 80 chars.
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.
Make it returning a valid result: [].
| @@ -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.
Non-callable __subclasses__ is not the only cause of a crash.
There was a problem hiding this comment.
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.
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.
OK, I went for the second one for consistency with other checks.
|
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