Пример #1
0
    def test_basic(self):
        import _abc

        class SomeABC:
            pass

        _abc._abc_init(SomeABC)
        assert hasattr(SomeABC, '__abstractmethods__')

        class SomeConcreteSubClass:
            pass

        _abc._abc_register(SomeABC, SomeConcreteSubClass)

        # _abc._abc_instancecheck calls cls.__subclasscheck__, but since
        # we've only called _abc_init and haven't set the metaclass, we
        # need to monkeypatch SomeABC before calling _abc_instancecheck
        from types import MethodType
        SomeABC.__subclasscheck__ = MethodType(_abc._abc_subclasscheck,
                                               SomeABC)
        assert _abc._abc_instancecheck(SomeABC, SomeConcreteSubClass())
        assert _abc._abc_subclasscheck(SomeABC, SomeConcreteSubClass)

        class SomeOtherClass:
            pass

        assert not _abc._abc_instancecheck(SomeABC, SomeOtherClass())
        assert not _abc._abc_subclasscheck(SomeABC, SomeOtherClass)

        _abc._reset_registry(SomeABC)
        _abc._reset_caches(SomeABC)
        assert not _abc._abc_instancecheck(SomeABC, SomeConcreteSubClass())
        assert not _abc._abc_subclasscheck(SomeABC, SomeConcreteSubClass)
Пример #2
0
 def __instancecheck__(cls, instance):
     """Override for isinstance(instance, cls)."""
     return _abc_instancecheck(cls, instance)
Пример #3
0
 def __instancecheck__(cls, instance):
     """Override for isinstance(instance, cls)."""
     return _abc_instancecheck(cls, instance)