Skip to content

Commit ac268ec

Browse files
committed
Move some checks from RendererAgg wrapper into itself
1 parent 42b88d0 commit ac268ec

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

src/_backend_agg.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ RendererAgg::RendererAgg(unsigned int width, unsigned int height, double dpi)
2929
lastclippath(NULL),
3030
_fill_color(agg::rgba(1, 1, 1, 0))
3131
{
32+
if (dpi <= 0.0) {
33+
throw std::range_error("dpi must be positive");
34+
}
35+
36+
if (width >= 1 << 16 || height >= 1 << 16) {
37+
throw std::range_error(
38+
"Image size of " + std::to_string(width) + "x" + std::to_string(height) +
39+
" pixels is too large. It must be less than 2^16 in each direction.");
40+
}
41+
3242
unsigned stride(width * 4);
3343

3444
pixBuffer = new agg::int8u[NUMBYTES];

src/_backend_agg.h

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#ifndef MPL_BACKEND_AGG_H
77
#define MPL_BACKEND_AGG_H
88

9+
#include <pybind11/pybind11.h>
10+
911
#include <cmath>
1012
#include <algorithm>
1113

@@ -40,6 +42,8 @@
4042
#include "array.h"
4143
#include "agg_workaround.h"
4244

45+
namespace py = pybind11;
46+
4347
/**********************************************************************/
4448

4549
// a helper class to pass agg::buffer objects around.
@@ -1226,6 +1230,19 @@ inline void RendererAgg::draw_gouraud_triangles(GCAgg &gc,
12261230
ColorArray &colors,
12271231
agg::trans_affine &trans)
12281232
{
1233+
if (points.shape(0) && !check_trailing_shape(points, "points", 3, 2)) {
1234+
throw py::error_already_set();
1235+
}
1236+
if (colors.shape(0) && !check_trailing_shape(colors, "colors", 3, 4)) {
1237+
throw py::error_already_set();
1238+
}
1239+
if (points.shape(0) != colors.shape(0)) {
1240+
throw py::value_error(
1241+
"points and colors arrays must be the same length, got " +
1242+
std::to_string(points.shape(0)) + " points and " +
1243+
std::to_string(colors.shape(0)) + "colors");
1244+
}
1245+
12291246
theRasterizer.reset_clipping();
12301247
rendererBase.reset_clipping(true);
12311248
set_clipbox(gc.cliprect, theRasterizer);

src/_backend_agg_wrapper.cpp

-27
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,6 @@ static int PyRendererAgg_init(PyRendererAgg *self, PyObject *args, PyObject *kwd
145145
return -1;
146146
}
147147

148-
if (dpi <= 0.0) {
149-
PyErr_SetString(PyExc_ValueError, "dpi must be positive");
150-
return -1;
151-
}
152-
153-
if (width >= 1 << 16 || height >= 1 << 16) {
154-
PyErr_Format(
155-
PyExc_ValueError,
156-
"Image size of %dx%d pixels is too large. "
157-
"It must be less than 2^16 in each direction.",
158-
width, height);
159-
return -1;
160-
}
161-
162148
CALL_CPP_INIT("RendererAgg", self->x = new RendererAgg(width, height, dpi))
163149

164150
return 0;
@@ -420,19 +406,6 @@ PyRendererAgg_draw_gouraud_triangles(PyRendererAgg *self, PyObject *args)
420406
&trans)) {
421407
return NULL;
422408
}
423-
if (points.shape(0) && !check_trailing_shape(points, "points", 3, 2)) {
424-
return NULL;
425-
}
426-
if (colors.shape(0) && !check_trailing_shape(colors, "colors", 3, 4)) {
427-
return NULL;
428-
}
429-
if (points.shape(0) != colors.shape(0)) {
430-
PyErr_Format(PyExc_ValueError,
431-
"points and colors arrays must be the same length, got "
432-
"%" NPY_INTP_FMT " points and %" NPY_INTP_FMT "colors",
433-
points.shape(0), colors.shape(0));
434-
return NULL;
435-
}
436409

437410
CALL_CPP("draw_gouraud_triangles", self->x->draw_gouraud_triangles(gc, points, colors, trans));
438411

src/meson.build

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ extension_data = {
7777
'_backend_agg.cpp',
7878
'_backend_agg_wrapper.cpp',
7979
),
80-
'dependencies': [agg_dep, numpy_dep, freetype_dep],
80+
'dependencies': [agg_dep, numpy_dep, freetype_dep, pybind11_dep],
8181
},
8282
'_c_internal_utils': {
8383
'subdir': 'matplotlib',

src/py_exceptions.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,17 @@ class exception : public std::exception
4646
} \
4747
return (errorcode); \
4848
} \
49+
catch (const std::range_error &e) \
50+
{ \
51+
PyErr_Format(PyExc_ValueError, "In %s: %s", (name), e.what()); \
52+
{ \
53+
cleanup; \
54+
} \
55+
return (errorcode); \
56+
} \
4957
catch (const std::runtime_error &e) \
5058
{ \
51-
PyErr_Format(PyExc_RuntimeError, "In %s: %s", (name), e.what()); \
59+
PyErr_Format(PyExc_RuntimeError, "In %s: %s", (name), e.what()); \
5260
{ \
5361
cleanup; \
5462
} \

0 commit comments

Comments
 (0)