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


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:05:28