test_multiple_inheritance.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import pytest
  2. from pybind11_tests import ConstructorStats
  3. from pybind11_tests import multiple_inheritance as m
  4. def test_multiple_inheritance_cpp():
  5. mt = m.MIType(3, 4)
  6. assert mt.foo() == 3
  7. assert mt.bar() == 4
  8. def test_multiple_inheritance_mix1():
  9. class Base1:
  10. def __init__(self, i):
  11. self.i = i
  12. def foo(self):
  13. return self.i
  14. class MITypePy(Base1, m.Base2):
  15. def __init__(self, i, j):
  16. Base1.__init__(self, i)
  17. m.Base2.__init__(self, j)
  18. mt = MITypePy(3, 4)
  19. assert mt.foo() == 3
  20. assert mt.bar() == 4
  21. def test_multiple_inheritance_mix2():
  22. class Base2:
  23. def __init__(self, i):
  24. self.i = i
  25. def bar(self):
  26. return self.i
  27. class MITypePy(m.Base1, Base2):
  28. def __init__(self, i, j):
  29. m.Base1.__init__(self, i)
  30. Base2.__init__(self, j)
  31. mt = MITypePy(3, 4)
  32. assert mt.foo() == 3
  33. assert mt.bar() == 4
  34. def test_multiple_inheritance_python():
  35. class MI1(m.Base1, m.Base2):
  36. def __init__(self, i, j):
  37. m.Base1.__init__(self, i)
  38. m.Base2.__init__(self, j)
  39. class B1(object):
  40. def v(self):
  41. return 1
  42. class MI2(B1, m.Base1, m.Base2):
  43. def __init__(self, i, j):
  44. B1.__init__(self)
  45. m.Base1.__init__(self, i)
  46. m.Base2.__init__(self, j)
  47. class MI3(MI2):
  48. def __init__(self, i, j):
  49. MI2.__init__(self, i, j)
  50. class MI4(MI3, m.Base2):
  51. def __init__(self, i, j):
  52. MI3.__init__(self, i, j)
  53. # This should be ignored (Base2 is already initialized via MI2):
  54. m.Base2.__init__(self, i + 100)
  55. class MI5(m.Base2, B1, m.Base1):
  56. def __init__(self, i, j):
  57. B1.__init__(self)
  58. m.Base1.__init__(self, i)
  59. m.Base2.__init__(self, j)
  60. class MI6(m.Base2, B1):
  61. def __init__(self, i):
  62. m.Base2.__init__(self, i)
  63. B1.__init__(self)
  64. class B2(B1):
  65. def v(self):
  66. return 2
  67. class B3(object):
  68. def v(self):
  69. return 3
  70. class B4(B3, B2):
  71. def v(self):
  72. return 4
  73. class MI7(B4, MI6):
  74. def __init__(self, i):
  75. B4.__init__(self)
  76. MI6.__init__(self, i)
  77. class MI8(MI6, B3):
  78. def __init__(self, i):
  79. MI6.__init__(self, i)
  80. B3.__init__(self)
  81. class MI8b(B3, MI6):
  82. def __init__(self, i):
  83. B3.__init__(self)
  84. MI6.__init__(self, i)
  85. mi1 = MI1(1, 2)
  86. assert mi1.foo() == 1
  87. assert mi1.bar() == 2
  88. mi2 = MI2(3, 4)
  89. assert mi2.v() == 1
  90. assert mi2.foo() == 3
  91. assert mi2.bar() == 4
  92. mi3 = MI3(5, 6)
  93. assert mi3.v() == 1
  94. assert mi3.foo() == 5
  95. assert mi3.bar() == 6
  96. mi4 = MI4(7, 8)
  97. assert mi4.v() == 1
  98. assert mi4.foo() == 7
  99. assert mi4.bar() == 8
  100. mi5 = MI5(10, 11)
  101. assert mi5.v() == 1
  102. assert mi5.foo() == 10
  103. assert mi5.bar() == 11
  104. mi6 = MI6(12)
  105. assert mi6.v() == 1
  106. assert mi6.bar() == 12
  107. mi7 = MI7(13)
  108. assert mi7.v() == 4
  109. assert mi7.bar() == 13
  110. mi8 = MI8(14)
  111. assert mi8.v() == 1
  112. assert mi8.bar() == 14
  113. mi8b = MI8b(15)
  114. assert mi8b.v() == 3
  115. assert mi8b.bar() == 15
  116. def test_multiple_inheritance_python_many_bases():
  117. class MIMany14(m.BaseN1, m.BaseN2, m.BaseN3, m.BaseN4):
  118. def __init__(self):
  119. m.BaseN1.__init__(self, 1)
  120. m.BaseN2.__init__(self, 2)
  121. m.BaseN3.__init__(self, 3)
  122. m.BaseN4.__init__(self, 4)
  123. class MIMany58(m.BaseN5, m.BaseN6, m.BaseN7, m.BaseN8):
  124. def __init__(self):
  125. m.BaseN5.__init__(self, 5)
  126. m.BaseN6.__init__(self, 6)
  127. m.BaseN7.__init__(self, 7)
  128. m.BaseN8.__init__(self, 8)
  129. class MIMany916(m.BaseN9, m.BaseN10, m.BaseN11, m.BaseN12, m.BaseN13, m.BaseN14, m.BaseN15,
  130. m.BaseN16):
  131. def __init__(self):
  132. m.BaseN9.__init__(self, 9)
  133. m.BaseN10.__init__(self, 10)
  134. m.BaseN11.__init__(self, 11)
  135. m.BaseN12.__init__(self, 12)
  136. m.BaseN13.__init__(self, 13)
  137. m.BaseN14.__init__(self, 14)
  138. m.BaseN15.__init__(self, 15)
  139. m.BaseN16.__init__(self, 16)
  140. class MIMany19(MIMany14, MIMany58, m.BaseN9):
  141. def __init__(self):
  142. MIMany14.__init__(self)
  143. MIMany58.__init__(self)
  144. m.BaseN9.__init__(self, 9)
  145. class MIMany117(MIMany14, MIMany58, MIMany916, m.BaseN17):
  146. def __init__(self):
  147. MIMany14.__init__(self)
  148. MIMany58.__init__(self)
  149. MIMany916.__init__(self)
  150. m.BaseN17.__init__(self, 17)
  151. # Inherits from 4 registered C++ classes: can fit in one pointer on any modern arch:
  152. a = MIMany14()
  153. for i in range(1, 4):
  154. assert getattr(a, "f" + str(i))() == 2 * i
  155. # Inherits from 8: requires 1/2 pointers worth of holder flags on 32/64-bit arch:
  156. b = MIMany916()
  157. for i in range(9, 16):
  158. assert getattr(b, "f" + str(i))() == 2 * i
  159. # Inherits from 9: requires >= 2 pointers worth of holder flags
  160. c = MIMany19()
  161. for i in range(1, 9):
  162. assert getattr(c, "f" + str(i))() == 2 * i
  163. # Inherits from 17: requires >= 3 pointers worth of holder flags
  164. d = MIMany117()
  165. for i in range(1, 17):
  166. assert getattr(d, "f" + str(i))() == 2 * i
  167. def test_multiple_inheritance_virtbase():
  168. class MITypePy(m.Base12a):
  169. def __init__(self, i, j):
  170. m.Base12a.__init__(self, i, j)
  171. mt = MITypePy(3, 4)
  172. assert mt.bar() == 4
  173. assert m.bar_base2a(mt) == 4
  174. assert m.bar_base2a_sharedptr(mt) == 4
  175. def test_mi_static_properties():
  176. """Mixing bases with and without static properties should be possible
  177. and the result should be independent of base definition order"""
  178. for d in (m.VanillaStaticMix1(), m.VanillaStaticMix2()):
  179. assert d.vanilla() == "Vanilla"
  180. assert d.static_func1() == "WithStatic1"
  181. assert d.static_func2() == "WithStatic2"
  182. assert d.static_func() == d.__class__.__name__
  183. m.WithStatic1.static_value1 = 1
  184. m.WithStatic2.static_value2 = 2
  185. assert d.static_value1 == 1
  186. assert d.static_value2 == 2
  187. assert d.static_value == 12
  188. d.static_value1 = 0
  189. assert d.static_value1 == 0
  190. d.static_value2 = 0
  191. assert d.static_value2 == 0
  192. d.static_value = 0
  193. assert d.static_value == 0
  194. @pytest.unsupported_on_pypy
  195. def test_mi_dynamic_attributes():
  196. """Mixing bases with and without dynamic attribute support"""
  197. for d in (m.VanillaDictMix1(), m.VanillaDictMix2()):
  198. d.dynamic = 1
  199. assert d.dynamic == 1
  200. def test_mi_unaligned_base():
  201. """Returning an offset (non-first MI) base class pointer should recognize the instance"""
  202. n_inst = ConstructorStats.detail_reg_inst()
  203. c = m.I801C()
  204. d = m.I801D()
  205. # + 4 below because we have the two instances, and each instance has offset base I801B2
  206. assert ConstructorStats.detail_reg_inst() == n_inst + 4
  207. b1c = m.i801b1_c(c)
  208. assert b1c is c
  209. b2c = m.i801b2_c(c)
  210. assert b2c is c
  211. b1d = m.i801b1_d(d)
  212. assert b1d is d
  213. b2d = m.i801b2_d(d)
  214. assert b2d is d
  215. assert ConstructorStats.detail_reg_inst() == n_inst + 4 # no extra instances
  216. del c, b1c, b2c
  217. assert ConstructorStats.detail_reg_inst() == n_inst + 2
  218. del d, b1d, b2d
  219. assert ConstructorStats.detail_reg_inst() == n_inst
  220. def test_mi_base_return():
  221. """Tests returning an offset (non-first MI) base class pointer to a derived instance"""
  222. n_inst = ConstructorStats.detail_reg_inst()
  223. c1 = m.i801c_b1()
  224. assert type(c1) is m.I801C
  225. assert c1.a == 1
  226. assert c1.b == 2
  227. d1 = m.i801d_b1()
  228. assert type(d1) is m.I801D
  229. assert d1.a == 1
  230. assert d1.b == 2
  231. assert ConstructorStats.detail_reg_inst() == n_inst + 4
  232. c2 = m.i801c_b2()
  233. assert type(c2) is m.I801C
  234. assert c2.a == 1
  235. assert c2.b == 2
  236. d2 = m.i801d_b2()
  237. assert type(d2) is m.I801D
  238. assert d2.a == 1
  239. assert d2.b == 2
  240. assert ConstructorStats.detail_reg_inst() == n_inst + 8
  241. del c2
  242. assert ConstructorStats.detail_reg_inst() == n_inst + 6
  243. del c1, d1, d2
  244. assert ConstructorStats.detail_reg_inst() == n_inst
  245. # Returning an unregistered derived type with a registered base; we won't
  246. # pick up the derived type, obviously, but should still work (as an object
  247. # of whatever type was returned).
  248. e1 = m.i801e_c()
  249. assert type(e1) is m.I801C
  250. assert e1.a == 1
  251. assert e1.b == 2
  252. e2 = m.i801e_b2()
  253. assert type(e2) is m.I801B2
  254. assert e2.b == 2
  255. def test_diamond_inheritance():
  256. """Tests that diamond inheritance works as expected (issue #959)"""
  257. # Issue #959: this shouldn't segfault:
  258. d = m.D()
  259. # Make sure all the various distinct pointers are all recognized as registered instances:
  260. assert d is d.c0()
  261. assert d is d.c1()
  262. assert d is d.b()
  263. assert d is d.c0().b()
  264. assert d is d.c1().b()
  265. assert d is d.c0().c1().b().c0().b()