test_multiple_inheritance.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 import pytest
4 
5 import env # noqa: F401
6 from pybind11_tests import ConstructorStats
7 from pybind11_tests import multiple_inheritance as m
8 
9 
11  mt = m.MIType(3, 4)
12 
13  assert mt.foo() == 3
14  assert mt.bar() == 4
15 
16 
17 @pytest.mark.xfail("env.PYPY")
19  class Base1:
20  def __init__(self, i):
21  self.i = i
22 
23  def foo(self):
24  return self.i
25 
26  class MITypePy(Base1, m.Base2):
27  def __init__(self, i, j):
28  Base1.__init__(self, i)
29  m.Base2.__init__(self, j)
30 
31  mt = MITypePy(3, 4)
32 
33  assert mt.foo() == 3
34  assert mt.bar() == 4
35 
36 
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(m.Base1, Base2):
46  def __init__(self, i, j):
47  m.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 
56 @pytest.mark.xfail("env.PYPY")
58  class MI1(m.Base1, m.Base2):
59  def __init__(self, i, j):
60  m.Base1.__init__(self, i)
61  m.Base2.__init__(self, j)
62 
63  class B1:
64  def v(self):
65  return 1
66 
67  class MI2(B1, m.Base1, m.Base2):
68  def __init__(self, i, j):
69  B1.__init__(self)
70  m.Base1.__init__(self, i)
71  m.Base2.__init__(self, j)
72 
73  class MI3(MI2):
74  def __init__(self, i, j):
75  MI2.__init__(self, i, j)
76 
77  class MI4(MI3, m.Base2):
78  def __init__(self, i, j):
79  MI3.__init__(self, i, j)
80  # This should be ignored (Base2 is already initialized via MI2):
81  m.Base2.__init__(self, i + 100)
82 
83  class MI5(m.Base2, B1, m.Base1):
84  def __init__(self, i, j):
85  B1.__init__(self)
86  m.Base1.__init__(self, i)
87  m.Base2.__init__(self, j)
88 
89  class MI6(m.Base2, B1):
90  def __init__(self, i):
91  m.Base2.__init__(self, i)
92  B1.__init__(self)
93 
94  class B2(B1):
95  def v(self):
96  return 2
97 
98  class B3:
99  def v(self):
100  return 3
101 
102  class B4(B3, B2):
103  def v(self):
104  return 4
105 
106  class MI7(B4, MI6):
107  def __init__(self, i):
108  B4.__init__(self)
109  MI6.__init__(self, i)
110 
111  class MI8(MI6, B3):
112  def __init__(self, i):
113  MI6.__init__(self, i)
114  B3.__init__(self)
115 
116  class MI8b(B3, MI6):
117  def __init__(self, i):
118  B3.__init__(self)
119  MI6.__init__(self, i)
120 
121  mi1 = MI1(1, 2)
122  assert mi1.foo() == 1
123  assert mi1.bar() == 2
124 
125  mi2 = MI2(3, 4)
126  assert mi2.v() == 1
127  assert mi2.foo() == 3
128  assert mi2.bar() == 4
129 
130  mi3 = MI3(5, 6)
131  assert mi3.v() == 1
132  assert mi3.foo() == 5
133  assert mi3.bar() == 6
134 
135  mi4 = MI4(7, 8)
136  assert mi4.v() == 1
137  assert mi4.foo() == 7
138  assert mi4.bar() == 8
139 
140  mi5 = MI5(10, 11)
141  assert mi5.v() == 1
142  assert mi5.foo() == 10
143  assert mi5.bar() == 11
144 
145  mi6 = MI6(12)
146  assert mi6.v() == 1
147  assert mi6.bar() == 12
148 
149  mi7 = MI7(13)
150  assert mi7.v() == 4
151  assert mi7.bar() == 13
152 
153  mi8 = MI8(14)
154  assert mi8.v() == 1
155  assert mi8.bar() == 14
156 
157  mi8b = MI8b(15)
158  assert mi8b.v() == 3
159  assert mi8b.bar() == 15
160 
161 
163  class MIMany14(m.BaseN1, m.BaseN2, m.BaseN3, m.BaseN4):
164  def __init__(self):
165  m.BaseN1.__init__(self, 1)
166  m.BaseN2.__init__(self, 2)
167  m.BaseN3.__init__(self, 3)
168  m.BaseN4.__init__(self, 4)
169 
170  class MIMany58(m.BaseN5, m.BaseN6, m.BaseN7, m.BaseN8):
171  def __init__(self):
172  m.BaseN5.__init__(self, 5)
173  m.BaseN6.__init__(self, 6)
174  m.BaseN7.__init__(self, 7)
175  m.BaseN8.__init__(self, 8)
176 
177  class MIMany916(
178  m.BaseN9,
179  m.BaseN10,
180  m.BaseN11,
181  m.BaseN12,
182  m.BaseN13,
183  m.BaseN14,
184  m.BaseN15,
185  m.BaseN16,
186  ):
187  def __init__(self):
188  m.BaseN9.__init__(self, 9)
189  m.BaseN10.__init__(self, 10)
190  m.BaseN11.__init__(self, 11)
191  m.BaseN12.__init__(self, 12)
192  m.BaseN13.__init__(self, 13)
193  m.BaseN14.__init__(self, 14)
194  m.BaseN15.__init__(self, 15)
195  m.BaseN16.__init__(self, 16)
196 
197  class MIMany19(MIMany14, MIMany58, m.BaseN9):
198  def __init__(self):
199  MIMany14.__init__(self)
200  MIMany58.__init__(self)
201  m.BaseN9.__init__(self, 9)
202 
203  class MIMany117(MIMany14, MIMany58, MIMany916, m.BaseN17):
204  def __init__(self):
205  MIMany14.__init__(self)
206  MIMany58.__init__(self)
207  MIMany916.__init__(self)
208  m.BaseN17.__init__(self, 17)
209 
210  # Inherits from 4 registered C++ classes: can fit in one pointer on any modern arch:
211  a = MIMany14()
212  for i in range(1, 4):
213  assert getattr(a, "f" + str(i))() == 2 * i
214 
215  # Inherits from 8: requires 1/2 pointers worth of holder flags on 32/64-bit arch:
216  b = MIMany916()
217  for i in range(9, 16):
218  assert getattr(b, "f" + str(i))() == 2 * i
219 
220  # Inherits from 9: requires >= 2 pointers worth of holder flags
221  c = MIMany19()
222  for i in range(1, 9):
223  assert getattr(c, "f" + str(i))() == 2 * i
224 
225  # Inherits from 17: requires >= 3 pointers worth of holder flags
226  d = MIMany117()
227  for i in range(1, 17):
228  assert getattr(d, "f" + str(i))() == 2 * i
229 
230 
232  class MITypePy(m.Base12a):
233  def __init__(self, i, j):
234  m.Base12a.__init__(self, i, j)
235 
236  mt = MITypePy(3, 4)
237  assert mt.bar() == 4
238  assert m.bar_base2a(mt) == 4
239  assert m.bar_base2a_sharedptr(mt) == 4
240 
241 
243  """Mixing bases with and without static properties should be possible
244  and the result should be independent of base definition order"""
245 
246  for d in (m.VanillaStaticMix1(), m.VanillaStaticMix2()):
247  assert d.vanilla() == "Vanilla"
248  assert d.static_func1() == "WithStatic1"
249  assert d.static_func2() == "WithStatic2"
250  assert d.static_func() == d.__class__.__name__
251 
252  m.WithStatic1.static_value1 = 1
253  m.WithStatic2.static_value2 = 2
254  assert d.static_value1 == 1
255  assert d.static_value2 == 2
256  assert d.static_value == 12
257 
258  d.static_value1 = 0
259  assert d.static_value1 == 0
260  d.static_value2 = 0
261  assert d.static_value2 == 0
262  d.static_value = 0
263  assert d.static_value == 0
264 
265 
266 # Requires PyPy 6+
268  """Mixing bases with and without dynamic attribute support"""
269 
270  for d in (m.VanillaDictMix1(), m.VanillaDictMix2()):
271  d.dynamic = 1
272  assert d.dynamic == 1
273 
274 
276  """Returning an offset (non-first MI) base class pointer should recognize the instance"""
277 
278  n_inst = ConstructorStats.detail_reg_inst()
279 
280  c = m.I801C()
281  d = m.I801D()
282  # + 4 below because we have the two instances, and each instance has offset base I801B2
283  assert ConstructorStats.detail_reg_inst() == n_inst + 4
284  b1c = m.i801b1_c(c)
285  assert b1c is c
286  b2c = m.i801b2_c(c)
287  assert b2c is c
288  b1d = m.i801b1_d(d)
289  assert b1d is d
290  b2d = m.i801b2_d(d)
291  assert b2d is d
292 
293  assert ConstructorStats.detail_reg_inst() == n_inst + 4 # no extra instances
294  del c, b1c, b2c
295  assert ConstructorStats.detail_reg_inst() == n_inst + 2
296  del d, b1d, b2d
297  assert ConstructorStats.detail_reg_inst() == n_inst
298 
299 
301  """Tests returning an offset (non-first MI) base class pointer to a derived instance"""
302 
303  n_inst = ConstructorStats.detail_reg_inst()
304 
305  c1 = m.i801c_b1()
306  assert type(c1) is m.I801C
307  assert c1.a == 1
308  assert c1.b == 2
309 
310  d1 = m.i801d_b1()
311  assert type(d1) is m.I801D
312  assert d1.a == 1
313  assert d1.b == 2
314 
315  assert ConstructorStats.detail_reg_inst() == n_inst + 4
316 
317  c2 = m.i801c_b2()
318  assert type(c2) is m.I801C
319  assert c2.a == 1
320  assert c2.b == 2
321 
322  d2 = m.i801d_b2()
323  assert type(d2) is m.I801D
324  assert d2.a == 1
325  assert d2.b == 2
326 
327  assert ConstructorStats.detail_reg_inst() == n_inst + 8
328 
329  del c2
330  assert ConstructorStats.detail_reg_inst() == n_inst + 6
331  del c1, d1, d2
332  assert ConstructorStats.detail_reg_inst() == n_inst
333 
334  # Returning an unregistered derived type with a registered base; we won't
335  # pick up the derived type, obviously, but should still work (as an object
336  # of whatever type was returned).
337  e1 = m.i801e_c()
338  assert type(e1) is m.I801C
339  assert e1.a == 1
340  assert e1.b == 2
341 
342  e2 = m.i801e_b2()
343  assert type(e2) is m.I801B2
344  assert e2.b == 2
345 
346 
348  """Tests that diamond inheritance works as expected (issue #959)"""
349 
350  # Issue #959: this shouldn't segfault:
351  d = m.D()
352 
353  # Make sure all the various distinct pointers are all recognized as registered instances:
354  assert d is d.c0()
355  assert d is d.c1()
356  assert d is d.b()
357  assert d is d.c0().b()
358  assert d is d.c1().b()
359  assert d is d.c0().c1().b().c0().b()
360 
361 
363  o = m.MVB()
364  assert o.b == 1
365 
366  assert o.get_b_b() == 1
367 
368 
370  o = m.MVC()
371  assert o.b == 1
372  assert o.c == 2
373 
374  assert o.get_b_b() == 1
375  assert o.get_c_b() == 1
376 
377  assert o.get_c_c() == 2
378 
379 
381  o = m.MVD0()
382  assert o.b == 1
383  assert o.c == 2
384  assert o.d0 == 3
385 
386  assert o.get_b_b() == 1
387  assert o.get_c_b() == 1
388  assert o.get_d0_b() == 1
389 
390  assert o.get_c_c() == 2
391  assert o.get_d0_c() == 2
392 
393  assert o.get_d0_d0() == 3
394 
395 
397  o = m.MVD1()
398  assert o.b == 1
399  assert o.c == 2
400  assert o.d1 == 4
401 
402  assert o.get_b_b() == 1
403  assert o.get_c_b() == 1
404  assert o.get_d1_b() == 1
405 
406  assert o.get_c_c() == 2
407  assert o.get_d1_c() == 2
408 
409  assert o.get_d1_d1() == 4
410 
411 
413  o = m.MVE()
414  assert o.b == 1
415  assert o.c == 2
416  assert o.d0 == 3
417  assert o.d1 == 4
418  assert o.e == 5
419 
420  assert o.get_b_b() == 1
421  assert o.get_c_b() == 1
422  assert o.get_d0_b() == 1
423  assert o.get_d1_b() == 1
424  assert o.get_e_b() == 1
425 
426  assert o.get_c_c() == 2
427  assert o.get_d0_c() == 2
428  assert o.get_d1_c() == 2
429  assert o.get_e_c() == 2
430 
431  assert o.get_d0_d0() == 3
432  assert o.get_e_d0() == 3
433 
434  assert o.get_d1_d1() == 4
435  assert o.get_e_d1() == 4
436 
437  assert o.get_e_e() == 5
438 
439 
441  o = m.MVF()
442  assert o.b == 1
443  assert o.c == 2
444  assert o.d0 == 3
445  assert o.d1 == 4
446  assert o.e == 5
447  assert o.f == 6
448 
449  assert o.get_b_b() == 1
450  assert o.get_c_b() == 1
451  assert o.get_d0_b() == 1
452  assert o.get_d1_b() == 1
453  assert o.get_e_b() == 1
454  assert o.get_f_b() == 1
455 
456  assert o.get_c_c() == 2
457  assert o.get_d0_c() == 2
458  assert o.get_d1_c() == 2
459  assert o.get_e_c() == 2
460  assert o.get_f_c() == 2
461 
462  assert o.get_d0_d0() == 3
463  assert o.get_e_d0() == 3
464  assert o.get_f_d0() == 3
465 
466  assert o.get_d1_d1() == 4
467  assert o.get_e_d1() == 4
468  assert o.get_f_d1() == 4
469 
470  assert o.get_e_e() == 5
471  assert o.get_f_e() == 5
472 
473  assert o.get_f_f() == 6
474 
475 
477  """Tests extending a Python class from a single inheritor of a MI class"""
478 
479  class PyMVF(m.MVF):
480  g = 7
481 
482  def get_g_g(self):
483  return self.g
484 
485  o = PyMVF()
486 
487  assert o.b == 1
488  assert o.c == 2
489  assert o.d0 == 3
490  assert o.d1 == 4
491  assert o.e == 5
492  assert o.f == 6
493  assert o.g == 7
494 
495  assert o.get_g_g() == 7
test_multiple_inheritance.test_mi_dynamic_attributes
def test_mi_dynamic_attributes()
Definition: test_multiple_inheritance.py:267
test_multiple_inheritance.test_python_inherit_from_mi
def test_python_inherit_from_mi()
Definition: test_multiple_inheritance.py:476
b
Scalar * b
Definition: benchVecAdd.cpp:17
test_multiple_inheritance.test_pr3635_diamond_e
def test_pr3635_diamond_e()
Definition: test_multiple_inheritance.py:412
type
Definition: pytypes.h:1525
getattr
object getattr(handle obj, handle name)
Definition: pytypes.h:890
test_multiple_inheritance.test_diamond_inheritance
def test_diamond_inheritance()
Definition: test_multiple_inheritance.py:347
test_multiple_inheritance.test_multiple_inheritance_python_many_bases
def test_multiple_inheritance_python_many_bases()
Definition: test_multiple_inheritance.py:162
test_multiple_inheritance.test_pr3635_diamond_f
def test_pr3635_diamond_f()
Definition: test_multiple_inheritance.py:440
c1
static double c1
Definition: airy.c:54
gtsam::range
Double_ range(const Point2_ &p, const Point2_ &q)
Definition: slam/expressions.h:30
test_multiple_inheritance.test_multiple_inheritance_virtbase
def test_multiple_inheritance_virtbase()
Definition: test_multiple_inheritance.py:231
test_multiple_inheritance.test_multiple_inheritance_python
def test_multiple_inheritance_python()
Definition: test_multiple_inheritance.py:57
test_multiple_inheritance.test_mi_base_return
def test_mi_base_return()
Definition: test_multiple_inheritance.py:300
test_multiple_inheritance.test_mi_static_properties
def test_mi_static_properties()
Definition: test_multiple_inheritance.py:242
test_multiple_inheritance.test_pr3635_diamond_d1
def test_pr3635_diamond_d1()
Definition: test_multiple_inheritance.py:396
test_multiple_inheritance.test_multiple_inheritance_cpp
def test_multiple_inheritance_cpp()
Definition: test_multiple_inheritance.py:10
foo
void foo(CV_QUALIFIER Matrix3d &m)
Definition: block_nonconst_ctor_on_const_xpr_0.cpp:11
test_multiple_inheritance.test_pr3635_diamond_b
def test_pr3635_diamond_b()
Definition: test_multiple_inheritance.py:362
test_multiple_inheritance.test_multiple_inheritance_mix1
def test_multiple_inheritance_mix1()
Definition: test_multiple_inheritance.py:18
gtwrap.interface_parser.function.__init__
def __init__(self, Union[Type, TemplatedType] ctype, str name, ParseResults default=None)
Definition: interface_parser/function.py:41
str
Definition: pytypes.h:1558
test_multiple_inheritance.test_mi_unaligned_base
def test_mi_unaligned_base()
Definition: test_multiple_inheritance.py:275
test_multiple_inheritance.test_multiple_inheritance_mix2
def test_multiple_inheritance_mix2()
Definition: test_multiple_inheritance.py:37
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
test_multiple_inheritance.test_pr3635_diamond_d0
def test_pr3635_diamond_d0()
Definition: test_multiple_inheritance.py:380
test_multiple_inheritance.test_pr3635_diamond_c
def test_pr3635_diamond_c()
Definition: test_multiple_inheritance.py:369


gtsam
Author(s):
autogenerated on Thu Jul 4 2024 03:06:01