2 from pytest
import approx
4 from pybind11_tests
import ConstructorStats
5 from pybind11_tests
import sequences_and_iterators
as m
9 assert m.make_forward_slice_size_t() ==
slice(0, -1, 1)
10 assert m.make_reversed_slice_object() ==
slice(
None,
None, -1)
13 @pytest.mark.skipif(
not m.has_optional, reason=
"no <optional>")
15 assert m.make_reversed_slice_size_t_optional() ==
slice(
None,
None, -1)
16 assert m.make_reversed_slice_size_t_optional_verbose() ==
slice(
None,
None, -1)
20 assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero()) == [(1, 2), (3, 4)]
21 assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero()) == [(1, 2)]
22 assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero()) == []
24 assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_keys()) == [1, 3]
25 assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1]
26 assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == []
28 assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_values()) == [2, 4]
29 assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_values()) == [2]
30 assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_values()) == []
33 it = m.IntPairs([(0, 0)]).nonzero()
35 with pytest.raises(StopIteration):
38 it = m.IntPairs([(0, 0)]).nonzero_keys()
40 with pytest.raises(StopIteration):
45 pairs = m.IntPairs([(1, 2), (3, 4), (0, 5)])
46 assert list(pairs.nonref()) == [(1, 2), (3, 4), (0, 5)]
47 assert list(pairs.nonref_keys()) == [1, 3, 0]
48 assert list(pairs.nonref_values()) == [2, 4, 5]
52 assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_iterator()) == [
57 assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_keys()) == [1, 3, 0]
58 assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_values()) == [2, 4, 5]
62 """Test that iterators reference rather than copy their referents.""" 63 vec = m.VectorNonCopyableInt()
66 assert [
int(x)
for x
in vec] == [3, 5]
70 assert [
int(x)
for x
in vec] == [4, 6]
72 vec = m.VectorNonCopyableIntPair()
75 assert [
int(x)
for x
in vec.keys()] == [3, 5]
76 assert [
int(x)
for x
in vec.values()] == [4, 7]
79 for x
in vec.values():
81 assert [
int(x)
for x
in vec.keys()] == [4, 6]
82 assert [
int(x)
for x
in vec.values()] == [14, 17]
86 sliceable = m.Sliceable(100)
87 assert sliceable[::] == (0, 100, 1)
88 assert sliceable[10::] == (10, 100, 1)
89 assert sliceable[:10:] == (0, 10, 1)
90 assert sliceable[::10] == (0, 100, 10)
91 assert sliceable[-10::] == (90, 100, 1)
92 assert sliceable[:-10:] == (0, 90, 1)
93 assert sliceable[::-10] == (99, -1, -10)
94 assert sliceable[50:60:1] == (50, 60, 1)
95 assert sliceable[50:60:-1] == (50, 60, -1)
102 assert cstats.values() == [
"of size",
"5"]
104 assert "Sequence" in repr(s)
106 assert s[0] == 0
and s[3] == 0
107 assert 12.34
not in s
108 s[0], s[3] = 12.34, 56.78
110 assert s[0] == approx(12.34, rel=1e-05)
111 assert s[3] == approx(56.78, rel=1e-05)
114 assert cstats.values() == [
"of size",
"5"]
117 assert cstats.values() == [
"of size",
"5"]
119 it =
iter(m.Sequence(0))
121 with pytest.raises(StopIteration):
123 assert cstats.values() == [
"of size",
"0"]
125 expected = [0, 56.78, 0, 0, 12.34]
126 assert rev == approx(expected, rel=1e-05)
127 assert rev2 == approx(expected, rel=1e-05)
130 rev[0::2] = m.Sequence([2.0, 2.0, 2.0])
131 assert cstats.values() == [
"of size",
"3",
"from std::vector"]
133 assert rev == approx([2, 56.78, 2, 0, 2], rel=1e-05)
135 assert cstats.alive() == 4
137 assert cstats.alive() == 3
139 assert cstats.alive() == 2
141 assert cstats.alive() == 1
143 assert cstats.alive() == 0
145 assert cstats.values() == []
146 assert cstats.default_constructions == 0
147 assert cstats.copy_constructions == 0
148 assert cstats.move_constructions >= 1
149 assert cstats.copy_assignments == 0
150 assert cstats.move_assignments == 0
154 """#2076: Exception raised by len(arg) should be propagated""" 156 class BadLen(RuntimeError):
160 def __getitem__(self, i):
166 with pytest.raises(BadLen):
167 m.sequence_length(SequenceLike())
169 assert m.sequence_length([1, 2, 3]) == 3
170 assert m.sequence_length(
"hello") == 5
174 sm = m.StringMap({
"hi":
"bye",
"black":
"white"})
175 assert sm[
"hi"] ==
"bye" 177 assert sm[
"black"] ==
"white" 179 with pytest.raises(KeyError):
181 sm[
"orange"] =
"banana" 182 assert sm[
"orange"] ==
"banana" 184 expected = {
"hi":
"bye",
"black":
"white",
"orange":
"banana"}
186 assert sm[k] == expected[k]
187 for k, v
in sm.items():
188 assert v == expected[k]
189 assert list(sm.values()) == [expected[k]
for k
in sm]
191 it =
iter(m.StringMap({}))
193 with pytest.raises(StopIteration):
199 assert m.object_to_list(t) == [1, 2, 3]
200 assert m.object_to_list(
iter(t)) == [1, 2, 3]
201 assert m.iterator_to_list(
iter(t)) == [1, 2, 3]
203 with pytest.raises(TypeError)
as excinfo:
205 assert "object is not iterable" in str(excinfo.value)
207 with pytest.raises(TypeError)
as excinfo:
208 m.iterator_to_list(1)
209 assert "incompatible function arguments" in str(excinfo.value)
212 raise RuntimeError(
"py::iterator::advance() should propagate errors")
214 with pytest.raises(RuntimeError)
as excinfo:
215 m.iterator_to_list(
iter(bad_next_call,
None))
216 assert str(excinfo.value) ==
"py::iterator::advance() should propagate errors" 218 lst = [1,
None, 0,
None]
219 assert m.count_none(lst) == 2
220 assert m.find_none(lst)
is True 221 assert m.count_nonzeros({
"a": 0,
"b": 1,
"c": 2}) == 2
224 assert all(m.tuple_iterator(
tuple(r)))
225 assert all(m.list_iterator(
list(r)))
226 assert all(m.sequence_iterator(r))
230 """#181: iterator passthrough did not compile""" 231 from pybind11_tests.sequences_and_iterators
import iterator_passthrough
233 values = [3, 5, 7, 9, 11, 13, 15]
234 assert list(iterator_passthrough(
iter(values))) == values
238 """#388: Can't make iterators via make_iterator() with different r/v policies""" 239 import pybind11_tests.sequences_and_iterators
as m
241 assert list(m.make_iterator_1()) == [1, 2, 3]
242 assert list(m.make_iterator_2()) == [1, 2, 3]
243 assert not isinstance(m.make_iterator_1(),
type(m.make_iterator_2()))
static const Eigen::internal::all_t all
def test_slice_constructors_explicit_optional()
iterator iter(handle obj)
def test_python_iterator_in_cpp()
bool isinstance(handle obj)
def test_slice_constructors()
def test_generalized_iterators()
def test_generalized_iterators_simple()
static ConstructorStats & get(std::type_index type)
def test_iterator_passthrough()
Double_ range(const Point2_ &p, const Point2_ &q)
def test_sequence_length()
def test_nonref_iterators()
size_t len(handle h)
Get the length of a Python object.
def test_iterator_referencing()