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