MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_python_iterator_in_cpp

Function test_python_iterator_in_cpp

tests/test_sequences_and_iterators.py:235–264  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

233
234
235def test_python_iterator_in_cpp():
236 t = (1, 2, 3)
237 assert m.object_to_list(t) == [1, 2, 3]
238 assert m.object_to_list(iter(t)) == [1, 2, 3]
239 assert m.iterator_to_list(iter(t)) == [1, 2, 3]
240
241 with pytest.raises(TypeError) as excinfo:
242 m.object_to_list(1)
243 assert "object is not iterable" in str(excinfo.value)
244
245 with pytest.raises(TypeError) as excinfo:
246 m.iterator_to_list(1)
247 assert "incompatible function arguments" in str(excinfo.value)
248
249 def bad_next_call():
250 raise RuntimeError("py::iterator::advance() should propagate errors")
251
252 with pytest.raises(RuntimeError) as excinfo:
253 m.iterator_to_list(iter(bad_next_call, None))
254 assert str(excinfo.value) == "py::iterator::advance() should propagate errors"
255
256 lst = [1, None, 0, None]
257 assert m.count_none(lst) == 2
258 assert m.find_none(lst) is True
259 assert m.count_nonzeros({"a": 0, "b": 1, "c": 2}) == 2
260
261 r = range(5)
262 assert all(m.tuple_iterator(tuple(r)))
263 assert all(m.list_iterator(list(r)))
264 assert all(m.sequence_iterator(r))
265
266
267def test_iterator_passthrough():

Callers

nothing calls this directly

Calls 4

iterFunction · 0.85
strClass · 0.85
tupleClass · 0.85
listClass · 0.85

Tested by

no test coverage detected