mirror of
https://github.com/python/cpython.git
synced 2025-10-02 13:22:19 +00:00
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.
(cherry picked from commit 40472dd42d
)
Co-authored-by: jab <jab@users.noreply.github.com>
This commit is contained in:
parent
8f46176f0e
commit
346964ba05
4 changed files with 11 additions and 0 deletions
|
@ -107,6 +107,8 @@ class ABCMeta(type):
|
||||||
|
|
||||||
def __subclasscheck__(cls, subclass):
|
def __subclasscheck__(cls, subclass):
|
||||||
"""Override for issubclass(subclass, cls)."""
|
"""Override for issubclass(subclass, cls)."""
|
||||||
|
if not isinstance(subclass, type):
|
||||||
|
raise TypeError('issubclass() arg 1 must be a class')
|
||||||
# Check cache
|
# Check cache
|
||||||
if subclass in cls._abc_cache:
|
if subclass in cls._abc_cache:
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -202,6 +202,7 @@ Dillon Brock
|
||||||
Richard Brodie
|
Richard Brodie
|
||||||
Michael Broghton
|
Michael Broghton
|
||||||
Ammar Brohi
|
Ammar Brohi
|
||||||
|
Josh Bronson
|
||||||
Daniel Brotsky
|
Daniel Brotsky
|
||||||
Jean Brouwers
|
Jean Brouwers
|
||||||
Gary S. Brown
|
Gary S. Brown
|
||||||
|
|
|
@ -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.
|
|
@ -569,6 +569,11 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
|
||||||
PyObject *subclass)
|
PyObject *subclass)
|
||||||
/*[clinic end generated code: output=b56c9e4a530e3894 input=1d947243409d10b8]*/
|
/*[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;
|
PyObject *ok, *mro = NULL, *subclasses = NULL, *result = NULL;
|
||||||
Py_ssize_t pos;
|
Py_ssize_t pos;
|
||||||
int incache;
|
int incache;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue