Skip to content

Commit ee3e424

Browse files
committed
Handle exceptions in numpy::array_view<...>::set().
set() can throw an exception; we must check for that to properly get that exception propagated to the python side; otherwise we get a SystemError ("method ... returned a result with an exception set"). Example repro: ``` from pylab import * gca().add_collection(mpl.collections.LineCollection(rand(2, 2, 2), array=0)) ``` (Here the C extension method receives a single tuple (rgba) color rather than an array of tuple (rgba) colors.) I'd rather not explicitly test for the exception being raised (a ValueError) because if switching to pybind11 one naturally gets a broadcast of the scalar value into a correctly dimensionalized array; indeed mplcairo handles the above example just fine.
1 parent efd66d4 commit ee3e424

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/py_converters.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,8 @@ int convert_points(PyObject *obj, void *pointsp)
510510
if (obj == NULL || obj == Py_None) {
511511
return 1;
512512
}
513-
points->set(obj);
514-
if (points->size() && !check_trailing_shape(*points, "points", 2)) {
513+
if (!points->set(obj)
514+
|| points->size() && !check_trailing_shape(*points, "points", 2)) {
515515
return 0;
516516
}
517517
return 1;
@@ -523,8 +523,8 @@ int convert_transforms(PyObject *obj, void *transp)
523523
if (obj == NULL || obj == Py_None) {
524524
return 1;
525525
}
526-
trans->set(obj);
527-
if (trans->size() && !check_trailing_shape(*trans, "transforms", 3, 3)) {
526+
if (!trans->set(obj)
527+
|| trans->size() && !check_trailing_shape(*trans, "transforms", 3, 3)) {
528528
return 0;
529529
}
530530
return 1;
@@ -536,8 +536,8 @@ int convert_bboxes(PyObject *obj, void *bboxp)
536536
if (obj == NULL || obj == Py_None) {
537537
return 1;
538538
}
539-
bbox->set(obj);
540-
if (bbox->size() && !check_trailing_shape(*bbox, "bbox array", 2, 2)) {
539+
if (!bbox->set(obj)
540+
|| bbox->size() && !check_trailing_shape(*bbox, "bbox array", 2, 2)) {
541541
return 0;
542542
}
543543
return 1;
@@ -549,8 +549,8 @@ int convert_colors(PyObject *obj, void *colorsp)
549549
if (obj == NULL || obj == Py_None) {
550550
return 1;
551551
}
552-
colors->set(obj);
553-
if (colors->size() && !check_trailing_shape(*colors, "colors", 4)) {
552+
if (!colors->set(obj)
553+
|| colors->size() && !check_trailing_shape(*colors, "colors", 4)) {
554554
return 0;
555555
}
556556
return 1;

0 commit comments

Comments
 (0)