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


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:03