test_custom_type_casters.py
Go to the documentation of this file.
1 import pytest
2 
3 from pybind11_tests import custom_type_casters as m
4 
5 
7  a = m.ArgInspector()
8  assert (
9  msg(a.f("hi"))
10  == """
11  loading ArgInspector1 argument WITH conversion allowed. Argument value = hi
12  """
13  )
14  assert (
15  msg(a.g("this is a", "this is b"))
16  == """
17  loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = this is a
18  loading ArgInspector1 argument WITH conversion allowed. Argument value = this is b
19  13
20  loading ArgInspector2 argument WITH conversion allowed. Argument value = (default arg inspector 2)
21  """
22  )
23  assert (
24  msg(a.g("this is a", "this is b", 42))
25  == """
26  loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = this is a
27  loading ArgInspector1 argument WITH conversion allowed. Argument value = this is b
28  42
29  loading ArgInspector2 argument WITH conversion allowed. Argument value = (default arg inspector 2)
30  """
31  )
32  assert (
33  msg(a.g("this is a", "this is b", 42, "this is d"))
34  == """
35  loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = this is a
36  loading ArgInspector1 argument WITH conversion allowed. Argument value = this is b
37  42
38  loading ArgInspector2 argument WITH conversion allowed. Argument value = this is d
39  """
40  )
41  assert (
42  a.h("arg 1")
43  == "loading ArgInspector2 argument WITHOUT conversion allowed. Argument value = arg 1"
44  )
45  assert (
46  msg(m.arg_inspect_func("A1", "A2"))
47  == """
48  loading ArgInspector2 argument WITH conversion allowed. Argument value = A1
49  loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = A2
50  """
51  )
52 
53  assert m.floats_preferred(4) == 2.0
54  assert m.floats_only(4.0) == 2.0
55  with pytest.raises(TypeError) as excinfo:
56  m.floats_only(4)
57  assert (
58  msg(excinfo.value)
59  == """
60  floats_only(): incompatible function arguments. The following argument types are supported:
61  1. (f: float) -> float
62 
63  Invoked with: 4
64  """
65  )
66 
67  assert m.ints_preferred(4) == 2
68  assert m.ints_preferred(True) == 0
69  with pytest.raises(TypeError) as excinfo:
70  m.ints_preferred(4.0)
71  assert (
72  msg(excinfo.value)
73  == """
74  ints_preferred(): incompatible function arguments. The following argument types are supported:
75  1. (i: int) -> int
76 
77  Invoked with: 4.0
78  """
79  )
80 
81  assert m.ints_only(4) == 2
82  with pytest.raises(TypeError) as excinfo:
83  m.ints_only(4.0)
84  assert (
85  msg(excinfo.value)
86  == """
87  ints_only(): incompatible function arguments. The following argument types are supported:
88  1. (i: int) -> int
89 
90  Invoked with: 4.0
91  """
92  )
93 
94 
96  """Tests that returning a pointer to a type that gets converted with a custom type caster gets
97  destroyed when the function has py::return_value_policy::take_ownership policy applied."""
98 
99  cstats = m.destruction_tester_cstats()
100  # This one *doesn't* have take_ownership: the pointer should be used but not destroyed:
101  z = m.custom_caster_no_destroy()
102  assert cstats.alive() == 1 and cstats.default_constructions == 1
103  assert z
104 
105  # take_ownership applied: this constructs a new object, casts it, then destroys it:
106  z = m.custom_caster_destroy()
107  assert z
108  assert cstats.default_constructions == 2
109 
110  # Same, but with a const pointer return (which should *not* inhibit destruction):
111  z = m.custom_caster_destroy_const()
112  assert z
113  assert cstats.default_constructions == 3
114 
115  # Make sure we still only have the original object (from ..._no_destroy()) alive:
116  assert cstats.alive() == 1
117 
118 
120  assert m.other_lib_type(True)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:45