test_multiple_inheritance.py revision 12037:d28054ac6ec9
1import pytest 2 3 4def test_multiple_inheritance_cpp(): 5 from pybind11_tests import MIType 6 7 mt = MIType(3, 4) 8 9 assert mt.foo() == 3 10 assert mt.bar() == 4 11 12 13def test_multiple_inheritance_mix1(): 14 from pybind11_tests import Base2 15 16 class Base1: 17 def __init__(self, i): 18 self.i = i 19 20 def foo(self): 21 return self.i 22 23 class MITypePy(Base1, Base2): 24 def __init__(self, i, j): 25 Base1.__init__(self, i) 26 Base2.__init__(self, j) 27 28 mt = MITypePy(3, 4) 29 30 assert mt.foo() == 3 31 assert mt.bar() == 4 32 33 34def test_multiple_inheritance_mix2(): 35 from pybind11_tests import Base1 36 37 class Base2: 38 def __init__(self, i): 39 self.i = i 40 41 def bar(self): 42 return self.i 43 44 class MITypePy(Base1, Base2): 45 def __init__(self, i, j): 46 Base1.__init__(self, i) 47 Base2.__init__(self, j) 48 49 mt = MITypePy(3, 4) 50 51 assert mt.foo() == 3 52 assert mt.bar() == 4 53 54 55def test_multiple_inheritance_error(): 56 """Inheriting from multiple C++ bases in Python is not supported""" 57 from pybind11_tests import Base1, Base2 58 59 with pytest.raises(TypeError) as excinfo: 60 # noinspection PyUnusedLocal 61 class MI(Base1, Base2): 62 pass 63 assert "Can't inherit from multiple C++ classes in Python" in str(excinfo.value) 64 65 66def test_multiple_inheritance_virtbase(): 67 from pybind11_tests import Base12a, bar_base2a, bar_base2a_sharedptr 68 69 class MITypePy(Base12a): 70 def __init__(self, i, j): 71 Base12a.__init__(self, i, j) 72 73 mt = MITypePy(3, 4) 74 assert mt.bar() == 4 75 assert bar_base2a(mt) == 4 76 assert bar_base2a_sharedptr(mt) == 4 77 78 79def test_mi_static_properties(): 80 """Mixing bases with and without static properties should be possible 81 and the result should be independent of base definition order""" 82 from pybind11_tests import mi 83 84 for d in (mi.VanillaStaticMix1(), mi.VanillaStaticMix2()): 85 assert d.vanilla() == "Vanilla" 86 assert d.static_func1() == "WithStatic1" 87 assert d.static_func2() == "WithStatic2" 88 assert d.static_func() == d.__class__.__name__ 89 90 mi.WithStatic1.static_value1 = 1 91 mi.WithStatic2.static_value2 = 2 92 assert d.static_value1 == 1 93 assert d.static_value2 == 2 94 assert d.static_value == 12 95 96 d.static_value1 = 0 97 assert d.static_value1 == 0 98 d.static_value2 = 0 99 assert d.static_value2 == 0 100 d.static_value = 0 101 assert d.static_value == 0 102 103 104@pytest.unsupported_on_pypy 105def test_mi_dynamic_attributes(): 106 """Mixing bases with and without dynamic attribute support""" 107 from pybind11_tests import mi 108 109 for d in (mi.VanillaDictMix1(), mi.VanillaDictMix2()): 110 d.dynamic = 1 111 assert d.dynamic == 1 112