Skip to content

Commit

Permalink
Add test returning py::exception<void> (fails at runtime).
Browse files Browse the repository at this point in the history
Passing `py::exception<void>` does not compile:

```
pytypes.h:459:36: error: could not convert ‘{h, pybind11::object::borrowed_t()}’ from ‘<brace-enclosed initializer list>’ to ‘pybind11::exception<void>’
  459 |     return {h, object::borrowed_t{}};
```
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Oct 22, 2023
1 parent 6a3a954 commit 169b0e5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/test_exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,7 @@ TEST_SUBMODULE(exceptions, m) {
// function returns None instead of int, should give a useful error message
fn().cast<int>();
});

// m.def("pass_exception_void", [](const py::exception<void>&) {}); // Does not compile.
m.def("return_exception_void", []() { return py::exception<void>(); });
}
6 changes: 6 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,9 @@ def test_fn_cast_int_exception():
assert str(excinfo.value).startswith(
"Unable to cast Python instance of type <class 'NoneType'> to C++ type"
)


def test_return_exception_void():
with pytest.raises(TypeError) as excinfo:
m.return_exception_void()
assert 'Annotated[Any, CppTypePybind11("exception<void>")]' in str(excinfo.value)

0 comments on commit 169b0e5

Please sign in to comment.