summaryrefslogtreecommitdiff
path: root/ext/pybind11/tests/test_sequences_and_iterators.py
diff options
context:
space:
mode:
Diffstat (limited to 'ext/pybind11/tests/test_sequences_and_iterators.py')
-rw-r--r--ext/pybind11/tests/test_sequences_and_iterators.py87
1 files changed, 60 insertions, 27 deletions
diff --git a/ext/pybind11/tests/test_sequences_and_iterators.py b/ext/pybind11/tests/test_sequences_and_iterators.py
index 30b6aaf4b..640ca07bd 100644
--- a/ext/pybind11/tests/test_sequences_and_iterators.py
+++ b/ext/pybind11/tests/test_sequences_and_iterators.py
@@ -1,4 +1,6 @@
import pytest
+from pybind11_tests import sequences_and_iterators as m
+from pybind11_tests import ConstructorStats
def isclose(a, b, rel_tol=1e-05, abs_tol=0.0):
@@ -11,24 +13,30 @@ def allclose(a_list, b_list, rel_tol=1e-05, abs_tol=0.0):
def test_generalized_iterators():
- from pybind11_tests.sequences_and_iterators import IntPairs
+ assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero()) == [(1, 2), (3, 4)]
+ assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero()) == [(1, 2)]
+ assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero()) == []
- assert list(IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero()) == [(1, 2), (3, 4)]
- assert list(IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero()) == [(1, 2)]
- assert list(IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero()) == []
+ assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_keys()) == [1, 3]
+ assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1]
+ assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == []
- assert list(IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_keys()) == [1, 3]
- assert list(IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1]
- assert list(IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == []
+ # __next__ must continue to raise StopIteration
+ it = m.IntPairs([(0, 0)]).nonzero()
+ for _ in range(3):
+ with pytest.raises(StopIteration):
+ next(it)
+ it = m.IntPairs([(0, 0)]).nonzero_keys()
+ for _ in range(3):
+ with pytest.raises(StopIteration):
+ next(it)
-def test_sequence():
- from pybind11_tests import ConstructorStats
- from pybind11_tests.sequences_and_iterators import Sequence
- cstats = ConstructorStats.get(Sequence)
+def test_sequence():
+ cstats = ConstructorStats.get(m.Sequence)
- s = Sequence(5)
+ s = m.Sequence(5)
assert cstats.values() == ['of size', '5']
assert "Sequence" in repr(s)
@@ -45,16 +53,24 @@ def test_sequence():
rev2 = s[::-1]
assert cstats.values() == ['of size', '5']
+ it = iter(m.Sequence(0))
+ for _ in range(3): # __next__ must continue to raise StopIteration
+ with pytest.raises(StopIteration):
+ next(it)
+ assert cstats.values() == ['of size', '0']
+
expected = [0, 56.78, 0, 0, 12.34]
assert allclose(rev, expected)
assert allclose(rev2, expected)
assert rev == rev2
- rev[0::2] = Sequence([2.0, 2.0, 2.0])
+ rev[0::2] = m.Sequence([2.0, 2.0, 2.0])
assert cstats.values() == ['of size', '3', 'from std::vector']
assert allclose(rev, [2, 56.78, 2, 0, 2])
+ assert cstats.alive() == 4
+ del it
assert cstats.alive() == 3
del s
assert cstats.alive() == 2
@@ -72,28 +88,29 @@ def test_sequence():
def test_map_iterator():
- from pybind11_tests.sequences_and_iterators import StringMap
-
- m = StringMap({'hi': 'bye', 'black': 'white'})
- assert m['hi'] == 'bye'
- assert len(m) == 2
- assert m['black'] == 'white'
+ sm = m.StringMap({'hi': 'bye', 'black': 'white'})
+ assert sm['hi'] == 'bye'
+ assert len(sm) == 2
+ assert sm['black'] == 'white'
with pytest.raises(KeyError):
- assert m['orange']
- m['orange'] = 'banana'
- assert m['orange'] == 'banana'
+ assert sm['orange']
+ sm['orange'] = 'banana'
+ assert sm['orange'] == 'banana'
expected = {'hi': 'bye', 'black': 'white', 'orange': 'banana'}
- for k in m:
- assert m[k] == expected[k]
- for k, v in m.items():
+ for k in sm:
+ assert sm[k] == expected[k]
+ for k, v in sm.items():
assert v == expected[k]
+ it = iter(m.StringMap({}))
+ for _ in range(3): # __next__ must continue to raise StopIteration
+ with pytest.raises(StopIteration):
+ next(it)
-def test_python_iterator_in_cpp():
- import pybind11_tests.sequences_and_iterators as m
+def test_python_iterator_in_cpp():
t = (1, 2, 3)
assert m.object_to_list(t) == [1, 2, 3]
assert m.object_to_list(iter(t)) == [1, 2, 3]
@@ -123,3 +140,19 @@ def test_python_iterator_in_cpp():
assert all(m.tuple_iterator(tuple(r)))
assert all(m.list_iterator(list(r)))
assert all(m.sequence_iterator(r))
+
+
+def test_iterator_passthrough():
+ """#181: iterator passthrough did not compile"""
+ from pybind11_tests.sequences_and_iterators import iterator_passthrough
+
+ assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]
+
+
+def test_iterator_rvp():
+ """#388: Can't make iterators via make_iterator() with different r/v policies """
+ import pybind11_tests.sequences_and_iterators as m
+
+ assert list(m.make_iterator_1()) == [1, 2, 3]
+ assert list(m.make_iterator_2()) == [1, 2, 3]
+ assert not isinstance(m.make_iterator_1(), type(m.make_iterator_2()))