MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_smart_ptr

Function test_smart_ptr

tests/test_smart_ptr.py:12–117  ·  view source on GitHub ↗
(capture)

Source from the content-addressed store, hash-verified

10
11@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC")
12def test_smart_ptr(capture):
13 # Object1
14 for i, o in enumerate(
15 [m.make_object_1(), m.make_object_2(), m.MyObject1(3)], start=1
16 ):
17 assert o.getRefCount() == 1
18 with capture:
19 m.print_object_1(o)
20 m.print_object_2(o)
21 m.print_object_3(o)
22 m.print_object_4(o)
23 assert capture == f"MyObject1[{i}]\n" * 4
24
25 for i, o in enumerate(
26 [m.make_myobject1_1(), m.make_myobject1_2(), m.MyObject1(6), 7], start=4
27 ):
28 print(o)
29 with capture:
30 if not isinstance(o, int):
31 m.print_object_1(o)
32 m.print_object_2(o)
33 m.print_object_3(o)
34 m.print_object_4(o)
35 m.print_myobject1_1(o)
36 m.print_myobject1_2(o)
37 m.print_myobject1_3(o)
38 m.print_myobject1_4(o)
39
40 times = 4 if isinstance(o, int) else 8
41 assert capture == f"MyObject1[{i}]\n" * times
42
43 cstats = ConstructorStats.get(m.MyObject1)
44 assert cstats.alive() == 0
45 expected_values = [f"MyObject1[{i}]" for i in range(1, 7)] + ["MyObject1[7]"] * 4
46 assert cstats.values() == expected_values
47 assert cstats.default_constructions == 0
48 assert cstats.copy_constructions == 0
49 # assert cstats.move_constructions >= 0 # Doesn't invoke any
50 assert cstats.copy_assignments == 0
51 assert cstats.move_assignments == 0
52
53 # Object2
54 for i, o in zip(
55 [8, 6, 7], [m.MyObject2(8), m.make_myobject2_1(), m.make_myobject2_2()]
56 ):
57 print(o)
58 with capture:
59 m.print_myobject2_1(o)
60 m.print_myobject2_2(o)
61 m.print_myobject2_3(o)
62 m.print_myobject2_4(o)
63 assert capture == f"MyObject2[{i}]\n" * 4
64
65 cstats = ConstructorStats.get(m.MyObject2)
66 assert cstats.alive() == 1
67 o = None
68 assert cstats.alive() == 0
69 assert cstats.values() == ["MyObject2[8]", "MyObject2[6]", "MyObject2[7]"]

Callers

nothing calls this directly

Calls 9

printFunction · 0.85
isinstanceFunction · 0.85
MyObject1Method · 0.80
getRefCountMethod · 0.80
aliveMethod · 0.80
valuesMethod · 0.80
MyObject2Method · 0.80
MyObject3Method · 0.80
getMethod · 0.45

Tested by

no test coverage detected