test_multiple_inheritance.py 9.23 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
import pytest
3
4
5

import env  # noqa: F401

6
from pybind11_tests import ConstructorStats
7
from pybind11_tests import multiple_inheritance as m
8
9


10
def test_multiple_inheritance_cpp():
11
    mt = m.MIType(3, 4)
Wenzel Jakob's avatar
Wenzel Jakob committed
12
13
14
15
16

    assert mt.foo() == 3
    assert mt.bar() == 4


17
18
@pytest.mark.skipif("env.PYPY and env.PY2")
@pytest.mark.xfail("env.PYPY and not env.PY2")
19
def test_multiple_inheritance_mix1():
Wenzel Jakob's avatar
Wenzel Jakob committed
20
21
22
23
24
25
26
    class Base1:
        def __init__(self, i):
            self.i = i

        def foo(self):
            return self.i

27
    class MITypePy(Base1, m.Base2):
Wenzel Jakob's avatar
Wenzel Jakob committed
28
29
        def __init__(self, i, j):
            Base1.__init__(self, i)
30
            m.Base2.__init__(self, j)
Wenzel Jakob's avatar
Wenzel Jakob committed
31
32
33
34
35
36
37

    mt = MITypePy(3, 4)

    assert mt.foo() == 3
    assert mt.bar() == 4


38
def test_multiple_inheritance_mix2():
Wenzel Jakob's avatar
Wenzel Jakob committed
39
40
41
42
43
44
45
    class Base2:
        def __init__(self, i):
            self.i = i

        def bar(self):
            return self.i

46
    class MITypePy(m.Base1, Base2):
Wenzel Jakob's avatar
Wenzel Jakob committed
47
        def __init__(self, i, j):
48
            m.Base1.__init__(self, i)
Wenzel Jakob's avatar
Wenzel Jakob committed
49
50
51
52
53
54
55
56
            Base2.__init__(self, j)

    mt = MITypePy(3, 4)

    assert mt.foo() == 3
    assert mt.bar() == 4


57
58
@pytest.mark.skipif("env.PYPY and env.PY2")
@pytest.mark.xfail("env.PYPY and not env.PY2")
59
def test_multiple_inheritance_python():
60

61
    class MI1(m.Base1, m.Base2):
62
        def __init__(self, i, j):
63
64
            m.Base1.__init__(self, i)
            m.Base2.__init__(self, j)
65
66
67
68
69

    class B1(object):
        def v(self):
            return 1

70
    class MI2(B1, m.Base1, m.Base2):
71
72
        def __init__(self, i, j):
            B1.__init__(self)
73
74
            m.Base1.__init__(self, i)
            m.Base2.__init__(self, j)
75
76
77
78
79

    class MI3(MI2):
        def __init__(self, i, j):
            MI2.__init__(self, i, j)

80
    class MI4(MI3, m.Base2):
81
82
        def __init__(self, i, j):
            MI3.__init__(self, i, j)
83
84
            # This should be ignored (Base2 is already initialized via MI2):
            m.Base2.__init__(self, i + 100)
85

86
    class MI5(m.Base2, B1, m.Base1):
87
88
        def __init__(self, i, j):
            B1.__init__(self)
89
90
            m.Base1.__init__(self, i)
            m.Base2.__init__(self, j)
91

92
    class MI6(m.Base2, B1):
93
        def __init__(self, i):
94
            m.Base2.__init__(self, i)
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
            B1.__init__(self)

    class B2(B1):
        def v(self):
            return 2

    class B3(object):
        def v(self):
            return 3

    class B4(B3, B2):
        def v(self):
            return 4

    class MI7(B4, MI6):
        def __init__(self, i):
            B4.__init__(self)
            MI6.__init__(self, i)

    class MI8(MI6, B3):
        def __init__(self, i):
            MI6.__init__(self, i)
            B3.__init__(self)

    class MI8b(B3, MI6):
        def __init__(self, i):
            B3.__init__(self)
            MI6.__init__(self, i)

    mi1 = MI1(1, 2)
    assert mi1.foo() == 1
    assert mi1.bar() == 2

    mi2 = MI2(3, 4)
    assert mi2.v() == 1
    assert mi2.foo() == 3
    assert mi2.bar() == 4

    mi3 = MI3(5, 6)
    assert mi3.v() == 1
    assert mi3.foo() == 5
    assert mi3.bar() == 6

138
    mi4 = MI4(7, 8)
139
    assert mi4.v() == 1
140
141
    assert mi4.foo() == 7
    assert mi4.bar() == 8
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

    mi5 = MI5(10, 11)
    assert mi5.v() == 1
    assert mi5.foo() == 10
    assert mi5.bar() == 11

    mi6 = MI6(12)
    assert mi6.v() == 1
    assert mi6.bar() == 12

    mi7 = MI7(13)
    assert mi7.v() == 4
    assert mi7.bar() == 13

    mi8 = MI8(14)
    assert mi8.v() == 1
    assert mi8.bar() == 14

    mi8b = MI8b(15)
    assert mi8b.v() == 3
    assert mi8b.bar() == 15


def test_multiple_inheritance_python_many_bases():

167
    class MIMany14(m.BaseN1, m.BaseN2, m.BaseN3, m.BaseN4):
168
        def __init__(self):
169
170
171
172
            m.BaseN1.__init__(self, 1)
            m.BaseN2.__init__(self, 2)
            m.BaseN3.__init__(self, 3)
            m.BaseN4.__init__(self, 4)
173

174
    class MIMany58(m.BaseN5, m.BaseN6, m.BaseN7, m.BaseN8):
175
        def __init__(self):
176
177
178
179
            m.BaseN5.__init__(self, 5)
            m.BaseN6.__init__(self, 6)
            m.BaseN7.__init__(self, 7)
            m.BaseN8.__init__(self, 8)
180

181
182
    class MIMany916(m.BaseN9, m.BaseN10, m.BaseN11, m.BaseN12, m.BaseN13, m.BaseN14, m.BaseN15,
                    m.BaseN16):
183
        def __init__(self):
184
185
186
187
188
189
190
191
192
193
            m.BaseN9.__init__(self, 9)
            m.BaseN10.__init__(self, 10)
            m.BaseN11.__init__(self, 11)
            m.BaseN12.__init__(self, 12)
            m.BaseN13.__init__(self, 13)
            m.BaseN14.__init__(self, 14)
            m.BaseN15.__init__(self, 15)
            m.BaseN16.__init__(self, 16)

    class MIMany19(MIMany14, MIMany58, m.BaseN9):
194
195
196
        def __init__(self):
            MIMany14.__init__(self)
            MIMany58.__init__(self)
197
            m.BaseN9.__init__(self, 9)
198

199
    class MIMany117(MIMany14, MIMany58, MIMany916, m.BaseN17):
200
201
202
203
        def __init__(self):
            MIMany14.__init__(self)
            MIMany58.__init__(self)
            MIMany916.__init__(self)
204
            m.BaseN17.__init__(self, 17)
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

    # Inherits from 4 registered C++ classes: can fit in one pointer on any modern arch:
    a = MIMany14()
    for i in range(1, 4):
        assert getattr(a, "f" + str(i))() == 2 * i

    # Inherits from 8: requires 1/2 pointers worth of holder flags on 32/64-bit arch:
    b = MIMany916()
    for i in range(9, 16):
        assert getattr(b, "f" + str(i))() == 2 * i

    # Inherits from 9: requires >= 2 pointers worth of holder flags
    c = MIMany19()
    for i in range(1, 9):
        assert getattr(c, "f" + str(i))() == 2 * i

    # Inherits from 17: requires >= 3 pointers worth of holder flags
    d = MIMany117()
    for i in range(1, 17):
        assert getattr(d, "f" + str(i))() == 2 * i
225
226


227
def test_multiple_inheritance_virtbase():
Wenzel Jakob's avatar
Wenzel Jakob committed
228

229
    class MITypePy(m.Base12a):
Wenzel Jakob's avatar
Wenzel Jakob committed
230
        def __init__(self, i, j):
231
            m.Base12a.__init__(self, i, j)
Wenzel Jakob's avatar
Wenzel Jakob committed
232
233
234

    mt = MITypePy(3, 4)
    assert mt.bar() == 4
235
236
    assert m.bar_base2a(mt) == 4
    assert m.bar_base2a_sharedptr(mt) == 4
237
238
239
240
241
242


def test_mi_static_properties():
    """Mixing bases with and without static properties should be possible
     and the result should be independent of base definition order"""

243
    for d in (m.VanillaStaticMix1(), m.VanillaStaticMix2()):
244
245
246
247
248
        assert d.vanilla() == "Vanilla"
        assert d.static_func1() == "WithStatic1"
        assert d.static_func2() == "WithStatic2"
        assert d.static_func() == d.__class__.__name__

249
250
        m.WithStatic1.static_value1 = 1
        m.WithStatic2.static_value2 = 2
251
252
253
254
255
256
257
258
259
260
261
262
        assert d.static_value1 == 1
        assert d.static_value2 == 2
        assert d.static_value == 12

        d.static_value1 = 0
        assert d.static_value1 == 0
        d.static_value2 = 0
        assert d.static_value2 == 0
        d.static_value = 0
        assert d.static_value == 0


263
# Requires PyPy 6+
264
265
266
def test_mi_dynamic_attributes():
    """Mixing bases with and without dynamic attribute support"""

267
    for d in (m.VanillaDictMix1(), m.VanillaDictMix2()):
268
269
        d.dynamic = 1
        assert d.dynamic == 1
270
271


272
273
274
275
276
def test_mi_unaligned_base():
    """Returning an offset (non-first MI) base class pointer should recognize the instance"""

    n_inst = ConstructorStats.detail_reg_inst()

277
278
    c = m.I801C()
    d = m.I801D()
279
280
    # + 4 below because we have the two instances, and each instance has offset base I801B2
    assert ConstructorStats.detail_reg_inst() == n_inst + 4
281
    b1c = m.i801b1_c(c)
282
    assert b1c is c
283
    b2c = m.i801b2_c(c)
284
    assert b2c is c
285
    b1d = m.i801b1_d(d)
286
    assert b1d is d
287
    b2d = m.i801b2_d(d)
288
289
290
291
292
293
294
295
296
    assert b2d is d

    assert ConstructorStats.detail_reg_inst() == n_inst + 4  # no extra instances
    del c, b1c, b2c
    assert ConstructorStats.detail_reg_inst() == n_inst + 2
    del d, b1d, b2d
    assert ConstructorStats.detail_reg_inst() == n_inst


297
298
299
300
301
def test_mi_base_return():
    """Tests returning an offset (non-first MI) base class pointer to a derived instance"""

    n_inst = ConstructorStats.detail_reg_inst()

302
303
    c1 = m.i801c_b1()
    assert type(c1) is m.I801C
304
305
306
    assert c1.a == 1
    assert c1.b == 2

307
308
    d1 = m.i801d_b1()
    assert type(d1) is m.I801D
309
310
311
    assert d1.a == 1
    assert d1.b == 2

312
    assert ConstructorStats.detail_reg_inst() == n_inst + 4
313

314
315
    c2 = m.i801c_b2()
    assert type(c2) is m.I801C
316
317
318
    assert c2.a == 1
    assert c2.b == 2

319
320
    d2 = m.i801d_b2()
    assert type(d2) is m.I801D
321
322
323
    assert d2.a == 1
    assert d2.b == 2

324
    assert ConstructorStats.detail_reg_inst() == n_inst + 8
325
326

    del c2
327
    assert ConstructorStats.detail_reg_inst() == n_inst + 6
328
329
330
331
332
333
    del c1, d1, d2
    assert ConstructorStats.detail_reg_inst() == n_inst

    # Returning an unregistered derived type with a registered base; we won't
    # pick up the derived type, obviously, but should still work (as an object
    # of whatever type was returned).
334
335
    e1 = m.i801e_c()
    assert type(e1) is m.I801C
336
337
338
    assert e1.a == 1
    assert e1.b == 2

339
340
    e2 = m.i801e_b2()
    assert type(e2) is m.I801B2
341
    assert e2.b == 2
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356


def test_diamond_inheritance():
    """Tests that diamond inheritance works as expected (issue #959)"""

    # Issue #959: this shouldn't segfault:
    d = m.D()

    # Make sure all the various distinct pointers are all recognized as registered instances:
    assert d is d.c0()
    assert d is d.c1()
    assert d is d.b()
    assert d is d.c0().b()
    assert d is d.c1().b()
    assert d is d.c0().c1().b().c0().b()