@@ -85,16 +85,16 @@ _get_transform_mesh(const py::object& transform, const py::ssize_t *dims)
8585 py::array_t <double , py::array::c_style | py::array::forcecast>(output_mesh);
8686
8787 if (output_mesh_array.ndim () != 2 ) {
88- throw std::runtime_error (
89- " Inverse transformed mesh array should be 2D not {}D" _s. format (
88+ throw std::runtime_error (mpl::format (
89+ " Inverse transformed mesh array should be 2D not {}D" ,
9090 output_mesh_array.ndim ()));
9191 }
9292
9393 // An undersized mesh would be read out of bounds by the resampler.
9494 if (output_mesh_array.shape (0 ) != mesh_dims[0 ] ||
9595 output_mesh_array.shape (1 ) != mesh_dims[1 ]) {
96- throw std::runtime_error (
97- " Inverse transformed mesh array should have shape ({}, {}) not ({}, {})" _s. format (
96+ throw std::runtime_error (mpl::format (
97+ " Inverse transformed mesh array should have shape ({}, {}) not ({}, {})" ,
9898 mesh_dims[0 ], mesh_dims[1 ],
9999 output_mesh_array.shape (0 ), output_mesh_array.shape (1 )));
100100 }
@@ -124,8 +124,8 @@ image_resample(py::array input_array,
124124 }
125125
126126 if (ndim == 3 && input_array.shape (2 ) != 4 ) {
127- throw std::invalid_argument (
128- " 3D input array must be RGBA with shape (M, N, 4), has trailing dimension of {}" _s. format (
127+ throw std::invalid_argument (mpl::format (
128+ " 3D input array must be RGBA with shape (M, N, 4), has trailing dimension of {}" ,
129129 input_array.shape (2 )));
130130 }
131131
@@ -139,14 +139,14 @@ image_resample(py::array input_array,
139139 auto out_ndim = output_array.ndim ();
140140
141141 if (out_ndim != ndim) {
142- throw std::invalid_argument (
143- " Input ({}D) and output ({}D) arrays have different dimensionalities" _s. format (
142+ throw std::invalid_argument (mpl::format (
143+ " Input ({}D) and output ({}D) arrays have different dimensionalities" ,
144144 ndim, out_ndim));
145145 }
146146
147147 if (out_ndim == 3 && output_array.shape (2 ) != 4 ) {
148- throw std::invalid_argument (
149- " 3D output array must be RGBA with shape (M, N, 4), has trailing dimension of {}" _s. format (
148+ throw std::invalid_argument (mpl::format (
149+ " 3D output array must be RGBA with shape (M, N, 4), has trailing dimension of {}" ,
150150 output_array.shape (2 )));
151151 }
152152
@@ -220,6 +220,16 @@ image_resample(py::array input_array,
220220}
221221
222222
223+ [[noreturn]] static void
224+ raise_image_comparison_failure (std::string msg)
225+ {
226+ auto exceptions = py::module_::import (" matplotlib.testing.exceptions" );
227+ auto ImageComparisonFailure = exceptions.attr (" ImageComparisonFailure" );
228+ py::set_error (ImageComparisonFailure.ptr (), msg.c_str ());
229+ throw py::error_already_set ();
230+ }
231+
232+
223233// This is used by matplotlib.testing.compare to calculate RMS and a difference image.
224234static py::tuple
225235calculate_rms_and_diff (py::array_t <unsigned char > expected_image,
@@ -229,13 +239,9 @@ calculate_rms_and_diff(py::array_t<unsigned char> expected_image,
229239 std::pair{actual_image, " Actual" }})
230240 {
231241 if (image.ndim () != 3 ) {
232- auto exceptions = py::module_::import (" matplotlib.testing.exceptions" );
233- auto ImageComparisonFailure = exceptions.attr (" ImageComparisonFailure" );
234- py::set_error (
235- ImageComparisonFailure,
236- " {name} image must be 3-dimensional, but is {ndim}-dimensional" _s.format (
237- " name" _a=name, " ndim" _a=image.ndim ()));
238- throw py::error_already_set ();
242+ raise_image_comparison_failure (mpl::format (
243+ " {} image must be 3-dimensional, but is {}-dimensional" ,
244+ name, image.ndim ()));
239245 }
240246 }
241247
@@ -244,25 +250,16 @@ calculate_rms_and_diff(py::array_t<unsigned char> expected_image,
244250 auto depth = expected_image.shape (2 );
245251
246252 if (depth != 3 && depth != 4 ) {
247- auto exceptions = py::module_::import (" matplotlib.testing.exceptions" );
248- auto ImageComparisonFailure = exceptions.attr (" ImageComparisonFailure" );
249- py::set_error (
250- ImageComparisonFailure,
251- " Image must be RGB or RGBA but has depth {depth}" _s.format (
252- " depth" _a=depth));
253- throw py::error_already_set ();
253+ raise_image_comparison_failure (mpl::format (
254+ " Image must be RGB or RGBA but has depth {}" , depth));
254255 }
255256
256257 if (height != actual_image.shape (0 ) || width != actual_image.shape (1 ) ||
257258 depth != actual_image.shape (2 )) {
258- auto exceptions = py::module_::import (" matplotlib.testing.exceptions" );
259- auto ImageComparisonFailure = exceptions.attr (" ImageComparisonFailure" );
260- py::set_error (
261- ImageComparisonFailure,
262- " Image sizes do not match expected size: {expected_image.shape} " _s
263- " actual size {actual_image.shape}" _s.format (
259+ raise_image_comparison_failure (mpl::format (
260+ " Image sizes do not match expected size: {expected_image.shape} "
261+ " actual size {actual_image.shape}" ,
264262 " expected_image" _a=expected_image, " actual_image" _a=actual_image));
265- throw py::error_already_set ();
266263 }
267264 auto expected = expected_image.unchecked <3 >();
268265 auto actual = actual_image.unchecked <3 >();
0 commit comments