diff options
author | Nate <nate@so8r.es> | 2017-06-07 04:21:34 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2017-06-07 04:21:34 (GMT) |
commit | 09b6c0c71ea944f7e8b46998f3ebaf5b9fbe15f6 (patch) | |
tree | d739191285cb0132915499694eb6b8b97c1fe4f0 /Lib/inspect.py | |
parent | 6fb12b5c43945f61f3da82e33eafb4ddae2296ee (diff) | |
download | cpython-09b6c0c71ea944f7e8b46998f3ebaf5b9fbe15f6.zip cpython-09b6c0c71ea944f7e8b46998f3ebaf5b9fbe15f6.tar.gz cpython-09b6c0c71ea944f7e8b46998f3ebaf5b9fbe15f6.tar.bz2 |
[3.6] bpo-29822: make inspect.isabstract() work during __init_subclass__ (#1979)
At the time when an abstract base class' __init_subclass__ runs,
ABCMeta.__new__ has not yet finished running, so in the presence of
__init_subclass__, inspect.isabstract() can no longer depend only on
TPFLAGS_IS_ABSTRACT.
(cherry picked from commit fcfe80ec2592fed8b3941c79056a8737abef7d3b)
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r-- | Lib/inspect.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index a2dcb88..2894672 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -31,6 +31,7 @@ Here are some of the useful functions provided by this module: __author__ = ('Ka-Ping Yee <ping@lfw.org>', 'Yury Selivanov <yselivanov@sprymix.com>') +import abc import ast import dis import collections.abc @@ -291,7 +292,27 @@ def isroutine(object): def isabstract(object): """Return true if the object is an abstract base class (ABC).""" - return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT) + if not isinstance(object, type): + return False + if object.__flags__ & TPFLAGS_IS_ABSTRACT: + return True + if not issubclass(type(object), abc.ABCMeta): + return False + if hasattr(object, '__abstractmethods__'): + # It looks like ABCMeta.__new__ has finished running; + # TPFLAGS_IS_ABSTRACT should have been accurate. + return False + # It looks like ABCMeta.__new__ has not finished running yet; we're + # probably in __init_subclass__. We'll look for abstractmethods manually. + for name, value in object.__dict__.items(): + if getattr(value, "__isabstractmethod__", False): + return True + for base in object.__bases__: + for name in getattr(base, "__abstractmethods__", ()): + value = getattr(object, name, None) + if getattr(value, "__isabstractmethod__", False): + return True + return False def getmembers(object, predicate=None): """Return all members of an object as (name, value) pairs sorted by name. |