Skip to content
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

Limit error message size when a function is invoked with the wrong type(s) #5060

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 14 additions & 2 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1122,9 +1122,13 @@ class cpp_function : public function {
msg += '\n';
}
msg += "\nInvoked with: ";
constexpr auto max_args_repr_size = 5000u;
const auto orig_msg_size = msg.size();
auto args_ = reinterpret_borrow<tuple>(args_in);
bool some_args = false;
for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) {
for (size_t ti = overloads->is_constructor ? 1 : 0;
ti < args_.size() && msg.size() - orig_msg_size <= max_args_repr_size;
++ti) {
if (!some_args) {
some_args = true;
} else {
Expand All @@ -1136,7 +1140,7 @@ class cpp_function : public function {
msg += "<repr raised Error>";
}
}
if (kwargs_in) {
if (kwargs_in && msg.size() - orig_msg_size <= max_args_repr_size) {
auto kwargs = reinterpret_borrow<dict>(kwargs_in);
if (!kwargs.empty()) {
if (some_args) {
Expand All @@ -1145,6 +1149,10 @@ class cpp_function : public function {
msg += "kwargs: ";
bool first = true;
for (const auto &kwarg : kwargs) {
if (msg.size() - orig_msg_size > max_args_repr_size) {
break;
}

if (first) {
first = false;
} else {
Expand All @@ -1160,6 +1168,10 @@ class cpp_function : public function {
}
}

if (msg.size() - orig_msg_size > max_args_repr_size) {
msg += "...";
}

append_note_if_missing_header_is_suspected(msg);
// Attach additional error info to the exception if supported
if (PyErr_Occurred()) {
Expand Down
Loading