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


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:03