1 from __future__
import annotations
4 from pytest
import approx
6 from pybind11_tests
import ConstructorStats
7 from pybind11_tests
import sequences_and_iterators
as m
11 assert m.make_forward_slice_size_t() ==
slice(0, -1, 1)
12 assert m.make_reversed_slice_object() ==
slice(
None,
None, -1)
15 @pytest.mark.skipif(
not m.has_optional, reason=
"no <optional>")
17 assert m.make_reversed_slice_size_t_optional() ==
slice(
None,
None, -1)
18 assert m.make_reversed_slice_size_t_optional_verbose() ==
slice(
None,
None, -1)
22 assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero()) == [(1, 2), (3, 4)]
23 assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero()) == [(1, 2)]
24 assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero()) == []
26 assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_keys()) == [1, 3]
27 assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1]
28 assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == []
30 assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_values()) == [2, 4]
31 assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_values()) == [2]
32 assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_values()) == []
35 it = m.IntPairs([(0, 0)]).nonzero()
37 with pytest.raises(StopIteration):
40 it = m.IntPairs([(0, 0)]).nonzero_keys()
42 with pytest.raises(StopIteration):
47 pairs = m.IntPairs([(1, 2), (3, 4), (0, 5)])
48 assert list(pairs.nonref()) == [(1, 2), (3, 4), (0, 5)]
49 assert list(pairs.nonref_keys()) == [1, 3, 0]
50 assert list(pairs.nonref_values()) == [2, 4, 5]
54 assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_iterator()) == [
59 assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_keys()) == [1, 3, 0]
60 assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_values()) == [2, 4, 5]
64 assert m.IntPairs.nonref.__doc__.endswith(
"-> Iterator[tuple[int, int]]\n")
65 assert m.IntPairs.nonref_keys.__doc__.endswith(
"-> Iterator[int]\n")
66 assert m.IntPairs.nonref_values.__doc__.endswith(
"-> Iterator[int]\n")
67 assert m.IntPairs.simple_iterator.__doc__.endswith(
"-> Iterator[tuple[int, int]]\n")
68 assert m.IntPairs.simple_keys.__doc__.endswith(
"-> Iterator[int]\n")
69 assert m.IntPairs.simple_values.__doc__.endswith(
"-> Iterator[int]\n")
73 """Test that iterators reference rather than copy their referents."""
74 vec = m.VectorNonCopyableInt()
77 assert [
int(x)
for x
in vec] == [3, 5]
81 assert [
int(x)
for x
in vec] == [4, 6]
83 vec = m.VectorNonCopyableIntPair()
86 assert [
int(x)
for x
in vec.keys()] == [3, 5]
87 assert [
int(x)
for x
in vec.values()] == [4, 7]
90 for x
in vec.values():
92 assert [
int(x)
for x
in vec.keys()] == [4, 6]
93 assert [
int(x)
for x
in vec.values()] == [14, 17]
97 sliceable = m.Sliceable(100)
98 assert sliceable[::] == (0, 100, 1)
99 assert sliceable[10::] == (10, 100, 1)
100 assert sliceable[:10:] == (0, 10, 1)
101 assert sliceable[::10] == (0, 100, 10)
102 assert sliceable[-10::] == (90, 100, 1)
103 assert sliceable[:-10:] == (0, 90, 1)
104 assert sliceable[::-10] == (99, -1, -10)
105 assert sliceable[50:60:1] == (50, 60, 1)
106 assert sliceable[50:60:-1] == (50, 60, -1)
113 assert cstats.values() == [
"of size",
"5"]
115 assert "Sequence" in repr(s)
119 assert 12.34
not in s
120 s[0], s[3] = 12.34, 56.78
122 assert s[0] == approx(12.34, rel=1e-05)
123 assert s[3] == approx(56.78, rel=1e-05)
126 assert cstats.values() == [
"of size",
"5"]
129 assert cstats.values() == [
"of size",
"5"]
131 it =
iter(m.Sequence(0))
133 with pytest.raises(StopIteration):
135 assert cstats.values() == [
"of size",
"0"]
137 expected = [0, 56.78, 0, 0, 12.34]
138 assert rev == approx(expected, rel=1e-05)
139 assert rev2 == approx(expected, rel=1e-05)
142 rev[0::2] = m.Sequence([2.0, 2.0, 2.0])
143 assert cstats.values() == [
"of size",
"3",
"from std::vector"]
145 assert rev == approx([2, 56.78, 2, 0, 2], rel=1e-05)
147 assert cstats.alive() == 4
149 assert cstats.alive() == 3
151 assert cstats.alive() == 2
153 assert cstats.alive() == 1
155 assert cstats.alive() == 0
157 assert cstats.values() == []
158 assert cstats.default_constructions == 0
159 assert cstats.copy_constructions == 0
160 assert cstats.move_constructions >= 1
161 assert cstats.copy_assignments == 0
162 assert cstats.move_assignments == 0
166 """#2076: Exception raised by len(arg) should be propagated"""
168 class BadLen(RuntimeError):
172 def __getitem__(self, i):
178 with pytest.raises(BadLen):
179 m.sequence_length(SequenceLike())
181 assert m.sequence_length([1, 2, 3]) == 3
182 assert m.sequence_length(
"hello") == 5
186 assert m.sequence_length.__doc__.strip() ==
"sequence_length(arg0: Sequence) -> int"
190 sm = m.StringMap({
"hi":
"bye",
"black":
"white"})
191 assert sm[
"hi"] ==
"bye"
193 assert sm[
"black"] ==
"white"
195 with pytest.raises(KeyError):
197 sm[
"orange"] =
"banana"
198 assert sm[
"orange"] ==
"banana"
200 expected = {
"hi":
"bye",
"black":
"white",
"orange":
"banana"}
202 assert sm[k] == expected[k]
203 for k, v
in sm.items():
204 assert v == expected[k]
205 assert list(sm.values()) == [expected[k]
for k
in sm]
207 it =
iter(m.StringMap({}))
209 with pytest.raises(StopIteration):
215 assert m.object_to_list(t) == [1, 2, 3]
216 assert m.object_to_list(
iter(t)) == [1, 2, 3]
217 assert m.iterator_to_list(
iter(t)) == [1, 2, 3]
219 with pytest.raises(TypeError)
as excinfo:
221 assert "object is not iterable" in str(excinfo.value)
223 with pytest.raises(TypeError)
as excinfo:
224 m.iterator_to_list(1)
225 assert "incompatible function arguments" in str(excinfo.value)
228 raise RuntimeError(
"py::iterator::advance() should propagate errors")
230 with pytest.raises(RuntimeError)
as excinfo:
231 m.iterator_to_list(
iter(bad_next_call,
None))
232 assert str(excinfo.value) ==
"py::iterator::advance() should propagate errors"
234 lst = [1,
None, 0,
None]
235 assert m.count_none(lst) == 2
236 assert m.find_none(lst)
is True
237 assert m.count_nonzeros({
"a": 0,
"b": 1,
"c": 2}) == 2
240 assert all(m.tuple_iterator(
tuple(r)))
241 assert all(m.list_iterator(
list(r)))
242 assert all(m.sequence_iterator(r))
246 """#181: iterator passthrough did not compile"""
247 from pybind11_tests.sequences_and_iterators
import iterator_passthrough
249 values = [3, 5, 7, 9, 11, 13, 15]
250 assert list(iterator_passthrough(
iter(values))) == values
254 """#388: Can't make iterators via make_iterator() with different r/v policies"""
255 import pybind11_tests.sequences_and_iterators
as m
257 assert list(m.make_iterator_1()) == [1, 2, 3]
258 assert list(m.make_iterator_2()) == [1, 2, 3]
259 assert not isinstance(m.make_iterator_1(),
type(m.make_iterator_2()))
263 """#4100: Check for proper iterator overload with C-Arrays"""
265 arr_h = m.CArrayHolder(*args_gt)
267 assert args_gt == args