You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
9.1 KiB

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