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()
137 @pytest.mark.skipif(
not hasattr(m,
"has_exp_optional"), reason=
'no <experimental/optional>')
139 assert m.double_or_zero_exp(
None) == 0
140 assert m.double_or_zero_exp(42) == 84
141 pytest.raises(TypeError, m.double_or_zero_exp,
'foo')
143 assert m.half_or_none_exp(0)
is None 144 assert m.half_or_none_exp(42) == 21
145 pytest.raises(TypeError, m.half_or_none_exp,
'foo')
147 assert m.test_nullopt_exp() == 42
148 assert m.test_nullopt_exp(
None) == 42
149 assert m.test_nullopt_exp(42) == 42
150 assert m.test_nullopt_exp(43) == 43
152 assert m.test_no_assign_exp() == 42
153 assert m.test_no_assign_exp(
None) == 42
154 assert m.test_no_assign_exp(m.NoAssign(43)) == 43
155 pytest.raises(TypeError, m.test_no_assign_exp, 43)
157 holder = m.OptionalExpHolder()
158 mvalue = holder.member
159 assert mvalue.initialized
160 assert holder.member_initialized()
163 @pytest.mark.skipif(
not hasattr(m,
"load_variant"), reason=
'no <variant>')
165 assert m.load_variant(1) ==
"int" 166 assert m.load_variant(
"1") ==
"std::string" 167 assert m.load_variant(1.0) ==
"double" 168 assert m.load_variant(
None) ==
"std::nullptr_t" 170 assert m.load_variant_2pass(1) ==
"int" 171 assert m.load_variant_2pass(1.0) ==
"double" 173 assert m.cast_variant() == (5,
"Hello")
175 assert doc(m.load_variant) ==
"load_variant(arg0: Union[int, str, float, None]) -> str" 179 """#171: Can't return reference wrappers (or STL structures containing them)""" 180 assert str(m.return_vec_of_reference_wrapper(UserType(4))) == \
181 "[UserType(1), UserType(2), UserType(3), UserType(4)]" 185 """Passing nullptr or None to an STL container pointer is not expected to work""" 186 with pytest.raises(TypeError)
as excinfo:
187 m.stl_pass_by_pointer()
188 assert msg(excinfo.value) ==
""" 189 stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported: 190 1. (v: List[int] = None) -> List[int] 195 with pytest.raises(TypeError)
as excinfo:
196 m.stl_pass_by_pointer(
None)
197 assert msg(excinfo.value) ==
""" 198 stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported: 199 1. (v: List[int] = None) -> List[int] 204 assert m.stl_pass_by_pointer([1, 2, 3]) == [1, 2, 3]
208 """Trying convert `list` to a `std::vector`, or vice versa, without including 209 <pybind11/stl.h> should result in a helpful suggestion in the error message""" 210 import pybind11_cross_module_tests
as cm
212 expected_message = (
"Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,\n" 213 "<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic\n" 214 "conversions are optional and require extra headers to be included\n" 215 "when compiling your pybind11 module.")
217 with pytest.raises(TypeError)
as excinfo:
218 cm.missing_header_arg([1.0, 2.0, 3.0])
219 assert expected_message
in str(excinfo.value)
221 with pytest.raises(TypeError)
as excinfo:
222 cm.missing_header_return()
223 assert expected_message
in str(excinfo.value)
227 """Check if a string is NOT implicitly converted to a list, which was the 228 behavior before fix of issue #1258""" 229 assert m.func_with_string_or_vector_string_arg_overload((
'A',
'B', )) == 2
230 assert m.func_with_string_or_vector_string_arg_overload([
'A',
'B']) == 2
231 assert m.func_with_string_or_vector_string_arg_overload(
'A') == 3
236 assert cstats.alive() == 0
237 r = m.test_stl_ownership()
240 assert cstats.alive() == 0
244 assert m.array_cast_sequence((1, 2, 3)) == [1, 2, 3]
248 """ check fix for issue #1561 """ 249 bar = m.Issue1561Outer()
250 bar.list = [m.Issue1561Inner(
'bar')]
252 assert bar.list[0].data ==
'bar'
bool hasattr(handle obj, handle name)
def test_stl_pass_by_pointer(msg)
Annotation for documentation.
def test_function_with_string_and_vector_string_arg()
static ConstructorStats & get(std::type_index type)
def test_array_cast_sequence()
def test_vec_of_reference_wrapper()
def test_move_out_container()
def test_recursive_casting()
def test_missing_header_message()