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 
100  cstats = m.destruction_tester_cstats()
101  # This one *doesn't* have take_ownership: the pointer should be used but not destroyed:
102  z = m.custom_caster_no_destroy()
103  assert cstats.alive() == 1
104  assert cstats.default_constructions == 1
105  assert z
106 
107  # take_ownership applied: this constructs a new object, casts it, then destroys it:
108  z = m.custom_caster_destroy()
109  assert z
110  assert cstats.default_constructions == 2
111 
112  # Same, but with a const pointer return (which should *not* inhibit destruction):
113  z = m.custom_caster_destroy_const()
114  assert z
115  assert cstats.default_constructions == 3
116 
117  # Make sure we still only have the original object (from ..._no_destroy()) alive:
118  assert cstats.alive() == 1
119 
120 
122  assert m.other_lib_type(True)
test_custom_type_casters.test_custom_caster_destruction
def test_custom_caster_destruction()
Definition: test_custom_type_casters.py:95
test_custom_type_casters.test_noconvert_args
def test_noconvert_args(msg)
Definition: test_custom_type_casters.py:6
test_custom_type_casters.test_custom_caster_other_lib
def test_custom_caster_other_lib()
Definition: test_custom_type_casters.py:121
pybind11.msg
msg
Definition: wrap/pybind11/pybind11/__init__.py:4


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:05:28