3 from pybind11_tests
import stl
as m
4 from pybind11_tests
import UserType
5 from pybind11_tests
import ConstructorStats
9 """std::vector <-> list""" 13 assert m.load_vector(lst)
14 assert m.load_vector(tuple(lst))
16 assert m.cast_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)
41 assert doc(m.cast_array) ==
"cast_array() -> List[int[2]]" 42 assert doc(m.load_array) ==
"load_array(arg0: List[int[2]]) -> bool" 46 """std::valarray <-> list""" 47 lst = m.cast_valarray()
48 assert lst == [1, 4, 9]
49 assert m.load_valarray(lst)
51 assert doc(m.cast_valarray) ==
"cast_valarray() -> List[int]" 52 assert doc(m.load_valarray) ==
"load_valarray(arg0: List[int]) -> bool" 56 """std::map <-> dict""" 58 assert d == {
"key":
"value"}
64 assert doc(m.cast_map) ==
"cast_map() -> Dict[str, str]" 65 assert doc(m.load_map) ==
"load_map(arg0: Dict[str, str]) -> bool" 69 """std::set <-> set""" 71 assert s == {
"key1",
"key2"}
75 assert doc(m.cast_set) ==
"cast_set() -> Set[str]" 76 assert doc(m.load_set) ==
"load_set(arg0: Set[str]) -> bool" 80 """Tests that stl casters preserve lvalue/rvalue context for container values""" 81 assert m.cast_rv_vector() == [
"rvalue",
"rvalue"]
82 assert m.cast_lv_vector() == [
"lvalue",
"lvalue"]
83 assert m.cast_rv_array() == [
"rvalue",
"rvalue",
"rvalue"]
84 assert m.cast_lv_array() == [
"lvalue",
"lvalue"]
85 assert m.cast_rv_map() == {
"a":
"rvalue"}
86 assert m.cast_lv_map() == {
"a":
"lvalue",
"b":
"lvalue"}
87 assert m.cast_rv_nested() == [[[{
"b":
"rvalue",
"c":
"rvalue"}], [{
"a":
"rvalue"}]]]
88 assert m.cast_lv_nested() == {
89 "a": [[[
"lvalue",
"lvalue"]], [[
"lvalue",
"lvalue"]]],
90 "b": [[[
"lvalue",
"lvalue"], [
"lvalue",
"lvalue"]]]
94 z = m.cast_unique_ptr_vector()
95 assert z[0].value == 7
and z[1].value == 42
99 """Properties use the `reference_internal` policy by default. If the underlying function 100 returns an rvalue, the policy is automatically changed to `move` to avoid referencing 101 a temporary. In case the return value is a container of user-defined types, the policy 102 also needs to be applied to the elements, not just the container.""" 103 c = m.MoveOutContainer()
104 moved_out_list = c.move_list
105 assert [x.value
for x
in moved_out_list] == [0, 1, 2]
108 @pytest.mark.skipif(
not hasattr(m,
"has_optional"), reason=
'no <optional>')
110 assert m.double_or_zero(
None) == 0
111 assert m.double_or_zero(42) == 84
112 pytest.raises(TypeError, m.double_or_zero,
'foo')
114 assert m.half_or_none(0)
is None 115 assert m.half_or_none(42) == 21
116 pytest.raises(TypeError, m.half_or_none,
'foo')
118 assert m.test_nullopt() == 42
119 assert m.test_nullopt(
None) == 42
120 assert m.test_nullopt(42) == 42
121 assert m.test_nullopt(43) == 43
123 assert m.test_no_assign() == 42
124 assert m.test_no_assign(
None) == 42
125 assert m.test_no_assign(m.NoAssign(43)) == 43
126 pytest.raises(TypeError, m.test_no_assign, 43)
128 assert m.nodefer_none_optional(
None)
131 @pytest.mark.skipif(
not hasattr(m,
"has_exp_optional"), reason=
'no <experimental/optional>')
133 assert m.double_or_zero_exp(
None) == 0
134 assert m.double_or_zero_exp(42) == 84
135 pytest.raises(TypeError, m.double_or_zero_exp,
'foo')
137 assert m.half_or_none_exp(0)
is None 138 assert m.half_or_none_exp(42) == 21
139 pytest.raises(TypeError, m.half_or_none_exp,
'foo')
141 assert m.test_nullopt_exp() == 42
142 assert m.test_nullopt_exp(
None) == 42
143 assert m.test_nullopt_exp(42) == 42
144 assert m.test_nullopt_exp(43) == 43
146 assert m.test_no_assign_exp() == 42
147 assert m.test_no_assign_exp(
None) == 42
148 assert m.test_no_assign_exp(m.NoAssign(43)) == 43
149 pytest.raises(TypeError, m.test_no_assign_exp, 43)
152 @pytest.mark.skipif(
not hasattr(m,
"load_variant"), reason=
'no <variant>')
154 assert m.load_variant(1) ==
"int" 155 assert m.load_variant(
"1") ==
"std::string" 156 assert m.load_variant(1.0) ==
"double" 157 assert m.load_variant(
None) ==
"std::nullptr_t" 159 assert m.load_variant_2pass(1) ==
"int" 160 assert m.load_variant_2pass(1.0) ==
"double" 162 assert m.cast_variant() == (5,
"Hello")
164 assert doc(m.load_variant) ==
"load_variant(arg0: Union[int, str, float, None]) -> str" 168 """#171: Can't return reference wrappers (or STL structures containing them)""" 169 assert str(m.return_vec_of_reference_wrapper(UserType(4))) == \
170 "[UserType(1), UserType(2), UserType(3), UserType(4)]" 174 """Passing nullptr or None to an STL container pointer is not expected to work""" 175 with pytest.raises(TypeError)
as excinfo:
176 m.stl_pass_by_pointer()
177 assert msg(excinfo.value) ==
""" 178 stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported: 179 1. (v: List[int] = None) -> List[int] 184 with pytest.raises(TypeError)
as excinfo:
185 m.stl_pass_by_pointer(
None)
186 assert msg(excinfo.value) ==
""" 187 stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported: 188 1. (v: List[int] = None) -> List[int] 193 assert m.stl_pass_by_pointer([1, 2, 3]) == [1, 2, 3]
197 """Trying convert `list` to a `std::vector`, or vice versa, without including 198 <pybind11/stl.h> should result in a helpful suggestion in the error message""" 199 import pybind11_cross_module_tests
as cm
201 expected_message = (
"Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,\n" 202 "<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic\n" 203 "conversions are optional and require extra headers to be included\n" 204 "when compiling your pybind11 module.")
206 with pytest.raises(TypeError)
as excinfo:
207 cm.missing_header_arg([1.0, 2.0, 3.0])
208 assert expected_message
in str(excinfo.value)
210 with pytest.raises(TypeError)
as excinfo:
211 cm.missing_header_return()
212 assert expected_message
in str(excinfo.value)
216 """Check if a string is NOT implicitly converted to a list, which was the 217 behavior before fix of issue #1258""" 218 assert m.func_with_string_or_vector_string_arg_overload((
'A',
'B', )) == 2
219 assert m.func_with_string_or_vector_string_arg_overload([
'A',
'B']) == 2
220 assert m.func_with_string_or_vector_string_arg_overload(
'A') == 3
225 assert cstats.alive() == 0
226 r = m.test_stl_ownership()
229 assert cstats.alive() == 0
233 assert m.array_cast_sequence((1, 2, 3)) == [1, 2, 3]
237 """ check fix for issue #1561 """ 238 bar = m.Issue1561Outer()
239 bar.list = [m.Issue1561Inner(
'bar')]
241 assert bar.list[0].data ==
'bar'
static ConstructorStats & get(std::type_index type)
def test_stl_pass_by_pointer(msg)
def test_function_with_string_and_vector_string_arg()
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()
bool hasattr(handle obj, handle name)