Skip to content

Attempting to iterate over python iterable, in c++, iterates past the end with bound c++ types. #896

Closed
@ariaTim

Description

@ariaTim

Issue

Attempting to iterate over python iterable, in c++, iterates past the end with bound c++ types. It seems that comparing the end iterator to the sentinel attempts to advance the end iterator past the end. When iterating a python type calling PyIter_Next past the end simply returns NULL however for a bound STL container this invokes undefined behaviour.

Occurs on:
Windows 10 (VS 2017), Ubuntu 17.04 (gcc 7.0)
pybind11 2.1.1 and master ( 0365d49 )

Example

cpp:

#include <pybind11/pybind11.h>
#include <pybind11/stl_bind.h>

namespace py = pybind11;

PYBIND11_MAKE_OPAQUE(std::vector<int>)

PYBIND11_MODULE(IterableTest, m)
{
    py::bind_vector<std::vector<int>>(m, "VectorInt");

    m.def("iter_test", [] (py::iterable iterable)
    {
        auto iter = py::iter(iterable);
        while (iter != py::iterator::sentinel())
        {
            py::print("got value: ", *iter);
            ++iter;
        }
    });
}

python:

from IterableTest import *

ivec = VectorInt()
ivec.append(3)
ivec.append(2)
ivec.append(1)

iter_test(ivec)

example output:

got value:  3
got value:  2
got value:  1
*debug assert iterator is not incrementable*

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions