Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Failed C++ Assert is Ignored in Python #5106

Open
3 tasks done
xARTPOP opened this issue Apr 19, 2024 · 0 comments
Open
3 tasks done

[BUG]: Failed C++ Assert is Ignored in Python #5106

xARTPOP opened this issue Apr 19, 2024 · 0 comments
Labels
triage New bug, unverified

Comments

@xARTPOP
Copy link

xARTPOP commented Apr 19, 2024

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

2.12.0

Problem description

In the C++ overloading of the operator[], I've got an assert to make sure the id being accessed isn't outside the bounds of my container. This works in the C++ code, but in Python, it gives no indication of the assert failure when accessing an invalid id and still returns a value, though I'm not sure where this value is coming from. Below is a reproduction of this behavior.

Reproducible example code

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

#include <array>
#include <assert>

class foo {
public:
        std::array<int, 4> data{1, 2, 3, 4};
        size_t size = 4;

        double operator[](const size_t idx) {
                assert(idx < this->size);
                return this->data[idx];
        }
};

namespace py = pybind11;

PYBIND11_MODULE(assert_issue, m) {
        py::class_<foo>(m, "foo")
                .def(py::init<>())
                .def("__getitem__", py::overload_cast<const size_t>(&foo::operator[]));
}

Is this a regression? Put the last known working version here if it is.

Not a regression

@xARTPOP xARTPOP added the triage New bug, unverified label Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage New bug, unverified
Projects
None yet
Development

No branches or pull requests

1 participant