pybind/pybind11

[BUG]: 'terminate called after throwing an instance of' in `def_buffer` method

Open

#3.336 geöffnet am 6. Okt. 2021

Auf GitHub ansehen
 (11 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)C++ (2.005 Forks)batch import
bugduplicatehelp wanted

Repository-Metriken

Stars
 (14.677 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 5T 2h) (10 gemergte PRs in 30 T)

Beschreibung

Required prerequisites

Problem description

It seems that if I throw an exception from a def_buffer method, this is not translated properly to a Python exception and instead terminates the program with something like:

terminate called after throwing an instance of 'std::invalid_argument'
  what():  unimplemented array format.
Aborted

Reproducible example code

I believe the following minimal repro causes this but I have not yet built it separately/verified outside of my project.

struct Foobar;
py::class_<Foobar>(m, "Foobar")
  .def(py::init<>())
  .def_buffer([](Foobar &) {
    throw std::invalid_argument("boom");
  });
import numpy as np
import my_module
np.array(my_module.Foobar())

Contributor Guide