Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b937895

Browse files
committed
Move some checks from RendererAgg wrapper into itself
1 parent 7176f0a commit b937895

5 files changed

Lines changed: 35 additions & 29 deletions

File tree

src/_backend_agg.cpp

Lines changed: 10 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <vector>
1111
#include <algorithm>
1212

13+
#include <pybind11/pybind11.h>
14+
1315
#include "agg_alpha_mask_u8.h"
1416
#include "agg_conv_curve.h"
1517
#include "agg_conv_dash.h"
@@ -1227,6 +1229,19 @@ inline void RendererAgg::draw_gouraud_triangles(GCAgg &gc,
12271229
ColorArray &colors,
12281230
agg::trans_affine &trans)
12291231
{
1232+
if (points.shape(0) && !check_trailing_shape(points, "points", 3, 2)) {
1233+
throw pybind11::error_already_set();
1234+
}
1235+
if (colors.shape(0) && !check_trailing_shape(colors, "colors", 3, 4)) {
1236+
throw pybind11::error_already_set();
1237+
}
1238+
if (points.shape(0) != colors.shape(0)) {
1239+
throw pybind11::value_error(
1240+
"points and colors arrays must be the same length, got " +
1241+
std::to_string(points.shape(0)) + " points and " +
1242+
std::to_string(colors.shape(0)) + "colors");
1243+
}
1244+
12301245
theRasterizer.reset_clipping();
12311246
rendererBase.reset_clipping(true);
12321247
set_clipbox(gc.cliprect, theRasterizer);

src/_backend_agg_wrapper.cpp

Lines changed: 0 additions & 27 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 9 additions & 1 deletion
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)