blob: 2d40d2565d886f6f074dfc29e0978fe6074ad3df [file] [log] [blame]
Dean Moldovan08cbe8d2017-02-15 21:10:25 +01001import pytest
Jason Rhinelander14e70652017-04-21 17:14:22 -04002from pybind11_tests import ConstructorStats
Dean Moldovan08cbe8d2017-02-15 21:10:25 +01003
4
Dean Moldovanbad17402016-11-20 21:21:54 +01005def test_multiple_inheritance_cpp():
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +09006 from pybind11_tests import MIType
7
8 mt = MIType(3, 4)
9
10 assert mt.foo() == 3
11 assert mt.bar() == 4
12
13
Dean Moldovanbad17402016-11-20 21:21:54 +010014def test_multiple_inheritance_mix1():
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090015 from pybind11_tests import Base2
16
17 class Base1:
18 def __init__(self, i):
19 self.i = i
20
21 def foo(self):
22 return self.i
23
24 class MITypePy(Base1, Base2):
25 def __init__(self, i, j):
26 Base1.__init__(self, i)
27 Base2.__init__(self, j)
28
29 mt = MITypePy(3, 4)
30
31 assert mt.foo() == 3
32 assert mt.bar() == 4
33
34
Dean Moldovanbad17402016-11-20 21:21:54 +010035def test_multiple_inheritance_mix2():
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090036 from pybind11_tests import Base1
37
38 class Base2:
39 def __init__(self, i):
40 self.i = i
41
42 def bar(self):
43 return self.i
44
45 class MITypePy(Base1, Base2):
46 def __init__(self, i, j):
47 Base1.__init__(self, i)
48 Base2.__init__(self, j)
49
50 mt = MITypePy(3, 4)
51
52 assert mt.foo() == 3
53 assert mt.bar() == 4
54
55
Dean Moldovan08cbe8d2017-02-15 21:10:25 +010056def test_multiple_inheritance_error():
57 """Inheriting from multiple C++ bases in Python is not supported"""
58 from pybind11_tests import Base1, Base2
59
60 with pytest.raises(TypeError) as excinfo:
61 # noinspection PyUnusedLocal
62 class MI(Base1, Base2):
63 pass
64 assert "Can't inherit from multiple C++ classes in Python" in str(excinfo.value)
65
66
Dean Moldovanbad17402016-11-20 21:21:54 +010067def test_multiple_inheritance_virtbase():
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090068 from pybind11_tests import Base12a, bar_base2a, bar_base2a_sharedptr
69
70 class MITypePy(Base12a):
71 def __init__(self, i, j):
72 Base12a.__init__(self, i, j)
73
74 mt = MITypePy(3, 4)
75 assert mt.bar() == 4
76 assert bar_base2a(mt) == 4
77 assert bar_base2a_sharedptr(mt) == 4
Dean Moldovan08cbe8d2017-02-15 21:10:25 +010078
79
80def test_mi_static_properties():
81 """Mixing bases with and without static properties should be possible
82 and the result should be independent of base definition order"""
83 from pybind11_tests import mi
84
85 for d in (mi.VanillaStaticMix1(), mi.VanillaStaticMix2()):
86 assert d.vanilla() == "Vanilla"
87 assert d.static_func1() == "WithStatic1"
88 assert d.static_func2() == "WithStatic2"
89 assert d.static_func() == d.__class__.__name__
90
91 mi.WithStatic1.static_value1 = 1
92 mi.WithStatic2.static_value2 = 2
93 assert d.static_value1 == 1
94 assert d.static_value2 == 2
95 assert d.static_value == 12
96
97 d.static_value1 = 0
98 assert d.static_value1 == 0
99 d.static_value2 = 0
100 assert d.static_value2 == 0
101 d.static_value = 0
102 assert d.static_value == 0
103
104
105@pytest.unsupported_on_pypy
106def test_mi_dynamic_attributes():
107 """Mixing bases with and without dynamic attribute support"""
108 from pybind11_tests import mi
109
110 for d in (mi.VanillaDictMix1(), mi.VanillaDictMix2()):
111 d.dynamic = 1
112 assert d.dynamic == 1
Jason Rhinelander14e70652017-04-21 17:14:22 -0400113
114
Jason Rhinelander1f8a1002017-04-21 19:01:30 -0400115def test_mi_unaligned_base():
116 """Returning an offset (non-first MI) base class pointer should recognize the instance"""
117 from pybind11_tests import I801C, I801D, i801b1_c, i801b2_c, i801b1_d, i801b2_d
118
119 n_inst = ConstructorStats.detail_reg_inst()
120
121 c = I801C()
122 d = I801D()
123 # + 4 below because we have the two instances, and each instance has offset base I801B2
124 assert ConstructorStats.detail_reg_inst() == n_inst + 4
125 b1c = i801b1_c(c)
126 assert b1c is c
127 b2c = i801b2_c(c)
128 assert b2c is c
129 b1d = i801b1_d(d)
130 assert b1d is d
131 b2d = i801b2_d(d)
132 assert b2d is d
133
134 assert ConstructorStats.detail_reg_inst() == n_inst + 4 # no extra instances
135 del c, b1c, b2c
136 assert ConstructorStats.detail_reg_inst() == n_inst + 2
137 del d, b1d, b2d
138 assert ConstructorStats.detail_reg_inst() == n_inst
139
140
Jason Rhinelander14e70652017-04-21 17:14:22 -0400141def test_mi_base_return():
142 """Tests returning an offset (non-first MI) base class pointer to a derived instance"""
143 from pybind11_tests import (I801B2, I801C, I801D, i801c_b1, i801c_b2, i801d_b1, i801d_b2,
144 i801e_c, i801e_b2)
145
146 n_inst = ConstructorStats.detail_reg_inst()
147
148 c1 = i801c_b1()
149 assert type(c1) is I801C
150 assert c1.a == 1
151 assert c1.b == 2
152
153 d1 = i801d_b1()
154 assert type(d1) is I801D
155 assert d1.a == 1
156 assert d1.b == 2
157
Jason Rhinelander1f8a1002017-04-21 19:01:30 -0400158 assert ConstructorStats.detail_reg_inst() == n_inst + 4
Jason Rhinelander14e70652017-04-21 17:14:22 -0400159
160 c2 = i801c_b2()
161 assert type(c2) is I801C
162 assert c2.a == 1
163 assert c2.b == 2
164
165 d2 = i801d_b2()
166 assert type(d2) is I801D
167 assert d2.a == 1
168 assert d2.b == 2
169
Jason Rhinelander1f8a1002017-04-21 19:01:30 -0400170 assert ConstructorStats.detail_reg_inst() == n_inst + 8
Jason Rhinelander14e70652017-04-21 17:14:22 -0400171
172 del c2
Jason Rhinelander1f8a1002017-04-21 19:01:30 -0400173 assert ConstructorStats.detail_reg_inst() == n_inst + 6
Jason Rhinelander14e70652017-04-21 17:14:22 -0400174 del c1, d1, d2
175 assert ConstructorStats.detail_reg_inst() == n_inst
176
177 # Returning an unregistered derived type with a registered base; we won't
178 # pick up the derived type, obviously, but should still work (as an object
179 # of whatever type was returned).
180 e1 = i801e_c()
181 assert type(e1) is I801C
182 assert e1.a == 1
183 assert e1.b == 2
184
185 e2 = i801e_b2()
186 assert type(e2) is I801B2
187 assert e2.b == 2