mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 13:15:12 +00:00
Add exception translation for std::overflow_error. (#1977)
This commit is contained in:
parent
55ff464233
commit
deb3cb238a
@ -28,6 +28,8 @@ exceptions:
|
|||||||
+--------------------------------------+--------------------------------------+
|
+--------------------------------------+--------------------------------------+
|
||||||
| :class:`std::range_error` | ``ValueError`` |
|
| :class:`std::range_error` | ``ValueError`` |
|
||||||
+--------------------------------------+--------------------------------------+
|
+--------------------------------------+--------------------------------------+
|
||||||
|
| :class:`std::overflow_error` | ``OverflowError`` |
|
||||||
|
+--------------------------------------+--------------------------------------+
|
||||||
| :class:`pybind11::stop_iteration` | ``StopIteration`` (used to implement |
|
| :class:`pybind11::stop_iteration` | ``StopIteration`` (used to implement |
|
||||||
| | custom iterators) |
|
| | custom iterators) |
|
||||||
+--------------------------------------+--------------------------------------+
|
+--------------------------------------+--------------------------------------+
|
||||||
|
@ -211,6 +211,7 @@ inline void translate_exception(std::exception_ptr p) {
|
|||||||
} catch (const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return;
|
} catch (const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return;
|
||||||
} catch (const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what()); return;
|
} catch (const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what()); return;
|
||||||
} catch (const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return;
|
} catch (const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return;
|
||||||
|
} catch (const std::overflow_error &e) { PyErr_SetString(PyExc_OverflowError, e.what()); return;
|
||||||
} catch (const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return;
|
} catch (const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!");
|
PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!");
|
||||||
|
@ -116,6 +116,7 @@ TEST_SUBMODULE(exceptions, m) {
|
|||||||
m.def("throws5", []() { throw MyException5("this is a helper-defined translated exception"); });
|
m.def("throws5", []() { throw MyException5("this is a helper-defined translated exception"); });
|
||||||
m.def("throws5_1", []() { throw MyException5_1("MyException5 subclass"); });
|
m.def("throws5_1", []() { throw MyException5_1("MyException5 subclass"); });
|
||||||
m.def("throws_logic_error", []() { throw std::logic_error("this error should fall through to the standard handler"); });
|
m.def("throws_logic_error", []() { throw std::logic_error("this error should fall through to the standard handler"); });
|
||||||
|
m.def("throws_overflow_error", []() {throw std::overflow_error(""); });
|
||||||
m.def("exception_matches", []() {
|
m.def("exception_matches", []() {
|
||||||
py::dict foo;
|
py::dict foo;
|
||||||
try {
|
try {
|
||||||
|
@ -79,6 +79,10 @@ def test_custom(msg):
|
|||||||
m.throws_logic_error()
|
m.throws_logic_error()
|
||||||
assert msg(excinfo.value) == "this error should fall through to the standard handler"
|
assert msg(excinfo.value) == "this error should fall through to the standard handler"
|
||||||
|
|
||||||
|
# OverFlow error translation.
|
||||||
|
with pytest.raises(OverflowError) as excinfo:
|
||||||
|
m.throws_overflow_error()
|
||||||
|
|
||||||
# Can we handle a helper-declared exception?
|
# Can we handle a helper-declared exception?
|
||||||
with pytest.raises(m.MyException5) as excinfo:
|
with pytest.raises(m.MyException5) as excinfo:
|
||||||
m.throws5()
|
m.throws5()
|
||||||
|
Loading…
Reference in New Issue
Block a user