Skip to content

Use pybind11 string formatter for exception messages #27868

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 1 commit into from
Mar 7, 2024
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
16 changes: 8 additions & 8 deletions src/_image_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ _get_transform_mesh(const py::object& transform, const py::ssize_t *dims)

if (output_mesh_array.ndim() != 2) {
throw std::runtime_error(
"Inverse transformed mesh array should be 2D not " +
std::to_string(output_mesh_array.ndim()) + "D");
"Inverse transformed mesh array should be 2D not {}D"_s.format(
output_mesh_array.ndim()));
}

return output_mesh_array;
Expand Down Expand Up @@ -108,8 +108,8 @@ image_resample(py::array input_array,

if (ndim == 3 && input_array.shape(2) != 4) {
throw std::invalid_argument(
"3D input array must be RGBA with shape (M, N, 4), has trailing dimension of " +
std::to_string(input_array.shape(2)));
"3D input array must be RGBA with shape (M, N, 4), has trailing dimension of {}"_s.format(
input_array.shape(2)));
}

// Ensure input array is contiguous, regardless of dtype
Expand All @@ -120,14 +120,14 @@ image_resample(py::array input_array,

if (out_ndim != ndim) {
throw std::invalid_argument(
"Input (" + std::to_string(ndim) + "D) and output (" + std::to_string(out_ndim) +
"D) arrays have different dimensionalities");
"Input ({}D) and output ({}D) arrays have different dimensionalities"_s.format(
ndim, out_ndim));
}

if (out_ndim == 3 && output_array.shape(2) != 4) {
throw std::invalid_argument(
"3D output array must be RGBA with shape (M, N, 4), has trailing dimension of " +
std::to_string(output_array.shape(2)));
"3D output array must be RGBA with shape (M, N, 4), has trailing dimension of {}"_s.format(
output_array.shape(2)));
}

if (!output_array.dtype().is(dtype)) {
Expand Down
13 changes: 7 additions & 6 deletions src/_tkagg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,19 @@ mpl_tk_blit(py::object interp_obj, const char *photo_name,

auto data_ptr = data.mutable_unchecked<3>(); // Checks ndim and writeable flag.
if (data.shape(2) != 4) {
throw py::value_error("Data pointer must be RGBA; last dimension is " +
std::to_string(data.shape(2)) + ", not 4");
throw py::value_error(
"Data pointer must be RGBA; last dimension is {}, not 4"_s.format(
data.shape(2)));
}
if (data.shape(0) > INT_MAX) { // Limited by Tk_PhotoPutBlock argument type.
throw std::range_error(
"Height (" + std::to_string(data.shape(0)) +
") exceeds maximum allowable size (" + std::to_string(INT_MAX) + ")");
"Height ({}) exceeds maximum allowable size ({})"_s.format(
data.shape(0), INT_MAX));
}
if (data.shape(1) > INT_MAX / 4) { // Limited by Tk_PhotoImageBlock.pitch field.
throw std::range_error(
"Width (" + std::to_string(data.shape(1)) +
") exceeds maximum allowable size (" + std::to_string(INT_MAX / 4) + ")");
"Width ({}) exceeds maximum allowable size ({})"_s.format(
data.shape(1), INT_MAX / 4));
}
const auto height = static_cast<int>(data.shape(0));
const auto width = static_cast<int>(data.shape(1));
Expand Down