pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.39k stars 2.07k forks source link

[BUG]: Crash when casting handle to array #4765

Open cctry opened 1 year ago

cctry commented 1 year ago

Required prerequisites

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

aa304c9c7d725ffb9d10af08a3b34cb372307020

Problem description

Converting a py::handle to py::array leads to segmentation fault.

Reproducible example code

// C++ code 
void test(py::list list) {
  for (auto&& obj : list) {
    auto handle = obj.cast<py::handle>();
    std::cout << handle.get_type().attr("__name__").cast<std::string>() << std::endl; // Show 'ndarray'
    auto arr = pybind11::array::ensure(handle); // Crash here
    // auto arr = py::cast<py::array>(handle); // Also crash
    // Other code below based on the dtype and shape of the array
  }
}
// Python code
arr = np.random.randn(4,4)
test([arr])

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

Not a regression