mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
raise a clear TypeError when trying to register a non-class
This commit is contained in:
parent
17527bedad
commit
2deb5c758a
3 changed files with 9 additions and 1 deletions
|
@ -96,7 +96,7 @@ class ABCMeta(type):
|
||||||
|
|
||||||
def register(cls, subclass):
|
def register(cls, subclass):
|
||||||
"""Register a virtual subclass of an ABC."""
|
"""Register a virtual subclass of an ABC."""
|
||||||
if not isinstance(cls, type):
|
if not isinstance(subclass, type):
|
||||||
raise TypeError("Can only register classes")
|
raise TypeError("Can only register classes")
|
||||||
if issubclass(subclass, cls):
|
if issubclass(subclass, cls):
|
||||||
return # Already a subclass
|
return # Already a subclass
|
||||||
|
|
|
@ -149,6 +149,12 @@ class TestABC(unittest.TestCase):
|
||||||
self.assertRaises(RuntimeError, C.register, A) # cycles not allowed
|
self.assertRaises(RuntimeError, C.register, A) # cycles not allowed
|
||||||
C.register(B) # ok
|
C.register(B) # ok
|
||||||
|
|
||||||
|
def test_register_non_class(self):
|
||||||
|
class A(object):
|
||||||
|
__metaclass__ = abc.ABCMeta
|
||||||
|
self.assertRaisesRegexp(TypeError, "Can only register classes",
|
||||||
|
A.register, 4)
|
||||||
|
|
||||||
def test_registration_transitiveness(self):
|
def test_registration_transitiveness(self):
|
||||||
class A:
|
class A:
|
||||||
__metaclass__ = abc.ABCMeta
|
__metaclass__ = abc.ABCMeta
|
||||||
|
|
|
@ -47,6 +47,8 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #7792: Registering non-classes to ABCs raised an obscure error.
|
||||||
|
|
||||||
- Removed the functions 'verify' and 'vereq' from Lib/test/test_support.py.
|
- Removed the functions 'verify' and 'vereq' from Lib/test/test_support.py.
|
||||||
|
|
||||||
- Issue #7773: Fix an UnboundLocalError in platform.linux_distribution() when
|
- Issue #7773: Fix an UnboundLocalError in platform.linux_distribution() when
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue