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


gtsam
Author(s):
autogenerated on Thu Jul 4 2024 03:06:00