[Python-checkins] bpo-33018: Improve issubclass() error checking and message. (GH-5944)

Ivan Levkivskyi webhook-mailer at python.org
Thu Mar 22 07:26:09 EDT 2018

commit: 40472dd42de4f7265d456458cd13ad6894d736db
branch: master
author: jab <jab at users.noreply.github.com>
committer: Ivan Levkivskyi <levkivskyi at gmail.com>
date: 2018-03-22T11:26:06Z

bpo-33018: Improve issubclass() error checking and message. (GH-5944)

This improves error message for situations when a non-class is
checked w.r.t. an abstract base class.

A Misc/NEWS.d/next/Core and Builtins/2018-03-22-23-09-06.bpo-33018.0ncEJV.rst
M Lib/_py_abc.py
M Modules/_abc.c

diff --git a/Lib/_py_abc.py b/Lib/_py_abc.py
index 6f42ef32fa69..3c3aa8e3d61b 100644
--- a/Lib/_py_abc.py
+++ b/Lib/_py_abc.py
@@ -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
diff --git a/Misc/ACKS b/Misc/ACKS
index d752d8a35434..05932a8d8991 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -202,6 +202,7 @@ Dillon Brock
 Richard Brodie
 Michael Broghton
 Ammar Brohi
+Josh Bronson
 Daniel Brotsky
 Jean Brouwers
 Gary S. Brown
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-03-22-23-09-06.bpo-33018.0ncEJV.rst b/Misc/NEWS.d/next/Core and Builtins/2018-03-22-23-09-06.bpo-33018.0ncEJV.rst
new file mode 100644
index 000000000000..e799e9834aa1
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2018-03-22-23-09-06.bpo-33018.0ncEJV.rst	
@@ -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.
diff --git a/Modules/_abc.c b/Modules/_abc.c
index 862883987fb7..7daa18e37e40 100644
--- a/Modules/_abc.c
+++ b/Modules/_abc.c
@@ -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;

More information about the Python-checkins mailing list