Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:55

0001 import pytest
0002 
0003 import env  # noqa: F401
0004 from pybind11_tests import ConstructorStats
0005 from pybind11_tests import multiple_inheritance as m
0006 
0007 
0008 def test_multiple_inheritance_cpp():
0009     mt = m.MIType(3, 4)
0010 
0011     assert mt.foo() == 3
0012     assert mt.bar() == 4
0013 
0014 
0015 @pytest.mark.xfail("env.PYPY")
0016 def test_multiple_inheritance_mix1():
0017     class Base1:
0018         def __init__(self, i):
0019             self.i = i
0020 
0021         def foo(self):
0022             return self.i
0023 
0024     class MITypePy(Base1, m.Base2):
0025         def __init__(self, i, j):
0026             Base1.__init__(self, i)
0027             m.Base2.__init__(self, j)
0028 
0029     mt = MITypePy(3, 4)
0030 
0031     assert mt.foo() == 3
0032     assert mt.bar() == 4
0033 
0034 
0035 def test_multiple_inheritance_mix2():
0036     class Base2:
0037         def __init__(self, i):
0038             self.i = i
0039 
0040         def bar(self):
0041             return self.i
0042 
0043     class MITypePy(m.Base1, Base2):
0044         def __init__(self, i, j):
0045             m.Base1.__init__(self, i)
0046             Base2.__init__(self, j)
0047 
0048     mt = MITypePy(3, 4)
0049 
0050     assert mt.foo() == 3
0051     assert mt.bar() == 4
0052 
0053 
0054 @pytest.mark.xfail("env.PYPY")
0055 def test_multiple_inheritance_python():
0056     class MI1(m.Base1, m.Base2):
0057         def __init__(self, i, j):
0058             m.Base1.__init__(self, i)
0059             m.Base2.__init__(self, j)
0060 
0061     class B1:
0062         def v(self):
0063             return 1
0064 
0065     class MI2(B1, m.Base1, m.Base2):
0066         def __init__(self, i, j):
0067             B1.__init__(self)
0068             m.Base1.__init__(self, i)
0069             m.Base2.__init__(self, j)
0070 
0071     class MI3(MI2):
0072         def __init__(self, i, j):
0073             MI2.__init__(self, i, j)
0074 
0075     class MI4(MI3, m.Base2):
0076         def __init__(self, i, j):
0077             MI3.__init__(self, i, j)
0078             # This should be ignored (Base2 is already initialized via MI2):
0079             m.Base2.__init__(self, i + 100)
0080 
0081     class MI5(m.Base2, B1, m.Base1):
0082         def __init__(self, i, j):
0083             B1.__init__(self)
0084             m.Base1.__init__(self, i)
0085             m.Base2.__init__(self, j)
0086 
0087     class MI6(m.Base2, B1):
0088         def __init__(self, i):
0089             m.Base2.__init__(self, i)
0090             B1.__init__(self)
0091 
0092     class B2(B1):
0093         def v(self):
0094             return 2
0095 
0096     class B3:
0097         def v(self):
0098             return 3
0099 
0100     class B4(B3, B2):
0101         def v(self):
0102             return 4
0103 
0104     class MI7(B4, MI6):
0105         def __init__(self, i):
0106             B4.__init__(self)
0107             MI6.__init__(self, i)
0108 
0109     class MI8(MI6, B3):
0110         def __init__(self, i):
0111             MI6.__init__(self, i)
0112             B3.__init__(self)
0113 
0114     class MI8b(B3, MI6):
0115         def __init__(self, i):
0116             B3.__init__(self)
0117             MI6.__init__(self, i)
0118 
0119     mi1 = MI1(1, 2)
0120     assert mi1.foo() == 1
0121     assert mi1.bar() == 2
0122 
0123     mi2 = MI2(3, 4)
0124     assert mi2.v() == 1
0125     assert mi2.foo() == 3
0126     assert mi2.bar() == 4
0127 
0128     mi3 = MI3(5, 6)
0129     assert mi3.v() == 1
0130     assert mi3.foo() == 5
0131     assert mi3.bar() == 6
0132 
0133     mi4 = MI4(7, 8)
0134     assert mi4.v() == 1
0135     assert mi4.foo() == 7
0136     assert mi4.bar() == 8
0137 
0138     mi5 = MI5(10, 11)
0139     assert mi5.v() == 1
0140     assert mi5.foo() == 10
0141     assert mi5.bar() == 11
0142 
0143     mi6 = MI6(12)
0144     assert mi6.v() == 1
0145     assert mi6.bar() == 12
0146 
0147     mi7 = MI7(13)
0148     assert mi7.v() == 4
0149     assert mi7.bar() == 13
0150 
0151     mi8 = MI8(14)
0152     assert mi8.v() == 1
0153     assert mi8.bar() == 14
0154 
0155     mi8b = MI8b(15)
0156     assert mi8b.v() == 3
0157     assert mi8b.bar() == 15
0158 
0159 
0160 def test_multiple_inheritance_python_many_bases():
0161     class MIMany14(m.BaseN1, m.BaseN2, m.BaseN3, m.BaseN4):
0162         def __init__(self):
0163             m.BaseN1.__init__(self, 1)
0164             m.BaseN2.__init__(self, 2)
0165             m.BaseN3.__init__(self, 3)
0166             m.BaseN4.__init__(self, 4)
0167 
0168     class MIMany58(m.BaseN5, m.BaseN6, m.BaseN7, m.BaseN8):
0169         def __init__(self):
0170             m.BaseN5.__init__(self, 5)
0171             m.BaseN6.__init__(self, 6)
0172             m.BaseN7.__init__(self, 7)
0173             m.BaseN8.__init__(self, 8)
0174 
0175     class MIMany916(
0176         m.BaseN9,
0177         m.BaseN10,
0178         m.BaseN11,
0179         m.BaseN12,
0180         m.BaseN13,
0181         m.BaseN14,
0182         m.BaseN15,
0183         m.BaseN16,
0184     ):
0185         def __init__(self):
0186             m.BaseN9.__init__(self, 9)
0187             m.BaseN10.__init__(self, 10)
0188             m.BaseN11.__init__(self, 11)
0189             m.BaseN12.__init__(self, 12)
0190             m.BaseN13.__init__(self, 13)
0191             m.BaseN14.__init__(self, 14)
0192             m.BaseN15.__init__(self, 15)
0193             m.BaseN16.__init__(self, 16)
0194 
0195     class MIMany19(MIMany14, MIMany58, m.BaseN9):
0196         def __init__(self):
0197             MIMany14.__init__(self)
0198             MIMany58.__init__(self)
0199             m.BaseN9.__init__(self, 9)
0200 
0201     class MIMany117(MIMany14, MIMany58, MIMany916, m.BaseN17):
0202         def __init__(self):
0203             MIMany14.__init__(self)
0204             MIMany58.__init__(self)
0205             MIMany916.__init__(self)
0206             m.BaseN17.__init__(self, 17)
0207 
0208     # Inherits from 4 registered C++ classes: can fit in one pointer on any modern arch:
0209     a = MIMany14()
0210     for i in range(1, 4):
0211         assert getattr(a, "f" + str(i))() == 2 * i
0212 
0213     # Inherits from 8: requires 1/2 pointers worth of holder flags on 32/64-bit arch:
0214     b = MIMany916()
0215     for i in range(9, 16):
0216         assert getattr(b, "f" + str(i))() == 2 * i
0217 
0218     # Inherits from 9: requires >= 2 pointers worth of holder flags
0219     c = MIMany19()
0220     for i in range(1, 9):
0221         assert getattr(c, "f" + str(i))() == 2 * i
0222 
0223     # Inherits from 17: requires >= 3 pointers worth of holder flags
0224     d = MIMany117()
0225     for i in range(1, 17):
0226         assert getattr(d, "f" + str(i))() == 2 * i
0227 
0228 
0229 def test_multiple_inheritance_virtbase():
0230     class MITypePy(m.Base12a):
0231         def __init__(self, i, j):
0232             m.Base12a.__init__(self, i, j)
0233 
0234     mt = MITypePy(3, 4)
0235     assert mt.bar() == 4
0236     assert m.bar_base2a(mt) == 4
0237     assert m.bar_base2a_sharedptr(mt) == 4
0238 
0239 
0240 def test_mi_static_properties():
0241     """Mixing bases with and without static properties should be possible
0242     and the result should be independent of base definition order"""
0243 
0244     for d in (m.VanillaStaticMix1(), m.VanillaStaticMix2()):
0245         assert d.vanilla() == "Vanilla"
0246         assert d.static_func1() == "WithStatic1"
0247         assert d.static_func2() == "WithStatic2"
0248         assert d.static_func() == d.__class__.__name__
0249 
0250         m.WithStatic1.static_value1 = 1
0251         m.WithStatic2.static_value2 = 2
0252         assert d.static_value1 == 1
0253         assert d.static_value2 == 2
0254         assert d.static_value == 12
0255 
0256         d.static_value1 = 0
0257         assert d.static_value1 == 0
0258         d.static_value2 = 0
0259         assert d.static_value2 == 0
0260         d.static_value = 0
0261         assert d.static_value == 0
0262 
0263 
0264 # Requires PyPy 6+
0265 def test_mi_dynamic_attributes():
0266     """Mixing bases with and without dynamic attribute support"""
0267 
0268     for d in (m.VanillaDictMix1(), m.VanillaDictMix2()):
0269         d.dynamic = 1
0270         assert d.dynamic == 1
0271 
0272 
0273 def test_mi_unaligned_base():
0274     """Returning an offset (non-first MI) base class pointer should recognize the instance"""
0275 
0276     n_inst = ConstructorStats.detail_reg_inst()
0277 
0278     c = m.I801C()
0279     d = m.I801D()
0280     # + 4 below because we have the two instances, and each instance has offset base I801B2
0281     assert ConstructorStats.detail_reg_inst() == n_inst + 4
0282     b1c = m.i801b1_c(c)
0283     assert b1c is c
0284     b2c = m.i801b2_c(c)
0285     assert b2c is c
0286     b1d = m.i801b1_d(d)
0287     assert b1d is d
0288     b2d = m.i801b2_d(d)
0289     assert b2d is d
0290 
0291     assert ConstructorStats.detail_reg_inst() == n_inst + 4  # no extra instances
0292     del c, b1c, b2c
0293     assert ConstructorStats.detail_reg_inst() == n_inst + 2
0294     del d, b1d, b2d
0295     assert ConstructorStats.detail_reg_inst() == n_inst
0296 
0297 
0298 def test_mi_base_return():
0299     """Tests returning an offset (non-first MI) base class pointer to a derived instance"""
0300 
0301     n_inst = ConstructorStats.detail_reg_inst()
0302 
0303     c1 = m.i801c_b1()
0304     assert type(c1) is m.I801C
0305     assert c1.a == 1
0306     assert c1.b == 2
0307 
0308     d1 = m.i801d_b1()
0309     assert type(d1) is m.I801D
0310     assert d1.a == 1
0311     assert d1.b == 2
0312 
0313     assert ConstructorStats.detail_reg_inst() == n_inst + 4
0314 
0315     c2 = m.i801c_b2()
0316     assert type(c2) is m.I801C
0317     assert c2.a == 1
0318     assert c2.b == 2
0319 
0320     d2 = m.i801d_b2()
0321     assert type(d2) is m.I801D
0322     assert d2.a == 1
0323     assert d2.b == 2
0324 
0325     assert ConstructorStats.detail_reg_inst() == n_inst + 8
0326 
0327     del c2
0328     assert ConstructorStats.detail_reg_inst() == n_inst + 6
0329     del c1, d1, d2
0330     assert ConstructorStats.detail_reg_inst() == n_inst
0331 
0332     # Returning an unregistered derived type with a registered base; we won't
0333     # pick up the derived type, obviously, but should still work (as an object
0334     # of whatever type was returned).
0335     e1 = m.i801e_c()
0336     assert type(e1) is m.I801C
0337     assert e1.a == 1
0338     assert e1.b == 2
0339 
0340     e2 = m.i801e_b2()
0341     assert type(e2) is m.I801B2
0342     assert e2.b == 2
0343 
0344 
0345 def test_diamond_inheritance():
0346     """Tests that diamond inheritance works as expected (issue #959)"""
0347 
0348     # Issue #959: this shouldn't segfault:
0349     d = m.D()
0350 
0351     # Make sure all the various distinct pointers are all recognized as registered instances:
0352     assert d is d.c0()
0353     assert d is d.c1()
0354     assert d is d.b()
0355     assert d is d.c0().b()
0356     assert d is d.c1().b()
0357     assert d is d.c0().c1().b().c0().b()
0358 
0359 
0360 def test_pr3635_diamond_b():
0361     o = m.MVB()
0362     assert o.b == 1
0363 
0364     assert o.get_b_b() == 1
0365 
0366 
0367 def test_pr3635_diamond_c():
0368     o = m.MVC()
0369     assert o.b == 1
0370     assert o.c == 2
0371 
0372     assert o.get_b_b() == 1
0373     assert o.get_c_b() == 1
0374 
0375     assert o.get_c_c() == 2
0376 
0377 
0378 def test_pr3635_diamond_d0():
0379     o = m.MVD0()
0380     assert o.b == 1
0381     assert o.c == 2
0382     assert o.d0 == 3
0383 
0384     assert o.get_b_b() == 1
0385     assert o.get_c_b() == 1
0386     assert o.get_d0_b() == 1
0387 
0388     assert o.get_c_c() == 2
0389     assert o.get_d0_c() == 2
0390 
0391     assert o.get_d0_d0() == 3
0392 
0393 
0394 def test_pr3635_diamond_d1():
0395     o = m.MVD1()
0396     assert o.b == 1
0397     assert o.c == 2
0398     assert o.d1 == 4
0399 
0400     assert o.get_b_b() == 1
0401     assert o.get_c_b() == 1
0402     assert o.get_d1_b() == 1
0403 
0404     assert o.get_c_c() == 2
0405     assert o.get_d1_c() == 2
0406 
0407     assert o.get_d1_d1() == 4
0408 
0409 
0410 def test_pr3635_diamond_e():
0411     o = m.MVE()
0412     assert o.b == 1
0413     assert o.c == 2
0414     assert o.d0 == 3
0415     assert o.d1 == 4
0416     assert o.e == 5
0417 
0418     assert o.get_b_b() == 1
0419     assert o.get_c_b() == 1
0420     assert o.get_d0_b() == 1
0421     assert o.get_d1_b() == 1
0422     assert o.get_e_b() == 1
0423 
0424     assert o.get_c_c() == 2
0425     assert o.get_d0_c() == 2
0426     assert o.get_d1_c() == 2
0427     assert o.get_e_c() == 2
0428 
0429     assert o.get_d0_d0() == 3
0430     assert o.get_e_d0() == 3
0431 
0432     assert o.get_d1_d1() == 4
0433     assert o.get_e_d1() == 4
0434 
0435     assert o.get_e_e() == 5
0436 
0437 
0438 def test_pr3635_diamond_f():
0439     o = m.MVF()
0440     assert o.b == 1
0441     assert o.c == 2
0442     assert o.d0 == 3
0443     assert o.d1 == 4
0444     assert o.e == 5
0445     assert o.f == 6
0446 
0447     assert o.get_b_b() == 1
0448     assert o.get_c_b() == 1
0449     assert o.get_d0_b() == 1
0450     assert o.get_d1_b() == 1
0451     assert o.get_e_b() == 1
0452     assert o.get_f_b() == 1
0453 
0454     assert o.get_c_c() == 2
0455     assert o.get_d0_c() == 2
0456     assert o.get_d1_c() == 2
0457     assert o.get_e_c() == 2
0458     assert o.get_f_c() == 2
0459 
0460     assert o.get_d0_d0() == 3
0461     assert o.get_e_d0() == 3
0462     assert o.get_f_d0() == 3
0463 
0464     assert o.get_d1_d1() == 4
0465     assert o.get_e_d1() == 4
0466     assert o.get_f_d1() == 4
0467 
0468     assert o.get_e_e() == 5
0469     assert o.get_f_e() == 5
0470 
0471     assert o.get_f_f() == 6
0472 
0473 
0474 def test_python_inherit_from_mi():
0475     """Tests extending a Python class from a single inheritor of a MI class"""
0476 
0477     class PyMVF(m.MVF):
0478         g = 7
0479 
0480         def get_g_g(self):
0481             return self.g
0482 
0483     o = PyMVF()
0484 
0485     assert o.b == 1
0486     assert o.c == 2
0487     assert o.d0 == 3
0488     assert o.d1 == 4
0489     assert o.e == 5
0490     assert o.f == 6
0491     assert o.g == 7
0492 
0493     assert o.get_g_g() == 7