mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
* Implement _collections.OrderedDict Extracted from #6799: native OrderedDict as a dict subclass, iterator mutation tracking, and mapping-view set operations. * Add OrderedDict, warnings source lines, and socket sendmsg Implement _collections.OrderedDict as a dict subclass with a linked list of insertion-order nodes. Pack struct F/D as native float/double complex pairs, and report unexpected __replace__ field names as a list repr. Register encodings.latin_1 at startup. Reject NUL in input() prompts. Copy non-exact type dicts in mapping order. Fetch warning source lines through _bless_my_loader and unicode splitlines. Keep the last warnings.filters list after deletion. Snapshot sendmsg sequences before converting buffers, parse ancillary ints with __index__, and dispatch AF_CAN by protocol. Store sys.trace and sys.profile on each ThreadSlot so _settraceallthreads/_setprofileallthreads are visible to sys.gettrace/getprofile on other threads. Use <frozen importlib._bootstrap> as frozen importlib co_filename. Keep HEAPTYPE socket objects GC-tracked. Assisted-by: Grok:grok-4.6 * Load marshal via readinto and dump sets in dumps order marshal.load fills a scratch buffer with readinto on a contiguous writable memoryview, and rejects a returned length other than the requested size. set and frozenset dumps write elements in sorted(v, key=marshal.dumps) order. Assisted-by: Grok:grok-4.6 * Add classmethod_descriptor and ABC IS_ABSTRACT flags Give METH_CLASS methods a distinct classmethod_descriptor type. Store Py_TPFLAGS_IS_ABSTRACT from __abstractmethods__ and look up that attribute only in the type's own dict. Expose defaultdict.default_factory as a member descriptor. Inherit bool's unused number slots from int. Mark set and frozenset __contains__ as METH_COEXIST. Parse classmethod, staticmethod, object.__new__, and heap type text signatures. Register interactive source in linecache. Assisted-by: Grok:grok-4.6 * address review: odict reuse, descr call, new wrapper Reuse odict node slots and walk the live list in next_after. Require both | operands to be dicts. Drop the odict iterator on GC clear. Bind classmethod descriptors through descr_get and update IS_ABSTRACT under the type lock. Give odict keys and items sq_contains. Walk tp_new_wrapper past new_wrapper so HEAPTYPE socket subclasses can call socket.__new__. Split FreeBSD Bluetooth protocol constants from Linux values. Assisted-by: Grok:grok-4.6 * Represent chflags bits with bitflagset Assisted-by: Grok:grok-4.6 * Represent access and mode bits with bitflagset Assisted-by: Grok:grok-4.6 * Continue unfinished REPL suites as incomplete input Assisted-by: Grok:grok-4.6 * Add pickle and os.stat inspect signatures Give _pickle.Pickler the clinic constructor text signature and accept fix_imports and buffer_callback as positional-or-keyword. Allow #[pyfunction(text_signature)] and use it so os.stat reports (path, *, dir_fd=None, follow_symlinks=True). Uncomment import _pickle and drop the pickle inspect expectedFailures. Assisted-by: Grok:grok-4.6 * Add Unpickler, dump/load, and lstat signatures Give _pickle Unpickler, dump, dumps, load, and loads clinic text signatures, and os.lstat (path, *, dir_fd=None). Assisted-by: Grok:grok-4.6 * Complete OrderedDict with insertion-order links Move the native OrderedDict into ordered_dict.rs and track order with a linked list of nodes, keeping mapping-view set operations. Assisted-by: Grok:grok-4.6 * address CI: stat casts, HCI_DATA_DIR, passing xfails Compare stat permission bits as Mode so Linux clippy does not flag a u32-to-u32 cast. Export HCI_DATA_DIR with the Bluetooth socket option values. Drop expectedFailure from tests that now pass. Assisted-by: Grok:grok-4.6 * Fix wasm teardown deadlock and bluetooth constants Assisted-by: Grok:grok-4.6 * Clear process-wide eval breaker bits in STW test Assisted-by: Grok:grok-4.6 * Drop exclusive-held panic that tripped wasm clippy Assisted-by: Grok:grok-4.6 * Fix OrderedDict contains and export Windows BDADDR Assisted-by: Grok:grok-4.6 * Add cmp_to_key and hash-once LRU cache _functools.cmp_to_key now returns KeyWrapper. LRU lookups store the key hash on _lru_list_elem so __hash__ is not called again on hits or eviction. Assisted-by: Grok:grok-4.6 * Allow subclass keywords on list, tuple, and float list.__init__ rejects keywords only when tp_new is still list's. tuple/float.__new__ reject them only when tp_init is still the base's. Assisted-by: Grok:grok-4.6 * Fix CI lint, test re-export, and structseq members Drop the duplicate clear_eval_breaker_for_test re-export. Format structseq imports. Expose struct sequence fields as readonly members so assignment raises "readonly attribute". Assisted-by: Grok:grok-4.6 * Allow subclass keywords on itertools and filter Positional-only constructors ignore extra keywords when a subclass has replaced tp_init, matching list/tuple/float. Assisted-by: Grok:grok-4.6 * Remove text_signature derive parameter Drop the string override on #[pyfunction] and #[pyclass]. Signatures are generated from the Rust parameter list only. Restore inspect expectedFailures that depended on the override. Assisted-by: Grok:grok-4.6 * Rename set contains_method to __contains__ Keep a borrowed contains helper for slots and C-API. The coexist pymethod is named __contains__. Assisted-by: Grok:grok-4.6 * Use MonitoringEvent instead of EVENT_* aliases Drop the unused MonitoringEvents bitflags and the EVENT_PY_START style mask constants. Call sites use the enum and .mask(). Assisted-by: Grok:grok-4.6 * Store warnings filters as PyListRef The list already serializes its elements. Drop the extra mutex around the cached list pointer. Assisted-by: Grok:grok-4.6 * Re-export clear_eval_breaker_for_test after rebase Keep the STW test helper visible without bringing back stop_bit_set. Assisted-by: Grok:grok-4.6 * Fix groupby reentrancy and islice arguments Give _grouper its own tgtkey and take currvalue after the key comparison. Accept __index__ on islice bounds, reject step 0, and drain the source up to stop. Assisted-by: Grok:grok-4.6 * SETREF warnings filters, onceregistry, and defaultaction Keep the last list, dict, or str after the module attributes are deleted by swapping the interpreter cache pointer. Do not store a None fallback for _warnings_context. Assisted-by: Grok:grok-4.6 * Iterate OrderedDict inner dict via next_entry &PyDict is not an iterator after the borrowed-ref signature change. Assisted-by: Grok:grok-4.6 * Remove expectedFailure from testBluetoothConstants Linux now exports BTPROTO_HCI and the rest of the set. Assisted-by: Grok:grok-4.6 * Borrow get_filter arguments as PyObject Match the warn_explicit call site that passes borrowed handles after the internal-helper borrow change. Assisted-by: Grok:grok-4.6 * Fix bool slots and SemHandle safety docs Leave unused bool number slots unset so inspect reports __add__ on int. Document SemHandle::from_raw safety. Assisted-by: Grok:grok-4.6 * Keep sendmsg errors and drop duplicate linecache Replace sendmsg iterable TypeError only. Register REPL source in linecache once per compiled input. Assisted-by: Grok:grok-4.6 * Nest REPL parse or-patterns for clippy Assisted-by: Grok:grok-4.6 * Mark pydoc os.stat signature test expectedFailure Assisted-by: Grok:grok-4.6 --------- Co-authored-by: Lee Dogeon <dev.moreal@gmail.com>
700 lines
24 KiB
Python
Vendored
700 lines
24 KiB
Python
Vendored
# Copyright 2007 Google, Inc. All Rights Reserved.
|
|
# Licensed to PSF under a Contributor Agreement.
|
|
|
|
# Note: each test is run with Python and C versions of ABCMeta. Except for
|
|
# test_ABC_helper(), which assures that abc.ABC is an instance of abc.ABCMeta.
|
|
|
|
"""Unit tests for abc.py."""
|
|
|
|
import unittest
|
|
|
|
import abc
|
|
import _py_abc
|
|
from inspect import isabstract
|
|
|
|
def test_factory(abc_ABCMeta, abc_get_cache_token):
|
|
class TestLegacyAPI(unittest.TestCase):
|
|
|
|
def test_abstractproperty_basics(self):
|
|
@abc.abstractproperty
|
|
def foo(self): pass
|
|
self.assertTrue(foo.__isabstractmethod__)
|
|
def bar(self): pass
|
|
self.assertNotHasAttr(bar, "__isabstractmethod__")
|
|
|
|
class C(metaclass=abc_ABCMeta):
|
|
@abc.abstractproperty
|
|
def foo(self): return 3
|
|
self.assertRaises(TypeError, C)
|
|
class D(C):
|
|
@property
|
|
def foo(self): return super().foo
|
|
self.assertEqual(D().foo, 3)
|
|
self.assertFalse(getattr(D.foo, "__isabstractmethod__", False))
|
|
|
|
def test_abstractclassmethod_basics(self):
|
|
@abc.abstractclassmethod
|
|
def foo(cls): pass
|
|
self.assertTrue(foo.__isabstractmethod__)
|
|
@classmethod
|
|
def bar(cls): pass
|
|
self.assertFalse(getattr(bar, "__isabstractmethod__", False))
|
|
|
|
class C(metaclass=abc_ABCMeta):
|
|
@abc.abstractclassmethod
|
|
def foo(cls): return cls.__name__
|
|
self.assertRaises(TypeError, C)
|
|
class D(C):
|
|
@classmethod
|
|
def foo(cls): return super().foo()
|
|
self.assertEqual(D.foo(), 'D')
|
|
self.assertEqual(D().foo(), 'D')
|
|
|
|
def test_abstractstaticmethod_basics(self):
|
|
@abc.abstractstaticmethod
|
|
def foo(): pass
|
|
self.assertTrue(foo.__isabstractmethod__)
|
|
@staticmethod
|
|
def bar(): pass
|
|
self.assertFalse(getattr(bar, "__isabstractmethod__", False))
|
|
|
|
class C(metaclass=abc_ABCMeta):
|
|
@abc.abstractstaticmethod
|
|
def foo(): return 3
|
|
self.assertRaises(TypeError, C)
|
|
class D(C):
|
|
@staticmethod
|
|
def foo(): return 4
|
|
self.assertEqual(D.foo(), 4)
|
|
self.assertEqual(D().foo(), 4)
|
|
|
|
|
|
class TestABC(unittest.TestCase):
|
|
|
|
def test_ABC_helper(self):
|
|
# create an ABC using the helper class and perform basic checks
|
|
class C(abc.ABC):
|
|
@classmethod
|
|
@abc.abstractmethod
|
|
def foo(cls): return cls.__name__
|
|
self.assertEqual(type(C), abc.ABCMeta)
|
|
self.assertRaises(TypeError, C)
|
|
class D(C):
|
|
@classmethod
|
|
def foo(cls): return super().foo()
|
|
self.assertEqual(D.foo(), 'D')
|
|
|
|
def test_abstractmethod_basics(self):
|
|
@abc.abstractmethod
|
|
def foo(self): pass
|
|
self.assertTrue(foo.__isabstractmethod__)
|
|
def bar(self): pass
|
|
self.assertNotHasAttr(bar, "__isabstractmethod__")
|
|
|
|
def test_abstractproperty_basics(self):
|
|
@property
|
|
@abc.abstractmethod
|
|
def foo(self): pass
|
|
self.assertTrue(foo.__isabstractmethod__)
|
|
def bar(self): pass
|
|
self.assertFalse(getattr(bar, "__isabstractmethod__", False))
|
|
|
|
class C(metaclass=abc_ABCMeta):
|
|
@property
|
|
@abc.abstractmethod
|
|
def foo(self): return 3
|
|
self.assertRaises(TypeError, C)
|
|
class D(C):
|
|
@C.foo.getter
|
|
def foo(self): return super().foo
|
|
self.assertEqual(D().foo, 3)
|
|
|
|
def test_abstractclassmethod_basics(self):
|
|
@classmethod
|
|
@abc.abstractmethod
|
|
def foo(cls): pass
|
|
self.assertTrue(foo.__isabstractmethod__)
|
|
@classmethod
|
|
def bar(cls): pass
|
|
self.assertFalse(getattr(bar, "__isabstractmethod__", False))
|
|
|
|
class C(metaclass=abc_ABCMeta):
|
|
@classmethod
|
|
@abc.abstractmethod
|
|
def foo(cls): return cls.__name__
|
|
self.assertRaises(TypeError, C)
|
|
class D(C):
|
|
@classmethod
|
|
def foo(cls): return super().foo()
|
|
self.assertEqual(D.foo(), 'D')
|
|
self.assertEqual(D().foo(), 'D')
|
|
|
|
def test_abstractstaticmethod_basics(self):
|
|
@staticmethod
|
|
@abc.abstractmethod
|
|
def foo(): pass
|
|
self.assertTrue(foo.__isabstractmethod__)
|
|
@staticmethod
|
|
def bar(): pass
|
|
self.assertFalse(getattr(bar, "__isabstractmethod__", False))
|
|
|
|
class C(metaclass=abc_ABCMeta):
|
|
@staticmethod
|
|
@abc.abstractmethod
|
|
def foo(): return 3
|
|
self.assertRaises(TypeError, C)
|
|
class D(C):
|
|
@staticmethod
|
|
def foo(): return 4
|
|
self.assertEqual(D.foo(), 4)
|
|
self.assertEqual(D().foo(), 4)
|
|
|
|
def test_object_new_with_one_abstractmethod(self):
|
|
class C(metaclass=abc_ABCMeta):
|
|
@abc.abstractmethod
|
|
def method_one(self):
|
|
pass
|
|
msg = r"class C without an implementation for abstract method 'method_one'"
|
|
self.assertRaisesRegex(TypeError, msg, C)
|
|
|
|
def test_object_new_with_many_abstractmethods(self):
|
|
class C(metaclass=abc_ABCMeta):
|
|
@abc.abstractmethod
|
|
def method_one(self):
|
|
pass
|
|
@abc.abstractmethod
|
|
def method_two(self):
|
|
pass
|
|
msg = r"class C without an implementation for abstract methods 'method_one', 'method_two'"
|
|
self.assertRaisesRegex(TypeError, msg, C)
|
|
|
|
def test_abstractmethod_integration(self):
|
|
for abstractthing in [abc.abstractmethod, abc.abstractproperty,
|
|
abc.abstractclassmethod,
|
|
abc.abstractstaticmethod]:
|
|
class C(metaclass=abc_ABCMeta):
|
|
@abstractthing
|
|
def foo(self): pass # abstract
|
|
def bar(self): pass # concrete
|
|
self.assertEqual(C.__abstractmethods__, {"foo"})
|
|
self.assertRaises(TypeError, C) # because foo is abstract
|
|
self.assertTrue(isabstract(C))
|
|
class D(C):
|
|
def bar(self): pass # concrete override of concrete
|
|
self.assertEqual(D.__abstractmethods__, {"foo"})
|
|
self.assertRaises(TypeError, D) # because foo is still abstract
|
|
self.assertTrue(isabstract(D))
|
|
class E(D):
|
|
def foo(self): pass
|
|
self.assertEqual(E.__abstractmethods__, set())
|
|
E() # now foo is concrete, too
|
|
self.assertFalse(isabstract(E))
|
|
class F(E):
|
|
@abstractthing
|
|
def bar(self): pass # abstract override of concrete
|
|
self.assertEqual(F.__abstractmethods__, {"bar"})
|
|
self.assertRaises(TypeError, F) # because bar is abstract now
|
|
self.assertTrue(isabstract(F))
|
|
|
|
def test_descriptors_with_abstractmethod(self):
|
|
class C(metaclass=abc_ABCMeta):
|
|
@property
|
|
@abc.abstractmethod
|
|
def foo(self): return 3
|
|
@foo.setter
|
|
@abc.abstractmethod
|
|
def foo(self, val): pass
|
|
self.assertRaises(TypeError, C)
|
|
class D(C):
|
|
@C.foo.getter
|
|
def foo(self): return super().foo
|
|
self.assertRaises(TypeError, D)
|
|
class E(D):
|
|
@D.foo.setter
|
|
def foo(self, val): pass
|
|
self.assertEqual(E().foo, 3)
|
|
# check that the property's __isabstractmethod__ descriptor does the
|
|
# right thing when presented with a value that fails truth testing:
|
|
class NotBool(object):
|
|
def __bool__(self):
|
|
raise ValueError()
|
|
__len__ = __bool__
|
|
with self.assertRaises(ValueError):
|
|
class F(C):
|
|
def bar(self):
|
|
pass
|
|
bar.__isabstractmethod__ = NotBool()
|
|
foo = property(bar)
|
|
|
|
|
|
def test_customdescriptors_with_abstractmethod(self):
|
|
class Descriptor:
|
|
def __init__(self, fget, fset=None):
|
|
self._fget = fget
|
|
self._fset = fset
|
|
def getter(self, callable):
|
|
return Descriptor(callable, self._fget)
|
|
def setter(self, callable):
|
|
return Descriptor(self._fget, callable)
|
|
@property
|
|
def __isabstractmethod__(self):
|
|
return (getattr(self._fget, '__isabstractmethod__', False)
|
|
or getattr(self._fset, '__isabstractmethod__', False))
|
|
class C(metaclass=abc_ABCMeta):
|
|
@Descriptor
|
|
@abc.abstractmethod
|
|
def foo(self): return 3
|
|
@foo.setter
|
|
@abc.abstractmethod
|
|
def foo(self, val): pass
|
|
self.assertRaises(TypeError, C)
|
|
class D(C):
|
|
@C.foo.getter
|
|
def foo(self): return super().foo
|
|
self.assertRaises(TypeError, D)
|
|
class E(D):
|
|
@D.foo.setter
|
|
def foo(self, val): pass
|
|
self.assertFalse(E.foo.__isabstractmethod__)
|
|
|
|
def test_metaclass_abc(self):
|
|
# Metaclasses can be ABCs, too.
|
|
class A(metaclass=abc_ABCMeta):
|
|
@abc.abstractmethod
|
|
def x(self):
|
|
pass
|
|
self.assertEqual(A.__abstractmethods__, {"x"})
|
|
class meta(type, A):
|
|
def x(self):
|
|
return 1
|
|
class C(metaclass=meta):
|
|
pass
|
|
|
|
def test_registration_basics(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
pass
|
|
class B(object):
|
|
pass
|
|
b = B()
|
|
self.assertNotIsSubclass(B, A)
|
|
self.assertNotIsSubclass(B, (A,))
|
|
self.assertNotIsInstance(b, A)
|
|
self.assertNotIsInstance(b, (A,))
|
|
B1 = A.register(B)
|
|
self.assertIsSubclass(B, A)
|
|
self.assertIsSubclass(B, (A,))
|
|
self.assertIsInstance(b, A)
|
|
self.assertIsInstance(b, (A,))
|
|
self.assertIs(B1, B)
|
|
class C(B):
|
|
pass
|
|
c = C()
|
|
self.assertIsSubclass(C, A)
|
|
self.assertIsSubclass(C, (A,))
|
|
self.assertIsInstance(c, A)
|
|
self.assertIsInstance(c, (A,))
|
|
|
|
def test_register_as_class_deco(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
pass
|
|
@A.register
|
|
class B(object):
|
|
pass
|
|
b = B()
|
|
self.assertIsSubclass(B, A)
|
|
self.assertIsSubclass(B, (A,))
|
|
self.assertIsInstance(b, A)
|
|
self.assertIsInstance(b, (A,))
|
|
@A.register
|
|
class C(B):
|
|
pass
|
|
c = C()
|
|
self.assertIsSubclass(C, A)
|
|
self.assertIsSubclass(C, (A,))
|
|
self.assertIsInstance(c, A)
|
|
self.assertIsInstance(c, (A,))
|
|
self.assertIs(C, A.register(C))
|
|
|
|
def test_isinstance_invalidation(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
pass
|
|
class B:
|
|
pass
|
|
b = B()
|
|
self.assertNotIsInstance(b, A)
|
|
self.assertNotIsInstance(b, (A,))
|
|
token_old = abc_get_cache_token()
|
|
A.register(B)
|
|
token_new = abc_get_cache_token()
|
|
self.assertGreater(token_new, token_old)
|
|
self.assertIsInstance(b, A)
|
|
self.assertIsInstance(b, (A,))
|
|
|
|
def test_registration_builtins(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
pass
|
|
A.register(int)
|
|
self.assertIsInstance(42, A)
|
|
self.assertIsInstance(42, (A,))
|
|
self.assertIsSubclass(int, A)
|
|
self.assertIsSubclass(int, (A,))
|
|
class B(A):
|
|
pass
|
|
B.register(str)
|
|
class C(str): pass
|
|
self.assertIsInstance("", A)
|
|
self.assertIsInstance("", (A,))
|
|
self.assertIsSubclass(str, A)
|
|
self.assertIsSubclass(str, (A,))
|
|
self.assertIsSubclass(C, A)
|
|
self.assertIsSubclass(C, (A,))
|
|
|
|
def test_registration_edge_cases(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
pass
|
|
A.register(A) # should pass silently
|
|
class A1(A):
|
|
pass
|
|
self.assertRaises(RuntimeError, A1.register, A) # cycles not allowed
|
|
class B(object):
|
|
pass
|
|
A1.register(B) # ok
|
|
A1.register(B) # should pass silently
|
|
class C(A):
|
|
pass
|
|
A.register(C) # should pass silently
|
|
self.assertRaises(RuntimeError, C.register, A) # cycles not allowed
|
|
C.register(B) # ok
|
|
|
|
def test_register_non_class(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
pass
|
|
self.assertRaisesRegex(TypeError, "Can only register classes",
|
|
A.register, 4)
|
|
|
|
def test_registration_transitiveness(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
pass
|
|
self.assertIsSubclass(A, A)
|
|
self.assertIsSubclass(A, (A,))
|
|
class B(metaclass=abc_ABCMeta):
|
|
pass
|
|
self.assertNotIsSubclass(A, B)
|
|
self.assertNotIsSubclass(A, (B,))
|
|
self.assertNotIsSubclass(B, A)
|
|
self.assertNotIsSubclass(B, (A,))
|
|
class C(metaclass=abc_ABCMeta):
|
|
pass
|
|
A.register(B)
|
|
class B1(B):
|
|
pass
|
|
self.assertIsSubclass(B1, A)
|
|
self.assertIsSubclass(B1, (A,))
|
|
class C1(C):
|
|
pass
|
|
B1.register(C1)
|
|
self.assertNotIsSubclass(C, B)
|
|
self.assertNotIsSubclass(C, (B,))
|
|
self.assertNotIsSubclass(C, B1)
|
|
self.assertNotIsSubclass(C, (B1,))
|
|
self.assertIsSubclass(C1, A)
|
|
self.assertIsSubclass(C1, (A,))
|
|
self.assertIsSubclass(C1, B)
|
|
self.assertIsSubclass(C1, (B,))
|
|
self.assertIsSubclass(C1, B1)
|
|
self.assertIsSubclass(C1, (B1,))
|
|
C1.register(int)
|
|
class MyInt(int):
|
|
pass
|
|
self.assertIsSubclass(MyInt, A)
|
|
self.assertIsSubclass(MyInt, (A,))
|
|
self.assertIsInstance(42, A)
|
|
self.assertIsInstance(42, (A,))
|
|
|
|
def test_issubclass_bad_arguments(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
pass
|
|
|
|
with self.assertRaises(TypeError):
|
|
issubclass({}, A) # unhashable
|
|
|
|
with self.assertRaises(TypeError):
|
|
issubclass(42, A) # No __mro__
|
|
|
|
# Python version supports any iterable as __mro__.
|
|
# But it's implementation detail and don't emulate it in C version.
|
|
class C:
|
|
__mro__ = 42 # __mro__ is not tuple
|
|
|
|
with self.assertRaises(TypeError):
|
|
issubclass(C(), A)
|
|
|
|
# bpo-34441: Check that issubclass() doesn't crash on bogus
|
|
# classes.
|
|
bogus_subclasses = [
|
|
None,
|
|
lambda x: [],
|
|
lambda: 42,
|
|
lambda: [42],
|
|
]
|
|
|
|
for i, func in enumerate(bogus_subclasses):
|
|
class S(metaclass=abc_ABCMeta):
|
|
__subclasses__ = func
|
|
|
|
with self.subTest(i=i):
|
|
with self.assertRaises(TypeError):
|
|
issubclass(int, S)
|
|
|
|
# Also check that issubclass() propagates exceptions raised by
|
|
# __subclasses__.
|
|
class CustomError(Exception): ...
|
|
exc_msg = "exception from __subclasses__"
|
|
|
|
def raise_exc():
|
|
raise CustomError(exc_msg)
|
|
|
|
class S(metaclass=abc_ABCMeta):
|
|
__subclasses__ = raise_exc
|
|
|
|
with self.assertRaisesRegex(CustomError, exc_msg):
|
|
issubclass(int, S)
|
|
|
|
def test_subclasshook(self):
|
|
class A(metaclass=abc.ABCMeta):
|
|
@classmethod
|
|
def __subclasshook__(cls, C):
|
|
if cls is A:
|
|
return 'foo' in C.__dict__
|
|
return NotImplemented
|
|
self.assertNotIsSubclass(A, A)
|
|
self.assertNotIsSubclass(A, (A,))
|
|
class B:
|
|
foo = 42
|
|
self.assertIsSubclass(B, A)
|
|
self.assertIsSubclass(B, (A,))
|
|
class C:
|
|
spam = 42
|
|
self.assertNotIsSubclass(C, A)
|
|
self.assertNotIsSubclass(C, (A,))
|
|
|
|
def test_all_new_methods_are_called(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
pass
|
|
class B(object):
|
|
counter = 0
|
|
def __new__(cls):
|
|
B.counter += 1
|
|
return super().__new__(cls)
|
|
class C(A, B):
|
|
pass
|
|
self.assertEqual(B.counter, 0)
|
|
C()
|
|
self.assertEqual(B.counter, 1)
|
|
|
|
def test_ABC_has___slots__(self):
|
|
self.assertHasAttr(abc.ABC, '__slots__')
|
|
|
|
def test_tricky_new_works(self):
|
|
def with_metaclass(meta, *bases):
|
|
class metaclass(type):
|
|
def __new__(cls, name, this_bases, d):
|
|
return meta(name, bases, d)
|
|
return type.__new__(metaclass, 'temporary_class', (), {})
|
|
class A: ...
|
|
class B: ...
|
|
class C(with_metaclass(abc_ABCMeta, A, B)):
|
|
pass
|
|
self.assertEqual(C.__class__, abc_ABCMeta)
|
|
|
|
def test_update_del(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
@abc.abstractmethod
|
|
def foo(self):
|
|
pass
|
|
|
|
del A.foo
|
|
self.assertEqual(A.__abstractmethods__, {'foo'})
|
|
self.assertNotHasAttr(A, 'foo')
|
|
|
|
abc.update_abstractmethods(A)
|
|
|
|
self.assertEqual(A.__abstractmethods__, set())
|
|
A()
|
|
|
|
|
|
def test_update_new_abstractmethods(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
@abc.abstractmethod
|
|
def bar(self):
|
|
pass
|
|
|
|
@abc.abstractmethod
|
|
def updated_foo(self):
|
|
pass
|
|
|
|
A.foo = updated_foo
|
|
abc.update_abstractmethods(A)
|
|
self.assertEqual(A.__abstractmethods__, {'foo', 'bar'})
|
|
msg = "class A without an implementation for abstract methods 'bar', 'foo'"
|
|
self.assertRaisesRegex(TypeError, msg, A)
|
|
|
|
def test_update_implementation(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
@abc.abstractmethod
|
|
def foo(self):
|
|
pass
|
|
|
|
class B(A):
|
|
pass
|
|
|
|
msg = "class B without an implementation for abstract method 'foo'"
|
|
self.assertRaisesRegex(TypeError, msg, B)
|
|
self.assertEqual(B.__abstractmethods__, {'foo'})
|
|
|
|
B.foo = lambda self: None
|
|
|
|
abc.update_abstractmethods(B)
|
|
|
|
B()
|
|
self.assertEqual(B.__abstractmethods__, set())
|
|
|
|
def test_update_as_decorator(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
@abc.abstractmethod
|
|
def foo(self):
|
|
pass
|
|
|
|
def class_decorator(cls):
|
|
cls.foo = lambda self: None
|
|
return cls
|
|
|
|
@abc.update_abstractmethods
|
|
@class_decorator
|
|
class B(A):
|
|
pass
|
|
|
|
B()
|
|
self.assertEqual(B.__abstractmethods__, set())
|
|
|
|
def test_update_non_abc(self):
|
|
class A:
|
|
pass
|
|
|
|
@abc.abstractmethod
|
|
def updated_foo(self):
|
|
pass
|
|
|
|
A.foo = updated_foo
|
|
abc.update_abstractmethods(A)
|
|
A()
|
|
self.assertNotHasAttr(A, '__abstractmethods__')
|
|
|
|
def test_update_del_implementation(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
@abc.abstractmethod
|
|
def foo(self):
|
|
pass
|
|
|
|
class B(A):
|
|
def foo(self):
|
|
pass
|
|
|
|
B()
|
|
|
|
del B.foo
|
|
|
|
abc.update_abstractmethods(B)
|
|
|
|
msg = "class B without an implementation for abstract method 'foo'"
|
|
self.assertRaisesRegex(TypeError, msg, B)
|
|
|
|
def test_update_layered_implementation(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
@abc.abstractmethod
|
|
def foo(self):
|
|
pass
|
|
|
|
class B(A):
|
|
pass
|
|
|
|
class C(B):
|
|
def foo(self):
|
|
pass
|
|
|
|
C()
|
|
|
|
del C.foo
|
|
|
|
abc.update_abstractmethods(C)
|
|
|
|
msg = "class C without an implementation for abstract method 'foo'"
|
|
self.assertRaisesRegex(TypeError, msg, C)
|
|
|
|
def test_update_multi_inheritance(self):
|
|
class A(metaclass=abc_ABCMeta):
|
|
@abc.abstractmethod
|
|
def foo(self):
|
|
pass
|
|
|
|
class B(metaclass=abc_ABCMeta):
|
|
def foo(self):
|
|
pass
|
|
|
|
class C(B, A):
|
|
@abc.abstractmethod
|
|
def foo(self):
|
|
pass
|
|
|
|
self.assertEqual(C.__abstractmethods__, {'foo'})
|
|
|
|
del C.foo
|
|
|
|
abc.update_abstractmethods(C)
|
|
|
|
self.assertEqual(C.__abstractmethods__, set())
|
|
|
|
C()
|
|
|
|
|
|
class TestABCWithInitSubclass(unittest.TestCase):
|
|
def test_works_with_init_subclass(self):
|
|
class abc_ABC(metaclass=abc_ABCMeta):
|
|
__slots__ = ()
|
|
saved_kwargs = {}
|
|
class ReceivesClassKwargs:
|
|
def __init_subclass__(cls, **kwargs):
|
|
super().__init_subclass__()
|
|
saved_kwargs.update(kwargs)
|
|
class Receiver(ReceivesClassKwargs, abc_ABC, x=1, y=2, z=3):
|
|
pass
|
|
self.assertEqual(saved_kwargs, dict(x=1, y=2, z=3))
|
|
|
|
def test_positional_only_and_kwonlyargs_with_init_subclass(self):
|
|
saved_kwargs = {}
|
|
|
|
class A:
|
|
def __init_subclass__(cls, **kwargs):
|
|
super().__init_subclass__()
|
|
saved_kwargs.update(kwargs)
|
|
|
|
class B(A, metaclass=abc_ABCMeta, name="test"):
|
|
pass
|
|
self.assertEqual(saved_kwargs, dict(name="test"))
|
|
|
|
return TestLegacyAPI, TestABC, TestABCWithInitSubclass
|
|
|
|
TestLegacyAPI_Py, TestABC_Py, TestABCWithInitSubclass_Py = test_factory(_py_abc.ABCMeta,
|
|
_py_abc.get_cache_token)
|
|
TestLegacyAPI_C, TestABC_C, TestABCWithInitSubclass_C = test_factory(abc.ABCMeta,
|
|
abc.get_cache_token)
|
|
|
|
# gh-130095: The _py_abc tests are not thread-safe when run with
|
|
# `--parallel-threads`
|
|
TestLegacyAPI_Py.__unittest_thread_unsafe__ = True
|
|
TestABC_Py.__unittest_thread_unsafe__ = True
|
|
TestABCWithInitSubclass_Py.__unittest_thread_unsafe__ = True
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|