MCPcopy Index your code
hub / github.com/python/cpython / test_altmro

Method test_altmro

Lib/test/test_descr.py:2012–2081  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2010 self.assertEqual(b.__class__, B)
2011
2012 def test_altmro(self):
2013 # Testing mro() and overriding it...
2014 class A(object):
2015 def f(self): return "A"
2016 class B(A):
2017 pass
2018 class C(A):
2019 def f(self): return "C"
2020 class D(B, C):
2021 pass
2022 self.assertEqual(A.mro(), [A, object])
2023 self.assertEqual(A.__mro__, (A, object))
2024 self.assertEqual(B.mro(), [B, A, object])
2025 self.assertEqual(B.__mro__, (B, A, object))
2026 self.assertEqual(C.mro(), [C, A, object])
2027 self.assertEqual(C.__mro__, (C, A, object))
2028 self.assertEqual(D.mro(), [D, B, C, A, object])
2029 self.assertEqual(D.__mro__, (D, B, C, A, object))
2030 self.assertEqual(D().f(), "C")
2031
2032 class PerverseMetaType(type):
2033 def mro(cls):
2034 L = type.mro(cls)
2035 L.reverse()
2036 return L
2037 class X(D,B,C,A, metaclass=PerverseMetaType):
2038 pass
2039 self.assertEqual(X.__mro__, (object, A, C, B, D, X))
2040 self.assertEqual(X().f(), "A")
2041
2042 try:
2043 class _metaclass(type):
2044 def mro(self):
2045 return [self, dict, object]
2046 class X(object, metaclass=_metaclass):
2047 pass
2048 # In CPython, the class creation above already raises
2049 # TypeError, as a protection against the fact that
2050 # instances of X would segfault it. In other Python
2051 # implementations it would be ok to let the class X
2052 # be created, but instead get a clean TypeError on the
2053 # __setitem__ below.
2054 x = object.__new__(X)
2055 x[5] = 6
2056 except TypeError:
2057 pass
2058 else:
2059 self.fail("devious mro() return not caught")
2060
2061 try:
2062 class _metaclass(type):
2063 def mro(self):
2064 return [1]
2065 class X(object, metaclass=_metaclass):
2066 pass
2067 except TypeError:
2068 pass
2069 else:

Callers

nothing calls this directly

Calls 7

DClass · 0.70
XClass · 0.70
assertEqualMethod · 0.45
mroMethod · 0.45
fMethod · 0.45
__new__Method · 0.45
failMethod · 0.45

Tested by

no test coverage detected