Skip to content

bpo-34441: Fix ABC.__subclasscheck__ crash on a class with invalid __subclasses__ #8835

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 9 commits into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
30 changes: 30 additions & 0 deletions Lib/test/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,36 @@ class C:
with self.assertRaises(TypeError):
issubclass(C(), A)

# bpo-34441: Check that issubclass() doesn't crash on bogus
# classes.
bogus_subclasses = [
None,
lambda x: [],
lambda: 42,
lambda: [42],
]

for i, bs in enumerate(bogus_subclasses):
class S(metaclass=abc_ABCMeta):
__subclasses__ = bs

with self.subTest(i=i), \
self.assertRaises(TypeError):
Copy link
Member

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)

Copy link
Contributor Author

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.

issubclass(int, S)

# Also check that issubclass() propagates exceptions raised by
# __subclasses__.
exc_msg = "exception from __subclasses__"

def raise_exc():
raise Exception(exc_msg)

class S(metaclass=abc_ABCMeta):
__subclasses__ = raise_exc

with self.assertRaisesRegex(Exception, exc_msg):
issubclass(int, S)

def test_all_new_methods_are_called(self):
class A(metaclass=abc_ABCMeta):
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix crash when an ``ABC``-derived class with non-callable ``__subclasses__``
Copy link
Member

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.

Copy link
Contributor Author

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.

is passed as the second argument to :func:`issubclass()`. Patch by Alexey
Izbyshev.
3 changes: 3 additions & 0 deletions Modules/_abc.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,9 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,

/* 6. Check if it's a subclass of a subclass (recursive). */
subclasses = PyObject_CallMethod(self, "__subclasses__", NULL);
if (subclasses == NULL) {
goto end;
}
if (!PyList_Check(subclasses)) {
PyErr_SetString(PyExc_TypeError, "__subclasses__() must return a list");
goto end;
Expand Down