Skip to content

Better error report #1965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,17 @@ class type_caster_generic {
case return_value_policy::copy:
if (copy_constructor)
valueptr = copy_constructor(src);
else
throw cast_error("return_value_policy = copy, but the "
"object is non-copyable!");
else {
#if defined(NDEBUG)
throw cast_error("return_value_policy = copy, but type is "
"non-copyable! (compile in debug mode for details)");
#else
std::string type_name(tinfo->cpptype->name());
detail::clean_type_id(type_name);
throw cast_error("return_value_policy = copy, but type " +
type_name + " is non-copyable!");
#endif
}
wrapper->owned = true;
break;

Expand All @@ -544,9 +552,18 @@ class type_caster_generic {
valueptr = move_constructor(src);
else if (copy_constructor)
valueptr = copy_constructor(src);
else
throw cast_error("return_value_policy = move, but the "
"object is neither movable nor copyable!");
else {
#if defined(NDEBUG)
throw cast_error("return_value_policy = move, but type is neither "
"movable nor copyable! "
"(compile in debug mode for details)");
#else
std::string type_name(tinfo->cpptype->name());
detail::clean_type_id(type_name);
throw cast_error("return_value_policy = move, but type " +
type_name + " is neither movable nor copyable!");
#endif
}
wrapper->owned = true;
break;

Expand Down
6 changes: 3 additions & 3 deletions tests/test_copy_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
def test_lacking_copy_ctor():
with pytest.raises(RuntimeError) as excinfo:
m.lacking_copy_ctor.get_one()
assert "the object is non-copyable!" in str(excinfo.value)
assert "is non-copyable!" in str(excinfo.value)


def test_lacking_move_ctor():
with pytest.raises(RuntimeError) as excinfo:
m.lacking_move_ctor.get_one()
assert "the object is neither movable nor copyable!" in str(excinfo.value)
assert "is neither movable nor copyable!" in str(excinfo.value)


def test_move_and_copy_casts():
Expand Down Expand Up @@ -98,7 +98,7 @@ def test_private_op_new():

with pytest.raises(RuntimeError) as excinfo:
m.private_op_new_value()
assert "the object is neither movable nor copyable" in str(excinfo.value)
assert "is neither movable nor copyable" in str(excinfo.value)

assert m.private_op_new_reference().value == 1

Expand Down