Skip to content

[3.7] bpo-33018: Improve issubclass() error checking and message. (GH-5944) #6188

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 1 commit into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions Lib/_py_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ def __instancecheck__(cls, instance):

def __subclasscheck__(cls, subclass):
"""Override for issubclass(subclass, cls)."""
if not isinstance(subclass, type):
raise TypeError('issubclass() arg 1 must be a class')
# Check cache
if subclass in cls._abc_cache:
return True
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ Dillon Brock
Richard Brodie
Michael Broghton
Ammar Brohi
Josh Bronson
Daniel Brotsky
Jean Brouwers
Gary S. Brown
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve consistency of errors raised by ``issubclass()`` when called with a
non-class and an abstract base class as the first and second arguments,
respectively. Patch by Josh Bronson.
5 changes: 5 additions & 0 deletions Modules/_abc.c
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,11 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
PyObject *subclass)
/*[clinic end generated code: output=b56c9e4a530e3894 input=1d947243409d10b8]*/
{
if (!PyType_Check(subclass)) {
PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class");
return NULL;
}

PyObject *ok, *mro = NULL, *subclasses = NULL, *result = NULL;
Py_ssize_t pos;
int incache;
Expand Down