From ce0bc6e6af34f4256de053dc7bdbb2120a276cd6 Mon Sep 17 00:00:00 2001 From: jab Date: Wed, 28 Feb 2018 23:55:11 +0000 Subject: [PATCH 1/2] Improve issubclass() error checking and message. --- Lib/_py_abc.py | 2 ++ Modules/_abc.c | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/Lib/_py_abc.py b/Lib/_py_abc.py index 6f42ef32fa6961..3c3aa8e3d61bee 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/Modules/_abc.c b/Modules/_abc.c index 504e23d9a74d3e..f413b2a1cfdffd 100644 --- a/Modules/_abc.c +++ b/Modules/_abc.c @@ -568,6 +568,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, *subclasses = NULL, *result = NULL; Py_ssize_t pos; int incache; From 175a18059580fcabc02886a881a3a20a2409c1a8 Mon Sep 17 00:00:00 2001 From: jab Date: Thu, 22 Mar 2018 23:14:33 +1300 Subject: [PATCH 2/2] add NEWS item --- Misc/ACKS | 1 + .../Core and Builtins/2018-03-22-23-09-06.bpo-33018.0ncEJV.rst | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2018-03-22-23-09-06.bpo-33018.0ncEJV.rst diff --git a/Misc/ACKS b/Misc/ACKS index d752d8a35434ae..05932a8d8991ca 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 00000000000000..e799e9834aa111 --- /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.