4 from pybind11_tests
import stl
as m
5 from pybind11_tests
import UserType
6 from pybind11_tests
import ConstructorStats
10 """std::vector <-> list"""
14 assert m.load_vector(lst)
15 assert m.load_vector(
tuple(lst))
17 assert m.cast_bool_vector() == [
True,
False]
18 assert m.load_bool_vector([
True,
False])
20 assert doc(m.cast_vector) ==
"cast_vector() -> List[int]"
21 assert doc(m.load_vector) ==
"load_vector(arg0: List[int]) -> bool"
24 assert m.cast_ptr_vector() == [
"lvalue",
"lvalue"]
28 """std::deque <-> list"""
32 assert m.load_deque(lst)
33 assert m.load_deque(
tuple(lst))
37 """std::array <-> list"""
40 assert m.load_array(lst)
42 assert doc(m.cast_array) ==
"cast_array() -> List[int[2]]"
43 assert doc(m.load_array) ==
"load_array(arg0: List[int[2]]) -> bool"
47 """std::valarray <-> list"""
48 lst = m.cast_valarray()
49 assert lst == [1, 4, 9]
50 assert m.load_valarray(lst)
52 assert doc(m.cast_valarray) ==
"cast_valarray() -> List[int]"
53 assert doc(m.load_valarray) ==
"load_valarray(arg0: List[int]) -> bool"
57 """std::map <-> dict"""
59 assert d == {
"key":
"value"}
65 assert doc(m.cast_map) ==
"cast_map() -> Dict[str, str]"
66 assert doc(m.load_map) ==
"load_map(arg0: Dict[str, str]) -> bool"
70 """std::set <-> set"""
72 assert s == {
"key1",
"key2"}
76 assert doc(m.cast_set) ==
"cast_set() -> Set[str]"
77 assert doc(m.load_set) ==
"load_set(arg0: Set[str]) -> bool"
81 """Tests that stl casters preserve lvalue/rvalue context for container values"""
82 assert m.cast_rv_vector() == [
"rvalue",
"rvalue"]
83 assert m.cast_lv_vector() == [
"lvalue",
"lvalue"]
84 assert m.cast_rv_array() == [
"rvalue",
"rvalue",
"rvalue"]
85 assert m.cast_lv_array() == [
"lvalue",
"lvalue"]
86 assert m.cast_rv_map() == {
"a":
"rvalue"}
87 assert m.cast_lv_map() == {
"a":
"lvalue",
"b":
"lvalue"}
88 assert m.cast_rv_nested() == [[[{
"b":
"rvalue",
"c":
"rvalue"}], [{
"a":
"rvalue"}]]]
89 assert m.cast_lv_nested() == {
90 "a": [[[
"lvalue",
"lvalue"]], [[
"lvalue",
"lvalue"]]],
91 "b": [[[
"lvalue",
"lvalue"], [
"lvalue",
"lvalue"]]],
95 z = m.cast_unique_ptr_vector()
96 assert z[0].value == 7
and z[1].value == 42
100 """Properties use the `reference_internal` policy by default. If the underlying function
101 returns an rvalue, the policy is automatically changed to `move` to avoid referencing
102 a temporary. In case the return value is a container of user-defined types, the policy
103 also needs to be applied to the elements, not just the container."""
104 c = m.MoveOutContainer()
105 moved_out_list = c.move_list
106 assert [x.value
for x
in moved_out_list] == [0, 1, 2]
109 @pytest.mark.skipif(
not hasattr(m,
"has_optional"), reason=
"no <optional>")
111 assert m.double_or_zero(
None) == 0
112 assert m.double_or_zero(42) == 84
113 pytest.raises(TypeError, m.double_or_zero,
"foo")
115 assert m.half_or_none(0)
is None
116 assert m.half_or_none(42) == 21
117 pytest.raises(TypeError, m.half_or_none,
"foo")
119 assert m.test_nullopt() == 42
120 assert m.test_nullopt(
None) == 42
121 assert m.test_nullopt(42) == 42
122 assert m.test_nullopt(43) == 43
124 assert m.test_no_assign() == 42
125 assert m.test_no_assign(
None) == 42
126 assert m.test_no_assign(m.NoAssign(43)) == 43
127 pytest.raises(TypeError, m.test_no_assign, 43)
129 assert m.nodefer_none_optional(
None)
131 holder = m.OptionalHolder()
132 mvalue = holder.member
133 assert mvalue.initialized
134 assert holder.member_initialized()
138 not hasattr(m,
"has_exp_optional"), reason=
"no <experimental/optional>"
141 assert m.double_or_zero_exp(
None) == 0
142 assert m.double_or_zero_exp(42) == 84
143 pytest.raises(TypeError, m.double_or_zero_exp,
"foo")
145 assert m.half_or_none_exp(0)
is None
146 assert m.half_or_none_exp(42) == 21
147 pytest.raises(TypeError, m.half_or_none_exp,
"foo")
149 assert m.test_nullopt_exp() == 42
150 assert m.test_nullopt_exp(
None) == 42
151 assert m.test_nullopt_exp(42) == 42
152 assert m.test_nullopt_exp(43) == 43
154 assert m.test_no_assign_exp() == 42
155 assert m.test_no_assign_exp(
None) == 42
156 assert m.test_no_assign_exp(m.NoAssign(43)) == 43
157 pytest.raises(TypeError, m.test_no_assign_exp, 43)
159 holder = m.OptionalExpHolder()
160 mvalue = holder.member
161 assert mvalue.initialized
162 assert holder.member_initialized()
165 @pytest.mark.skipif(
not hasattr(m,
"load_variant"), reason=
"no <variant>")
167 assert m.load_variant(1) ==
"int"
168 assert m.load_variant(
"1") ==
"std::string"
169 assert m.load_variant(1.0) ==
"double"
170 assert m.load_variant(
None) ==
"std::nullptr_t"
172 assert m.load_variant_2pass(1) ==
"int"
173 assert m.load_variant_2pass(1.0) ==
"double"
175 assert m.cast_variant() == (5,
"Hello")
178 doc(m.load_variant) ==
"load_variant(arg0: Union[int, str, float, None]) -> str"
183 """#171: Can't return reference wrappers (or STL structures containing them)"""
185 str(m.return_vec_of_reference_wrapper(UserType(4)))
186 ==
"[UserType(1), UserType(2), UserType(3), UserType(4)]"
191 """Passing nullptr or None to an STL container pointer is not expected to work"""
192 with pytest.raises(TypeError)
as excinfo:
193 m.stl_pass_by_pointer()
197 stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
198 1. (v: List[int] = None) -> List[int]
204 with pytest.raises(TypeError)
as excinfo:
205 m.stl_pass_by_pointer(
None)
209 stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
210 1. (v: List[int] = None) -> List[int]
216 assert m.stl_pass_by_pointer([1, 2, 3]) == [1, 2, 3]
220 """Trying convert `list` to a `std::vector`, or vice versa, without including
221 <pybind11/stl.h> should result in a helpful suggestion in the error message"""
222 import pybind11_cross_module_tests
as cm
225 "Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,\n"
226 "<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic\n"
227 "conversions are optional and require extra headers to be included\n"
228 "when compiling your pybind11 module."
231 with pytest.raises(TypeError)
as excinfo:
232 cm.missing_header_arg([1.0, 2.0, 3.0])
233 assert expected_message
in str(excinfo.value)
235 with pytest.raises(TypeError)
as excinfo:
236 cm.missing_header_return()
237 assert expected_message
in str(excinfo.value)
241 """Check if a string is NOT implicitly converted to a list, which was the
242 behavior before fix of issue #1258"""
243 assert m.func_with_string_or_vector_string_arg_overload((
"A",
"B")) == 2
244 assert m.func_with_string_or_vector_string_arg_overload([
"A",
"B"]) == 2
245 assert m.func_with_string_or_vector_string_arg_overload(
"A") == 3
250 assert cstats.alive() == 0
251 r = m.test_stl_ownership()
254 assert cstats.alive() == 0
258 assert m.array_cast_sequence((1, 2, 3)) == [1, 2, 3]
262 """ check fix for issue #1561 """
263 bar = m.Issue1561Outer()
264 bar.list = [m.Issue1561Inner(
"bar")]
266 assert bar.list[0].data ==
"bar"