diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index a7fe1898a8..3c7d0e4ec4 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -749,7 +749,11 @@ class cpp_function : public function { for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) { if (!some_args) some_args = true; else msg += ", "; - msg += pybind11::repr(args_[ti]); + try { + msg += pybind11::repr(args_[ti]); + } catch (...) { + msg += ""; + } } if (kwargs_in) { auto kwargs = reinterpret_borrow(kwargs_in); diff --git a/tests/test_constants_and_functions.py b/tests/test_constants_and_functions.py index 472682d619..038ee97450 100644 --- a/tests/test_constants_and_functions.py +++ b/tests/test_constants_and_functions.py @@ -1,3 +1,4 @@ +import pytest from pybind11_tests import constants_and_functions as m @@ -37,3 +38,13 @@ def test_exception_specifiers(): assert m.f2(53) == 55 assert m.f3(86) == 89 assert m.f4(140) == 144 + + +def test_incompatible_args_and_repr_exception(): + """Checks repr exceptions are handled gracefully by the dispatcher.""" + class ReprRaises(object): + def __repr__(self): + raise ValueError("repr") + with pytest.raises(TypeError, + match="Invoked with: "): + m.test_function(ReprRaises())