1import pytest
2from pybind11_tests import ConstructorStats
3from pybind11_tests import multiple_inheritance as m
4
5
6def test_multiple_inheritance_cpp():
7    mt = m.MIType(3, 4)
8
9    assert mt.foo() == 3
10    assert mt.bar() == 4
11
12
13def test_multiple_inheritance_mix1():
14    class Base1:
15        def __init__(self, i):
16            self.i = i
17
18        def foo(self):
19            return self.i
20
21    class MITypePy(Base1, m.Base2):
22        def __init__(self, i, j):
23            Base1.__init__(self, i)
24            m.Base2.__init__(self, j)
25
26    mt = MITypePy(3, 4)
27
28    assert mt.foo() == 3
29    assert mt.bar() == 4
30
31
32def test_multiple_inheritance_mix2():
33
34    class Base2:
35        def __init__(self, i):
36            self.i = i
37
38        def bar(self):
39            return self.i
40
41    class MITypePy(m.Base1, Base2):
42        def __init__(self, i, j):
43            m.Base1.__init__(self, i)
44            Base2.__init__(self, j)
45
46    mt = MITypePy(3, 4)
47
48    assert mt.foo() == 3
49    assert mt.bar() == 4
50
51
52def test_multiple_inheritance_python():
53
54    class MI1(m.Base1, m.Base2):
55        def __init__(self, i, j):
56            m.Base1.__init__(self, i)
57            m.Base2.__init__(self, j)
58
59    class B1(object):
60        def v(self):
61            return 1
62
63    class MI2(B1, m.Base1, m.Base2):
64        def __init__(self, i, j):
65            B1.__init__(self)
66            m.Base1.__init__(self, i)
67            m.Base2.__init__(self, j)
68
69    class MI3(MI2):
70        def __init__(self, i, j):
71            MI2.__init__(self, i, j)
72
73    class MI4(MI3, m.Base2):
74        def __init__(self, i, j):
75            MI3.__init__(self, i, j)
76            # This should be ignored (Base2 is already initialized via MI2):
77            m.Base2.__init__(self, i + 100)
78
79    class MI5(m.Base2, B1, m.Base1):
80        def __init__(self, i, j):
81            B1.__init__(self)
82            m.Base1.__init__(self, i)
83            m.Base2.__init__(self, j)
84
85    class MI6(m.Base2, B1):
86        def __init__(self, i):
87            m.Base2.__init__(self, i)
88            B1.__init__(self)
89
90    class B2(B1):
91        def v(self):
92            return 2
93
94    class B3(object):
95        def v(self):
96            return 3
97
98    class B4(B3, B2):
99        def v(self):
100            return 4
101
102    class MI7(B4, MI6):
103        def __init__(self, i):
104            B4.__init__(self)
105            MI6.__init__(self, i)
106
107    class MI8(MI6, B3):
108        def __init__(self, i):
109            MI6.__init__(self, i)
110            B3.__init__(self)
111
112    class MI8b(B3, MI6):
113        def __init__(self, i):
114            B3.__init__(self)
115            MI6.__init__(self, i)
116
117    mi1 = MI1(1, 2)
118    assert mi1.foo() == 1
119    assert mi1.bar() == 2
120
121    mi2 = MI2(3, 4)
122    assert mi2.v() == 1
123    assert mi2.foo() == 3
124    assert mi2.bar() == 4
125
126    mi3 = MI3(5, 6)
127    assert mi3.v() == 1
128    assert mi3.foo() == 5
129    assert mi3.bar() == 6
130
131    mi4 = MI4(7, 8)
132    assert mi4.v() == 1
133    assert mi4.foo() == 7
134    assert mi4.bar() == 8
135
136    mi5 = MI5(10, 11)
137    assert mi5.v() == 1
138    assert mi5.foo() == 10
139    assert mi5.bar() == 11
140
141    mi6 = MI6(12)
142    assert mi6.v() == 1
143    assert mi6.bar() == 12
144
145    mi7 = MI7(13)
146    assert mi7.v() == 4
147    assert mi7.bar() == 13
148
149    mi8 = MI8(14)
150    assert mi8.v() == 1
151    assert mi8.bar() == 14
152
153    mi8b = MI8b(15)
154    assert mi8b.v() == 3
155    assert mi8b.bar() == 15
156
157
158def test_multiple_inheritance_python_many_bases():
159
160    class MIMany14(m.BaseN1, m.BaseN2, m.BaseN3, m.BaseN4):
161        def __init__(self):
162            m.BaseN1.__init__(self, 1)
163            m.BaseN2.__init__(self, 2)
164            m.BaseN3.__init__(self, 3)
165            m.BaseN4.__init__(self, 4)
166
167    class MIMany58(m.BaseN5, m.BaseN6, m.BaseN7, m.BaseN8):
168        def __init__(self):
169            m.BaseN5.__init__(self, 5)
170            m.BaseN6.__init__(self, 6)
171            m.BaseN7.__init__(self, 7)
172            m.BaseN8.__init__(self, 8)
173
174    class MIMany916(m.BaseN9, m.BaseN10, m.BaseN11, m.BaseN12, m.BaseN13, m.BaseN14, m.BaseN15,
175                    m.BaseN16):
176        def __init__(self):
177            m.BaseN9.__init__(self, 9)
178            m.BaseN10.__init__(self, 10)
179            m.BaseN11.__init__(self, 11)
180            m.BaseN12.__init__(self, 12)
181            m.BaseN13.__init__(self, 13)
182            m.BaseN14.__init__(self, 14)
183            m.BaseN15.__init__(self, 15)
184            m.BaseN16.__init__(self, 16)
185
186    class MIMany19(MIMany14, MIMany58, m.BaseN9):
187        def __init__(self):
188            MIMany14.__init__(self)
189            MIMany58.__init__(self)
190            m.BaseN9.__init__(self, 9)
191
192    class MIMany117(MIMany14, MIMany58, MIMany916, m.BaseN17):
193        def __init__(self):
194            MIMany14.__init__(self)
195            MIMany58.__init__(self)
196            MIMany916.__init__(self)
197            m.BaseN17.__init__(self, 17)
198
199    # Inherits from 4 registered C++ classes: can fit in one pointer on any modern arch:
200    a = MIMany14()
201    for i in range(1, 4):
202        assert getattr(a, "f" + str(i))() == 2 * i
203
204    # Inherits from 8: requires 1/2 pointers worth of holder flags on 32/64-bit arch:
205    b = MIMany916()
206    for i in range(9, 16):
207        assert getattr(b, "f" + str(i))() == 2 * i
208
209    # Inherits from 9: requires >= 2 pointers worth of holder flags
210    c = MIMany19()
211    for i in range(1, 9):
212        assert getattr(c, "f" + str(i))() == 2 * i
213
214    # Inherits from 17: requires >= 3 pointers worth of holder flags
215    d = MIMany117()
216    for i in range(1, 17):
217        assert getattr(d, "f" + str(i))() == 2 * i
218
219
220def test_multiple_inheritance_virtbase():
221
222    class MITypePy(m.Base12a):
223        def __init__(self, i, j):
224            m.Base12a.__init__(self, i, j)
225
226    mt = MITypePy(3, 4)
227    assert mt.bar() == 4
228    assert m.bar_base2a(mt) == 4
229    assert m.bar_base2a_sharedptr(mt) == 4
230
231
232def test_mi_static_properties():
233    """Mixing bases with and without static properties should be possible
234     and the result should be independent of base definition order"""
235
236    for d in (m.VanillaStaticMix1(), m.VanillaStaticMix2()):
237        assert d.vanilla() == "Vanilla"
238        assert d.static_func1() == "WithStatic1"
239        assert d.static_func2() == "WithStatic2"
240        assert d.static_func() == d.__class__.__name__
241
242        m.WithStatic1.static_value1 = 1
243        m.WithStatic2.static_value2 = 2
244        assert d.static_value1 == 1
245        assert d.static_value2 == 2
246        assert d.static_value == 12
247
248        d.static_value1 = 0
249        assert d.static_value1 == 0
250        d.static_value2 = 0
251        assert d.static_value2 == 0
252        d.static_value = 0
253        assert d.static_value == 0
254
255
256@pytest.unsupported_on_pypy
257def test_mi_dynamic_attributes():
258    """Mixing bases with and without dynamic attribute support"""
259
260    for d in (m.VanillaDictMix1(), m.VanillaDictMix2()):
261        d.dynamic = 1
262        assert d.dynamic == 1
263
264
265def test_mi_unaligned_base():
266    """Returning an offset (non-first MI) base class pointer should recognize the instance"""
267
268    n_inst = ConstructorStats.detail_reg_inst()
269
270    c = m.I801C()
271    d = m.I801D()
272    # + 4 below because we have the two instances, and each instance has offset base I801B2
273    assert ConstructorStats.detail_reg_inst() == n_inst + 4
274    b1c = m.i801b1_c(c)
275    assert b1c is c
276    b2c = m.i801b2_c(c)
277    assert b2c is c
278    b1d = m.i801b1_d(d)
279    assert b1d is d
280    b2d = m.i801b2_d(d)
281    assert b2d is d
282
283    assert ConstructorStats.detail_reg_inst() == n_inst + 4  # no extra instances
284    del c, b1c, b2c
285    assert ConstructorStats.detail_reg_inst() == n_inst + 2
286    del d, b1d, b2d
287    assert ConstructorStats.detail_reg_inst() == n_inst
288
289
290def test_mi_base_return():
291    """Tests returning an offset (non-first MI) base class pointer to a derived instance"""
292
293    n_inst = ConstructorStats.detail_reg_inst()
294
295    c1 = m.i801c_b1()
296    assert type(c1) is m.I801C
297    assert c1.a == 1
298    assert c1.b == 2
299
300    d1 = m.i801d_b1()
301    assert type(d1) is m.I801D
302    assert d1.a == 1
303    assert d1.b == 2
304
305    assert ConstructorStats.detail_reg_inst() == n_inst + 4
306
307    c2 = m.i801c_b2()
308    assert type(c2) is m.I801C
309    assert c2.a == 1
310    assert c2.b == 2
311
312    d2 = m.i801d_b2()
313    assert type(d2) is m.I801D
314    assert d2.a == 1
315    assert d2.b == 2
316
317    assert ConstructorStats.detail_reg_inst() == n_inst + 8
318
319    del c2
320    assert ConstructorStats.detail_reg_inst() == n_inst + 6
321    del c1, d1, d2
322    assert ConstructorStats.detail_reg_inst() == n_inst
323
324    # Returning an unregistered derived type with a registered base; we won't
325    # pick up the derived type, obviously, but should still work (as an object
326    # of whatever type was returned).
327    e1 = m.i801e_c()
328    assert type(e1) is m.I801C
329    assert e1.a == 1
330    assert e1.b == 2
331
332    e2 = m.i801e_b2()
333    assert type(e2) is m.I801B2
334    assert e2.b == 2
335
336
337def test_diamond_inheritance():
338    """Tests that diamond inheritance works as expected (issue #959)"""
339
340    # Issue #959: this shouldn't segfault:
341    d = m.D()
342
343    # Make sure all the various distinct pointers are all recognized as registered instances:
344    assert d is d.c0()
345    assert d is d.c1()
346    assert d is d.b()
347    assert d is d.c0().b()
348    assert d is d.c1().b()
349    assert d is d.c0().c1().b().c0().b()
350