Improve make_tuple error message under debugging
When make_tuple fails (for example, when print() is called with a
non-convertible argument, as in #778) the error message a less helpful
than it could be:
make_tuple(): unable to convert arguments of types 'std::tuple<type1, type2>' to Python object
There is no actual std::tuple involved (only a parameter pack and a
Python tuple), but it also doesn't immediately reveal which type caused
the problem.
This commit changes the debugging mode output to show just the
problematic type:
make_tuple(): unable to convert argument of type 'type2' to Python object
diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h
index 0beaf6e..c77dfd0 100644
--- a/include/pybind11/cast.h
+++ b/include/pybind11/cast.h
@@ -1246,18 +1246,19 @@
template <return_value_policy policy = return_value_policy::automatic_reference,
typename... Args> tuple make_tuple(Args&&... args_) {
- const size_t size = sizeof...(Args);
+ constexpr size_t size = sizeof...(Args);
std::array<object, size> args {
{ reinterpret_steal<object>(detail::make_caster<Args>::cast(
std::forward<Args>(args_), policy, nullptr))... }
};
- for (auto &arg_value : args) {
- if (!arg_value) {
+ for (size_t i = 0; i < args.size(); i++) {
+ if (!args[i]) {
#if defined(NDEBUG)
throw cast_error("make_tuple(): unable to convert arguments to Python object (compile in debug mode for details)");
#else
- throw cast_error("make_tuple(): unable to convert arguments of types '" +
- (std::string) type_id<std::tuple<Args...>>() + "' to Python object");
+ std::array<std::string, size> argtypes { type_id<Args>()... };
+ throw cast_error("make_tuple(): unable to convert argument of type '" +
+ argtypes[i] + "' to Python object");
#endif
}
}
diff --git a/tests/test_python_types.cpp b/tests/test_python_types.cpp
index 5696239..6f20809 100644
--- a/tests/test_python_types.cpp
+++ b/tests/test_python_types.cpp
@@ -186,6 +186,7 @@
std::list<Value> move_list() const { return {{0}, {1}, {2}}; }
};
+struct UnregisteredType { };
test_initializer python_types([](py::module &m) {
/* No constructor is explicitly defined below. An exception is raised when
@@ -235,6 +236,13 @@
py::print("{a} + {b} = {c}"_s.format("a"_a="py::print", "b"_a="str.format", "c"_a="this"));
});
+ m.def("test_print_failure", []() { py::print(42, UnregisteredType()); });
+#if !defined(NDEBUG)
+ m.attr("debug_enabled") = true;
+#else
+ m.attr("debug_enabled") = false;
+#endif
+
m.def("test_str_format", []() {
auto s1 = "{} + {} = {}"_s.format(1, 2, 3);
auto s2 = "{a} + {b} = {c}"_s.format("a"_a=1, "b"_a=2, "c"_a=3);
diff --git a/tests/test_python_types.py b/tests/test_python_types.py
index 7956c7c..cf8c147 100644
--- a/tests/test_python_types.py
+++ b/tests/test_python_types.py
@@ -258,7 +258,7 @@
def test_print(capture):
- from pybind11_tests import test_print_function
+ from pybind11_tests import test_print_function, test_print_failure, debug_enabled
with capture:
test_print_function()
@@ -272,6 +272,14 @@
"""
assert capture.stderr == "this goes to stderr"
+ with pytest.raises(RuntimeError) as excinfo:
+ test_print_failure()
+ assert str(excinfo.value) == "make_tuple(): unable to convert " + (
+ "argument of type 'UnregisteredType' to Python object"
+ if debug_enabled else
+ "arguments to Python object (compile in debug mode for details)"
+ )
+
def test_str_api():
from pybind11_tests import test_str_format