diff options
author | Éric Araujo <merwok@netwok.org> | 2011-02-24 18:03:10 (GMT) |
---|---|---|
committer | Éric Araujo <merwok@netwok.org> | 2011-02-24 18:03:10 (GMT) |
commit | 6c3787cb70d8efa5f70939a5db300f56a724d698 (patch) | |
tree | bab3ed85944db0993682255f1bb751c788f38001 /Lib/abc.py | |
parent | 5390d00cc6b99a375140f22e8d75580c1f4cb0a4 (diff) | |
download | cpython-6c3787cb70d8efa5f70939a5db300f56a724d698.zip cpython-6c3787cb70d8efa5f70939a5db300f56a724d698.tar.gz cpython-6c3787cb70d8efa5f70939a5db300f56a724d698.tar.bz2 |
Allow usage of SomeABC.register as a class decorator. Patch by Edoardo Spadolini (#10868).
Diffstat (limited to 'Lib/abc.py')
-rw-r--r-- | Lib/abc.py | 8 |
1 files changed, 6 insertions, 2 deletions
@@ -133,11 +133,14 @@ class ABCMeta(type): return cls def register(cls, subclass): - """Register a virtual subclass of an ABC.""" + """Register a virtual subclass of an ABC. + + Returns the subclass, to allow usage as a class decorator. + """ if not isinstance(subclass, type): raise TypeError("Can only register classes") if issubclass(subclass, cls): - return # Already a subclass + return subclass # Already a subclass # Subtle: test for cycles *after* testing for "already a subclass"; # this means we allow X.register(X) and interpret it as a no-op. if issubclass(cls, subclass): @@ -145,6 +148,7 @@ class ABCMeta(type): raise RuntimeError("Refusing to create an inheritance cycle") cls._abc_registry.add(subclass) ABCMeta._abc_invalidation_counter += 1 # Invalidate negative cache + return subclass def _dump_registry(cls, file=None): """Debug helper to print the ABC registry.""" |