test_multiple_inheritance.py 9.3 KB

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