5 from pybind11_tests
import ConstructorStats
6 from pybind11_tests
import factory_constructors
as m
7 from pybind11_tests.factory_constructors
import tag
11 """Tests py::init_factory() wrapper around various ways of returning the object""" 15 for c
in [m.TestFactory1, m.TestFactory2, m.TestFactory3]
18 n_inst = ConstructorStats.detail_reg_inst()
20 x1 = m.TestFactory1(tag.unique_ptr, 3)
21 assert x1.value ==
"3" 22 y1 = m.TestFactory1(tag.pointer)
23 assert y1.value ==
"(empty)" 24 z1 = m.TestFactory1(
"hi!")
25 assert z1.value ==
"hi!" 27 assert ConstructorStats.detail_reg_inst() == n_inst + 3
29 x2 = m.TestFactory2(tag.move)
30 assert x2.value ==
"(empty2)" 31 y2 = m.TestFactory2(tag.pointer, 7)
32 assert y2.value ==
"7" 33 z2 = m.TestFactory2(tag.unique_ptr,
"hi again")
34 assert z2.value ==
"hi again" 36 assert ConstructorStats.detail_reg_inst() == n_inst + 6
38 x3 = m.TestFactory3(tag.shared_ptr)
39 assert x3.value ==
"(empty3)" 40 y3 = m.TestFactory3(tag.pointer, 42)
41 assert y3.value ==
"42" 42 z3 = m.TestFactory3(
"bye")
43 assert z3.value ==
"bye" 45 for null_ptr_kind
in [tag.null_ptr, tag.null_unique_ptr, tag.null_shared_ptr]:
46 with pytest.raises(TypeError)
as excinfo:
47 m.TestFactory3(null_ptr_kind)
49 str(excinfo.value) ==
"pybind11::init(): factory function returned nullptr" 52 assert [i.alive()
for i
in cstats] == [3, 3, 3]
53 assert ConstructorStats.detail_reg_inst() == n_inst + 9
56 assert [i.alive()
for i
in cstats] == [2, 2, 1]
57 assert ConstructorStats.detail_reg_inst() == n_inst + 5
58 del x2, x3, y1, z1, z2
59 assert [i.alive()
for i
in cstats] == [0, 0, 0]
60 assert ConstructorStats.detail_reg_inst() == n_inst
62 assert [i.values()
for i
in cstats] == [
67 assert [i.default_constructions
for i
in cstats] == [1, 1, 1]
71 with pytest.raises(TypeError)
as excinfo:
72 m.TestFactory1(
"invalid",
"constructor",
"arguments")
76 __init__(): incompatible constructor arguments. The following argument types are supported: 77 1. m.factory_constructors.TestFactory1(arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: int) 78 2. m.factory_constructors.TestFactory1(arg0: str) 79 3. m.factory_constructors.TestFactory1(arg0: m.factory_constructors.tag.pointer_tag) 80 4. m.factory_constructors.TestFactory1(arg0: handle, arg1: int, arg2: handle) 82 Invoked with: 'invalid', 'constructor', 'arguments' 87 msg(m.TestFactory1.__init__.__doc__)
89 __init__(*args, **kwargs) 92 1. __init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: int) -> None 94 2. __init__(self: m.factory_constructors.TestFactory1, arg0: str) -> None 96 3. __init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.pointer_tag) -> None 98 4. __init__(self: m.factory_constructors.TestFactory1, arg0: handle, arg1: int, arg2: handle) -> None 104 """Tests py::init_factory() wrapper with various upcasting and downcasting returns""" 108 for c
in [m.TestFactory3, m.TestFactory4, m.TestFactory5]
111 n_inst = ConstructorStats.detail_reg_inst()
114 a = m.TestFactory3(tag.pointer, tag.TF4, 4)
115 assert a.value ==
"4" 116 b = m.TestFactory3(tag.shared_ptr, tag.TF4, 5)
117 assert b.value ==
"5" 118 c = m.TestFactory3(tag.pointer, tag.TF5, 6)
119 assert c.value ==
"6" 120 d = m.TestFactory3(tag.shared_ptr, tag.TF5, 7)
121 assert d.value ==
"7" 123 assert ConstructorStats.detail_reg_inst() == n_inst + 4
126 e = m.TestFactory4(tag.pointer, tag.TF4, 8)
127 assert e.value ==
"8" 129 assert ConstructorStats.detail_reg_inst() == n_inst + 5
130 assert [i.alive()
for i
in cstats] == [5, 3, 2]
133 assert [i.alive()
for i
in cstats] == [4, 2, 2]
134 assert ConstructorStats.detail_reg_inst() == n_inst + 4
137 assert [i.alive()
for i
in cstats] == [1, 0, 1]
138 assert ConstructorStats.detail_reg_inst() == n_inst + 1
141 assert [i.alive()
for i
in cstats] == [0, 0, 0]
142 assert ConstructorStats.detail_reg_inst() == n_inst
144 assert [i.values()
for i
in cstats] == [
145 [
"4",
"5",
"6",
"7",
"8"],
152 """Tests py::init_factory() wrapper with value conversions and alias types""" 154 cstats = [m.TestFactory6.get_cstats(), m.TestFactory6.get_alias_cstats()]
156 n_inst = ConstructorStats.detail_reg_inst()
158 a = m.TestFactory6(tag.base, 1)
160 assert not a.has_alias()
161 b = m.TestFactory6(tag.alias,
"hi there")
164 c = m.TestFactory6(tag.alias, 3)
167 d = m.TestFactory6(tag.alias, tag.pointer, 4)
170 e = m.TestFactory6(tag.base, tag.pointer, 5)
172 assert not e.has_alias()
173 f = m.TestFactory6(tag.base, tag.alias, tag.pointer, 6)
177 assert ConstructorStats.detail_reg_inst() == n_inst + 6
178 assert [i.alive()
for i
in cstats] == [6, 4]
181 assert [i.alive()
for i
in cstats] == [3, 3]
182 assert ConstructorStats.detail_reg_inst() == n_inst + 3
184 assert [i.alive()
for i
in cstats] == [0, 0]
185 assert ConstructorStats.detail_reg_inst() == n_inst
187 class MyTest(m.TestFactory6):
189 m.TestFactory6.__init__(self, *args)
192 return -5 + m.TestFactory6.get(self)
195 z = MyTest(tag.base, 123)
196 assert z.get() == 118
200 y = MyTest(tag.alias,
"why hello!")
205 x = MyTest(tag.base, tag.pointer, 47)
209 assert ConstructorStats.detail_reg_inst() == n_inst + 3
210 assert [i.alive()
for i
in cstats] == [3, 3]
212 assert [i.alive()
for i
in cstats] == [0, 0]
213 assert ConstructorStats.detail_reg_inst() == n_inst
215 assert [i.values()
for i
in cstats] == [
216 [
"1",
"8",
"3",
"4",
"5",
"6",
"123",
"10",
"47"],
217 [
"hi there",
"3",
"4",
"6",
"move",
"123",
"why hello!",
"move",
"47"],
222 """Tests init factory functions with dual main/alias factory functions""" 223 from pybind11_tests.factory_constructors
import TestFactory7
225 cstats = [TestFactory7.get_cstats(), TestFactory7.get_alias_cstats()]
227 n_inst = ConstructorStats.detail_reg_inst()
236 assert a2.get() == 102
237 assert not a1.has_alias()
238 assert a2.has_alias()
241 b2 = PythFactory7(tag.pointer, 4)
243 assert b2.get() == 104
244 assert not b1.has_alias()
245 assert b2.has_alias()
248 c2 = PythFactory7(tag.mixed, 6)
250 assert c2.get() == 106
251 assert not c1.has_alias()
252 assert c2.has_alias()
255 d2 = PythFactory7(tag.base, tag.pointer, 8)
257 assert d2.get() == 108
258 assert not d1.has_alias()
259 assert d2.has_alias()
263 e2 = PythFactory7(tag.alias, tag.pointer, 10)
265 assert e2.get() == 200
266 assert e1.has_alias()
267 assert e2.has_alias()
270 f2 = PythFactory7(tag.shared_ptr, tag.base, 12)
271 assert f1.get() == 11
272 assert f2.get() == 112
273 assert not f1.has_alias()
274 assert f2.has_alias()
277 assert g1.get() == 13
278 assert not g1.has_alias()
279 with pytest.raises(TypeError)
as excinfo:
280 PythFactory7(tag.shared_ptr, tag.invalid_base, 14)
283 ==
"pybind11::init(): construction failed: returned holder-wrapped instance is not an " 287 assert [i.alive()
for i
in cstats] == [13, 7]
288 assert ConstructorStats.detail_reg_inst() == n_inst + 13
290 del a1, a2, b1, d1, e1, e2
291 assert [i.alive()
for i
in cstats] == [7, 4]
292 assert ConstructorStats.detail_reg_inst() == n_inst + 7
293 del b2, c1, c2, d2, f1, f2, g1
294 assert [i.alive()
for i
in cstats] == [0, 0]
295 assert ConstructorStats.detail_reg_inst() == n_inst
297 assert [i.values()
for i
in cstats] == [
298 [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"100",
"11",
"12",
"13",
"14"],
299 [
"2",
"4",
"6",
"8",
"9",
"100",
"12"],
304 """Prior to 2.2, `py::init<...>` relied on the type supporting placement 305 new; this tests a class without placement new support.""" 307 a = m.NoPlacementNew(123)
309 found = re.search(
r"^operator new called, returning (\d+)\n$",
str(capture))
315 assert capture ==
"operator delete called on " + found.group(1)
318 b = m.NoPlacementNew()
320 found = re.search(
r"^operator new called, returning (\d+)\n$",
str(capture))
326 assert capture ==
"operator delete called on " + found.group(1)
330 class MITest(m.TestFactory1, m.TestFactory2):
332 m.TestFactory1.__init__(self, tag.unique_ptr, 33)
333 m.TestFactory2.__init__(self, tag.move)
336 assert m.TestFactory1.value.fget(a) ==
"33" 337 assert m.TestFactory2.value.fget(a) ==
"(empty2)" 341 a = m.NoisyAlloc(*args)
348 return re.sub(
r"\s+#.*",
"", s)
352 """When the constructor is overloaded, previous overloads can require a preallocated value. 353 This test makes sure that such preallocated values only happen when they might be necessary, 354 and that they are deallocated properly.""" 378 noisy new # allocation required to attempt first overload 379 noisy delete # have to dealloc before considering factory init overload 380 noisy new # pointer factory calling "new", part 1: allocation 381 NoisyAlloc(double 1.5) # ... part two, invoking constructor 383 ~NoisyAlloc() # Destructor 384 noisy delete # operator delete 394 noisy new # pointer factory calling "new", allocation 395 NoisyAlloc(int 2) # constructor 397 ~NoisyAlloc() # Destructor 398 noisy delete # operator delete 408 NoisyAlloc(double 2.5) # construction (local func variable: operator_new not called) 409 noisy new # return-by-value "new" part 1: allocation 410 ~NoisyAlloc() # moved-away local func variable destruction 412 ~NoisyAlloc() # Destructor 413 noisy delete # operator delete 423 noisy new # preallocation needed before invoking placement-new overload 424 noisy placement new # Placement new 425 NoisyAlloc(double 3.5) # construction 427 ~NoisyAlloc() # Destructor 428 noisy delete # operator delete 438 noisy new # preallocation needed before invoking placement-new overload 439 noisy delete # deallocation of preallocated storage 440 noisy new # Factory pointer allocation 441 NoisyAlloc(int 4) # factory pointer construction 443 ~NoisyAlloc() # Destructor 444 noisy delete # operator delete 454 noisy new # preallocation needed before invoking first placement new 455 noisy delete # delete before considering new-style constructor 456 noisy new # preallocation for second placement new 457 noisy placement new # Placement new in the second placement new overload 458 NoisyAlloc(int 5) # construction 460 ~NoisyAlloc() # Destructor 461 noisy delete # operator delete 467 """Tests invocation of the pybind-registered base class with an invalid `self` argument.""" 469 class NotPybindDerived:
473 class BrokenTF1(m.TestFactory1):
476 a = m.TestFactory2(tag.pointer, 1)
477 m.TestFactory1.__init__(a, tag.pointer)
479 a = NotPybindDerived()
480 m.TestFactory1.__init__(a, tag.pointer)
483 class BrokenTF6(m.TestFactory6):
486 m.TestFactory6.__init__()
488 a = m.TestFactory2(tag.pointer, 1)
489 m.TestFactory6.__init__(a, tag.base, 1)
491 a = m.TestFactory2(tag.pointer, 1)
492 m.TestFactory6.__init__(a, tag.alias, 1)
494 m.TestFactory6.__init__(
495 NotPybindDerived.__new__(NotPybindDerived), tag.base, 1
498 m.TestFactory6.__init__(
499 NotPybindDerived.__new__(NotPybindDerived), tag.alias, 1
503 with pytest.raises(TypeError)
as excinfo:
507 ==
"__init__(self, ...) called with invalid or missing `self` argument" 510 for arg
in (0, 1, 2, 3, 4):
511 with pytest.raises(TypeError)
as excinfo:
515 ==
"__init__(self, ...) called with invalid or missing `self` argument" def test_reallocation_a(capture, msg)
def create_and_destroy(args)
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
def test_init_factory_signature(msg)
def test_init_factory_casting()
def test_reallocation_d(capture, msg)
def test_reallocation_g(capture, msg)
static ConstructorStats & get(std::type_index type)
def test_multiple_inheritance()
def test_reallocation_f(capture, msg)
def test_reallocation_e(capture, msg)
def test_init_factory_basic()
def test_no_placement_new(capture)
def test_init_factory_alias()
def test_init_factory_dual()
def test_reallocation_c(capture, msg)
def test_reallocation_b(capture, msg)
Container::iterator get(Container &c, Position position)