]> granicus.if.org Git - python/commitdiff
bpo-33018: Improve issubclass() error checking and message. (GH-5944)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 22 Mar 2018 11:49:26 +0000 (04:49 -0700)
committerGitHub <noreply@github.com>
Thu, 22 Mar 2018 11:49:26 +0000 (04:49 -0700)
This improves error message for situations when a non-class is
checked w.r.t. an abstract base class.
(cherry picked from commit 40472dd42de4f7265d456458cd13ad6894d736db)

Co-authored-by: jab <jab@users.noreply.github.com>
Lib/_py_abc.py
Misc/ACKS
Misc/NEWS.d/next/Core and Builtins/2018-03-22-23-09-06.bpo-33018.0ncEJV.rst [new file with mode: 0644]
Modules/_abc.c

index 6f42ef32fa696151a1c68ddae664b22b3a7d8949..3c3aa8e3d61bee9d789400af632136f6c3d1e340 100644 (file)
@@ -107,6 +107,8 @@ class ABCMeta(type):
 
     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
index 9c7cb6f0057db3c983369c07d242d00869a668a0..729e88e565828240d5423bcb295a3703dbc9bc76 100644 (file)
--- 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 (file)
index 0000000..e799e98
--- /dev/null
@@ -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.
index 862883987fb7f8cbb6fbea62bf8020b49e9177ce..7daa18e37e405b7fd2ef0caeadfb672395224b32 100644 (file)
@@ -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;