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