Пример #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 test_cache(self):
        import _abc

        class SomeABC:
            pass

        token_before = _abc.get_cache_token()
        assert _abc.get_cache_token() == token_before

        _abc._abc_init(SomeABC)
        assert _abc.get_cache_token() == token_before

        class SomeConcreteSubClass:
            pass

        _abc._abc_register(SomeABC, SomeConcreteSubClass)
        assert _abc.get_cache_token() != token_before

        registry, cache, negative_cache, negative_cache_version = _abc._get_dump(
            SomeABC)
        assert len(registry) == 1
        assert len(cache) == 0
        assert len(negative_cache) == 0
        assert negative_cache_version == token_before

        assert _abc._abc_subclasscheck(SomeABC, SomeConcreteSubClass)
        registry, cache, negative_cache, negative_cache_version = _abc._get_dump(
            SomeABC)
        assert len(registry) == 1
        assert len(cache) == 1
        assert len(negative_cache) == 0
        assert negative_cache_version == _abc.get_cache_token()

        class SomeOtherClass:
            pass

        assert not _abc._abc_subclasscheck(SomeABC, SomeOtherClass)
        registry, cache, negative_cache, negative_cache_version = _abc._get_dump(
            SomeABC)
        assert len(registry) == 1
        assert len(cache) == 1
        assert len(negative_cache) == 1
        assert negative_cache_version == _abc.get_cache_token()

        _abc._reset_caches(SomeABC)
        registry, cache, negative_cache, negative_cache_version = _abc._get_dump(
            SomeABC)
        assert len(registry) == 1
        assert len(cache) == 0
        assert len(negative_cache) == 0
        assert negative_cache_version == _abc.get_cache_token()

        _abc._reset_registry(SomeABC)
        registry, cache, negative_cache, negative_cache_version = _abc._get_dump(
            SomeABC)
        assert len(registry) == 0
        assert len(cache) == 0
        assert len(negative_cache) == 0
        assert negative_cache_version == _abc.get_cache_token()
Пример #3
0
    class ABCMeta(type):
        """Metaclass for defining Abstract Base Classes (ABCs).

        Use this metaclass to create an ABC.  An ABC can be subclassed
        directly, and then acts as a mix-in class.  You can also register
        unrelated concrete classes (even built-in classes) and unrelated
        ABCs as 'virtual subclasses' -- these and their descendants will
        be considered subclasses of the registering ABC by the built-in
        issubclass() function, but the registering ABC won't show up in
        their MRO (Method Resolution Order) nor will method
        implementations defined by the registering ABC be callable (not
        even via super()).
        """
        def __new__(mcls, name, bases, namespace, /, **kwargs):
            cls = super().__new__(mcls, name, bases, namespace, **kwargs)
            _abc_init(cls)
            return cls
Пример #4
0
 def __new__(mcls, name, bases, namespace, **kwargs):
     cls = super().__new__(mcls, name, bases, namespace, **kwargs)
     _abc_init(cls)
     return cls
Пример #5
0
 def __new__(mcls, name, bases, namespace, **kwargs):
     cls = super().__new__(mcls, name, bases, namespace, **kwargs)
     _abc_init(cls)
     return cls