3 from pybind11_tests
import ConstructorStats, UserType
4 from pybind11_tests
import stl
as m
8 """std::vector <-> list"""
12 assert m.load_vector(lst)
13 assert m.load_vector(
tuple(lst))
15 assert m.cast_bool_vector() == [
True,
False]
16 assert m.load_bool_vector([
True,
False])
17 assert m.load_bool_vector((
True,
False))
19 assert doc(m.cast_vector) ==
"cast_vector() -> List[int]"
20 assert doc(m.load_vector) ==
"load_vector(arg0: List[int]) -> bool"
23 assert m.cast_ptr_vector() == [
"lvalue",
"lvalue"]
27 """std::deque <-> list"""
31 assert m.load_deque(lst)
32 assert m.load_deque(
tuple(lst))
36 """std::array <-> list"""
39 assert m.load_array(lst)
40 assert m.load_array(
tuple(lst))
42 assert doc(m.cast_array) ==
"cast_array() -> Annotated[List[int], FixedSize(2)]"
45 ==
"load_array(arg0: Annotated[List[int], FixedSize(2)]) -> bool"
50 """std::valarray <-> list"""
51 lst = m.cast_valarray()
52 assert lst == [1, 4, 9]
53 assert m.load_valarray(lst)
54 assert m.load_valarray(
tuple(lst))
56 assert doc(m.cast_valarray) ==
"cast_valarray() -> List[int]"
57 assert doc(m.load_valarray) ==
"load_valarray(arg0: List[int]) -> bool"
61 """std::map <-> dict"""
63 assert d == {
"key":
"value"}
69 assert doc(m.cast_map) ==
"cast_map() -> Dict[str, str]"
70 assert doc(m.load_map) ==
"load_map(arg0: Dict[str, str]) -> bool"
74 """std::set <-> set"""
76 assert s == {
"key1",
"key2"}
81 assert doc(m.cast_set) ==
"cast_set() -> Set[str]"
82 assert doc(m.load_set) ==
"load_set(arg0: Set[str]) -> bool"
86 """Tests that stl casters preserve lvalue/rvalue context for container values"""
87 assert m.cast_rv_vector() == [
"rvalue",
"rvalue"]
88 assert m.cast_lv_vector() == [
"lvalue",
"lvalue"]
89 assert m.cast_rv_array() == [
"rvalue",
"rvalue",
"rvalue"]
90 assert m.cast_lv_array() == [
"lvalue",
"lvalue"]
91 assert m.cast_rv_map() == {
"a":
"rvalue"}
92 assert m.cast_lv_map() == {
"a":
"lvalue",
"b":
"lvalue"}
93 assert m.cast_rv_nested() == [[[{
"b":
"rvalue",
"c":
"rvalue"}], [{
"a":
"rvalue"}]]]
94 assert m.cast_lv_nested() == {
95 "a": [[[
"lvalue",
"lvalue"]], [[
"lvalue",
"lvalue"]]],
96 "b": [[[
"lvalue",
"lvalue"], [
"lvalue",
"lvalue"]]],
100 z = m.cast_unique_ptr_vector()
101 assert z[0].value == 7
102 assert z[1].value == 42
106 """Properties use the `reference_internal` policy by default. If the underlying function
107 returns an rvalue, the policy is automatically changed to `move` to avoid referencing
108 a temporary. In case the return value is a container of user-defined types, the policy
109 also needs to be applied to the elements, not just the container."""
110 c = m.MoveOutContainer()
111 moved_out_list = c.move_list
112 assert [x.value
for x
in moved_out_list] == [0, 1, 2]
115 @pytest.mark.skipif(
not hasattr(m,
"has_optional"), reason=
"no <optional>")
117 assert m.double_or_zero(
None) == 0
118 assert m.double_or_zero(42) == 84
119 pytest.raises(TypeError, m.double_or_zero,
"foo")
121 assert m.half_or_none(0)
is None
122 assert m.half_or_none(42) == 21
123 pytest.raises(TypeError, m.half_or_none,
"foo")
125 assert m.test_nullopt() == 42
126 assert m.test_nullopt(
None) == 42
127 assert m.test_nullopt(42) == 42
128 assert m.test_nullopt(43) == 43
130 assert m.test_no_assign() == 42
131 assert m.test_no_assign(
None) == 42
132 assert m.test_no_assign(m.NoAssign(43)) == 43
133 pytest.raises(TypeError, m.test_no_assign, 43)
135 assert m.nodefer_none_optional(
None)
137 holder = m.OptionalHolder()
138 mvalue = holder.member
139 assert mvalue.initialized
140 assert holder.member_initialized()
142 props = m.OptionalProperties()
143 assert int(props.access_by_ref) == 42
144 assert int(props.access_by_copy) == 42
148 not hasattr(m,
"has_exp_optional"), reason=
"no <experimental/optional>"
151 assert m.double_or_zero_exp(
None) == 0
152 assert m.double_or_zero_exp(42) == 84
153 pytest.raises(TypeError, m.double_or_zero_exp,
"foo")
155 assert m.half_or_none_exp(0)
is None
156 assert m.half_or_none_exp(42) == 21
157 pytest.raises(TypeError, m.half_or_none_exp,
"foo")
159 assert m.test_nullopt_exp() == 42
160 assert m.test_nullopt_exp(
None) == 42
161 assert m.test_nullopt_exp(42) == 42
162 assert m.test_nullopt_exp(43) == 43
164 assert m.test_no_assign_exp() == 42
165 assert m.test_no_assign_exp(
None) == 42
166 assert m.test_no_assign_exp(m.NoAssign(43)) == 43
167 pytest.raises(TypeError, m.test_no_assign_exp, 43)
169 holder = m.OptionalExpHolder()
170 mvalue = holder.member
171 assert mvalue.initialized
172 assert holder.member_initialized()
174 props = m.OptionalExpProperties()
175 assert int(props.access_by_ref) == 42
176 assert int(props.access_by_copy) == 42
179 @pytest.mark.skipif(
not hasattr(m,
"has_boost_optional"), reason=
"no <boost/optional>")
181 assert m.double_or_zero_boost(
None) == 0
182 assert m.double_or_zero_boost(42) == 84
183 pytest.raises(TypeError, m.double_or_zero_boost,
"foo")
185 assert m.half_or_none_boost(0)
is None
186 assert m.half_or_none_boost(42) == 21
187 pytest.raises(TypeError, m.half_or_none_boost,
"foo")
189 assert m.test_nullopt_boost() == 42
190 assert m.test_nullopt_boost(
None) == 42
191 assert m.test_nullopt_boost(42) == 42
192 assert m.test_nullopt_boost(43) == 43
194 assert m.test_no_assign_boost() == 42
195 assert m.test_no_assign_boost(
None) == 42
196 assert m.test_no_assign_boost(m.NoAssign(43)) == 43
197 pytest.raises(TypeError, m.test_no_assign_boost, 43)
199 holder = m.OptionalBoostHolder()
200 mvalue = holder.member
201 assert mvalue.initialized
202 assert holder.member_initialized()
204 props = m.OptionalBoostProperties()
205 assert int(props.access_by_ref) == 42
206 assert int(props.access_by_copy) == 42
210 assert m.double_or_zero_refsensitive(
None) == 0
211 assert m.double_or_zero_refsensitive(42) == 84
212 pytest.raises(TypeError, m.double_or_zero_refsensitive,
"foo")
214 assert m.half_or_none_refsensitive(0)
is None
215 assert m.half_or_none_refsensitive(42) == 21
216 pytest.raises(TypeError, m.half_or_none_refsensitive,
"foo")
218 assert m.test_nullopt_refsensitive() == 42
219 assert m.test_nullopt_refsensitive(
None) == 42
220 assert m.test_nullopt_refsensitive(42) == 42
221 assert m.test_nullopt_refsensitive(43) == 43
223 assert m.test_no_assign_refsensitive() == 42
224 assert m.test_no_assign_refsensitive(
None) == 42
225 assert m.test_no_assign_refsensitive(m.NoAssign(43)) == 43
226 pytest.raises(TypeError, m.test_no_assign_refsensitive, 43)
228 holder = m.OptionalRefSensitiveHolder()
229 mvalue = holder.member
230 assert mvalue.initialized
231 assert holder.member_initialized()
233 props = m.OptionalRefSensitiveProperties()
234 assert int(props.access_by_ref) == 42
235 assert int(props.access_by_copy) == 42
238 @pytest.mark.skipif(
not hasattr(m,
"has_filesystem"), reason=
"no <filesystem>")
240 from pathlib
import Path
243 def __fspath__(self):
246 class PseudoBytesPath:
247 def __fspath__(self):
250 assert m.parent_path(Path(
"foo/bar")) == Path(
"foo")
251 assert m.parent_path(
"foo/bar") == Path(
"foo")
252 assert m.parent_path(b
"foo/bar") == Path(
"foo")
253 assert m.parent_path(PseudoStrPath()) == Path(
"foo")
254 assert m.parent_path(PseudoBytesPath()) == Path(
"foo")
257 @pytest.mark.skipif(
not hasattr(m,
"load_variant"), reason=
"no <variant>")
259 assert m.load_variant(1) ==
"int"
260 assert m.load_variant(
"1") ==
"std::string"
261 assert m.load_variant(1.0) ==
"double"
262 assert m.load_variant(
None) ==
"std::nullptr_t"
264 assert m.load_variant_2pass(1) ==
"int"
265 assert m.load_variant_2pass(1.0) ==
"double"
267 assert m.cast_variant() == (5,
"Hello")
270 doc(m.load_variant) ==
"load_variant(arg0: Union[int, str, float, None]) -> str"
275 not hasattr(m,
"load_monostate_variant"), reason=
"no std::monostate"
278 assert m.load_monostate_variant(
None) ==
"std::monostate"
279 assert m.load_monostate_variant(1) ==
"int"
280 assert m.load_monostate_variant(
"1") ==
"std::string"
282 assert m.cast_monostate_variant() == (
None, 5,
"Hello")
285 doc(m.load_monostate_variant)
286 ==
"load_monostate_variant(arg0: Union[None, int, str]) -> str"
291 """#171: Can't return reference wrappers (or STL structures containing them)"""
293 str(m.return_vec_of_reference_wrapper(UserType(4)))
294 ==
"[UserType(1), UserType(2), UserType(3), UserType(4)]"
299 """Passing nullptr or None to an STL container pointer is not expected to work"""
300 with pytest.raises(TypeError)
as excinfo:
301 m.stl_pass_by_pointer()
305 stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
306 1. (v: List[int] = None) -> List[int]
312 with pytest.raises(TypeError)
as excinfo:
313 m.stl_pass_by_pointer(
None)
317 stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
318 1. (v: List[int] = None) -> List[int]
324 assert m.stl_pass_by_pointer([1, 2, 3]) == [1, 2, 3]
328 """Trying convert `list` to a `std::vector`, or vice versa, without including
329 <pybind11/stl.h> should result in a helpful suggestion in the error message"""
330 import pybind11_cross_module_tests
as cm
333 "Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,\n"
334 "<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic\n"
335 "conversions are optional and require extra headers to be included\n"
336 "when compiling your pybind11 module."
339 with pytest.raises(TypeError)
as excinfo:
340 cm.missing_header_arg([1.0, 2.0, 3.0])
341 assert expected_message
in str(excinfo.value)
343 with pytest.raises(TypeError)
as excinfo:
344 cm.missing_header_return()
345 assert expected_message
in str(excinfo.value)
349 """Check if a string is NOT implicitly converted to a list, which was the
350 behavior before fix of issue #1258"""
351 assert m.func_with_string_or_vector_string_arg_overload((
"A",
"B")) == 2
352 assert m.func_with_string_or_vector_string_arg_overload([
"A",
"B"]) == 2
353 assert m.func_with_string_or_vector_string_arg_overload(
"A") == 3
358 assert cstats.alive() == 0
359 r = m.test_stl_ownership()
362 assert cstats.alive() == 0
366 assert m.array_cast_sequence((1, 2, 3)) == [1, 2, 3]
370 """check fix for issue #1561"""
371 bar = m.Issue1561Outer()
372 bar.list = [m.Issue1561Inner(
"bar")]
374 assert bar.list[0].data ==
"bar"
379 v = m.return_vector_bool_raw_ptr()
381 assert len(v) == 4513