Add exception translation for std::overflow_error. (#1977)
diff --git a/docs/advanced/exceptions.rst b/docs/advanced/exceptions.rst
index 75ac24a..75ad7f7 100644
--- a/docs/advanced/exceptions.rst
+++ b/docs/advanced/exceptions.rst
@@ -28,6 +28,8 @@
+--------------------------------------+--------------------------------------+
| :class:`std::range_error` | ``ValueError`` |
+--------------------------------------+--------------------------------------+
+| :class:`std::overflow_error` | ``OverflowError`` |
++--------------------------------------+--------------------------------------+
| :class:`pybind11::stop_iteration` | ``StopIteration`` (used to implement |
| | custom iterators) |
+--------------------------------------+--------------------------------------+
diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h
index 067780c..87952da 100644
--- a/include/pybind11/detail/internals.h
+++ b/include/pybind11/detail/internals.h
@@ -211,6 +211,7 @@
} 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::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 (...) {
PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!");
diff --git a/tests/test_exceptions.cpp b/tests/test_exceptions.cpp
index d301390..56cd9bc 100644
--- a/tests/test_exceptions.cpp
+++ b/tests/test_exceptions.cpp
@@ -116,6 +116,7 @@
m.def("throws5", []() { throw MyException5("this is a helper-defined translated exception"); });
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_overflow_error", []() {throw std::overflow_error(""); });
m.def("exception_matches", []() {
py::dict foo;
try {
diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py
index 6edff9f..ac2b360 100644
--- a/tests/test_exceptions.py
+++ b/tests/test_exceptions.py
@@ -79,6 +79,10 @@
m.throws_logic_error()
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?
with pytest.raises(m.MyException5) as excinfo:
m.throws5()