Skip to content

Commit 527148f

Browse files
committed
Address comments
1 parent dbb950c commit 527148f

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

include/pybind11/cast.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,12 +1019,12 @@ type_caster<T, SFINAE> &load_type(type_caster<T, SFINAE> &conv, const handle &ha
10191019
#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
10201020
throw cast_error(
10211021
"Unable to cast Python instance of type "
1022-
+ static_cast<std::string>(str(type::handle_of(handle)))
1022+
+ str(type::handle_of(handle)).cast<std::string>()
10231023
+ " to C++ type '?' (#define "
10241024
"PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
10251025
#else
10261026
throw cast_error("Unable to cast Python instance of type "
1027-
+ (std::string) str(type::handle_of(handle)) + " to C++ type '"
1027+
+ str(type::handle_of(handle)).cast<std::string>() + " to C++ type '"
10281028
+ type_id<T>() + "'");
10291029
#endif
10301030
}
@@ -1088,13 +1088,13 @@ detail::enable_if_t<!detail::move_never<T>::value, T> move(object &&obj) {
10881088
if (obj.ref_count() > 1) {
10891089
#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
10901090
throw cast_error(
1091-
"Unable to cast Python " + (std::string) str(type::handle_of(obj))
1091+
"Unable to cast Python " + str(type::handle_of(obj)).cast<std::string>()
10921092
+ " instance to C++ rvalue: instance has multiple references"
10931093
" (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
10941094
#else
1095-
throw cast_error("Unable to move from Python " + (std::string) str(type::handle_of(obj))
1096-
+ " instance to C++ " + type_id<T>()
1097-
+ " instance: instance has multiple references");
1095+
throw cast_error("Unable to move from Python "
1096+
+ str(type::handle_of(obj)).cast<std::string>() + " instance to C++ "
1097+
+ type_id<T>() + " instance: instance has multiple references");
10981098
#endif
10991099
}
11001100

include/pybind11/detail/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ constexpr
12251225
#endif
12261226

12271227
// Pybind offers detailed error messages by default for all builts that are debug (through the
1228-
// negation of ndebug). This can also be manually enabled by users, for any builds, through
1228+
// negation of NDEBUG). This can also be manually enabled by users, for any builds, through
12291229
// defining PYBIND11_DETAILED_ERROR_MESSAGES.
12301230
//
12311231
// Pybind attempts to provide useful error messages by default. Specifically, this macro will

tests/test_callbacks.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import env # noqa: F401
77
from pybind11_tests import callbacks as m
8+
from pybind11_tests import detailed_error_messages_enabled
89

910

1011
def test_callbacks():
@@ -70,11 +71,19 @@ def f(*args, **kwargs):
7071

7172
with pytest.raises(RuntimeError) as excinfo:
7273
m.test_arg_conversion_error1(f)
73-
assert "Unable to convert call argument" in str(excinfo.value)
74+
assert str(excinfo.value) == "Unable to convert call argument " + (
75+
"'1' of type 'UnregisteredType' to Python object"
76+
if detailed_error_messages_enabled
77+
else "'1' to Python object (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)"
78+
)
7479

7580
with pytest.raises(RuntimeError) as excinfo:
7681
m.test_arg_conversion_error2(f)
77-
assert "Unable to convert call argument" in str(excinfo.value)
82+
assert str(excinfo.value) == "Unable to convert call argument " + (
83+
"'expected_name' of type 'UnregisteredType' to Python object"
84+
if detailed_error_messages_enabled
85+
else "'expected_name' to Python object (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)"
86+
)
7887

7988

8089
def test_lambda_closure_cleanup():

tests/test_exceptions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ TEST_SUBMODULE(exceptions, m) {
340340
return py::str("UNEXPECTED");
341341
});
342342

343-
m.def("test_hidden_error", [](const py::function &fn) {
344-
// function returns none instead of int, should give a useful error message
343+
m.def("test_fn_cast_int", [](const py::function &fn) {
344+
// function returns None instead of int, should give a useful error message
345345
fn().cast<int>();
346346
});
347347
}

tests/test_exceptions.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,10 @@ def test_pypy_oserror_normalization():
383383
assert "this_filename_must_not_exist" in what
384384

385385

386-
def test_sane_hidden_exception():
386+
def test_fn_cast_int_exception():
387387
with pytest.raises(RuntimeError) as excinfo:
388-
m.test_hidden_error(lambda: None)
388+
m.test_fn_cast_int(lambda: None)
389389

390-
# Always display the python type even if the C++ type isn't available
391390
assert str(excinfo.value).startswith(
392391
"Unable to cast Python instance of type <class 'NoneType'> to C++ type"
393392
)

0 commit comments

Comments
 (0)