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

Skip to content

Commit 906d1b2

Browse files
committed
Remove more direct use of the Python C API
1 parent ac59ca8 commit 906d1b2

File tree

6 files changed

+24
-160
lines changed

6 files changed

+24
-160
lines changed

src/_backend_agg.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,11 +1232,11 @@ inline void RendererAgg::draw_gouraud_triangles(GCAgg &gc,
12321232
ColorArray &colors,
12331233
agg::trans_affine &trans)
12341234
{
1235-
if (points.shape(0) && !check_trailing_shape(points, "points", 3, 2)) {
1236-
throw py::error_already_set();
1235+
if (points.shape(0)) {
1236+
check_trailing_shape(points, "points", 3, 2);
12371237
}
1238-
if (colors.shape(0) && !check_trailing_shape(colors, "colors", 3, 4)) {
1239-
throw py::error_already_set();
1238+
if (colors.shape(0)) {
1239+
check_trailing_shape(colors, "colors", 3, 4);
12401240
}
12411241
if (points.shape(0) != colors.shape(0)) {
12421242
throw py::value_error(

src/_path_wrapper.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ Py_affine_transform(py::array_t<double, py::array::c_style | py::array::forcecas
181181
if (vertices_arr.ndim() == 2) {
182182
auto vertices = vertices_arr.unchecked<2>();
183183

184-
if(!check_trailing_shape(vertices, "vertices", 2)) {
185-
throw py::error_already_set();
186-
}
184+
check_trailing_shape(vertices, "vertices", 2);
187185

188186
py::ssize_t dims[] = { vertices.shape(0), 2 };
189187
py::array_t<double> result(dims);

src/mplutils.h

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,74 +48,51 @@ enum {
4848
CLOSEPOLY = 0x4f
4949
};
5050

51-
inline int prepare_and_add_type(PyTypeObject *type, PyObject *module)
52-
{
53-
if (PyType_Ready(type)) {
54-
return -1;
55-
}
56-
char const* ptr = strrchr(type->tp_name, '.');
57-
if (!ptr) {
58-
PyErr_SetString(PyExc_ValueError, "tp_name should be a qualified name");
59-
return -1;
60-
}
61-
if (PyModule_AddObject(module, ptr + 1, (PyObject *)type)) {
62-
return -1;
63-
}
64-
return 0;
65-
}
66-
6751
#ifdef __cplusplus // not for macosx.m
6852
// Check that array has shape (N, d1) or (N, d1, d2). We cast d1, d2 to longs
6953
// so that we don't need to access the NPY_INTP_FMT macro here.
7054
#include <pybind11/pybind11.h>
7155
#include <pybind11/numpy.h>
7256

7357
namespace py = pybind11;
58+
using namespace pybind11::literals;
7459

7560
template<typename T>
76-
inline bool check_trailing_shape(T array, char const* name, long d1)
61+
inline void check_trailing_shape(T array, char const* name, long d1)
7762
{
7863
if (array.ndim() != 2) {
79-
PyErr_Format(PyExc_ValueError,
80-
"Expected 2-dimensional array, got %ld",
81-
array.ndim());
82-
return false;
64+
throw py::value_error(
65+
"Expected 2-dimensional array, got %d"_s.format(array.ndim()));
8366
}
8467
if (array.size() == 0) {
8568
// Sometimes things come through as atleast_2d, etc., but they're empty, so
8669
// don't bother enforcing the trailing shape.
87-
return true;
70+
return;
8871
}
8972
if (array.shape(1) != d1) {
90-
PyErr_Format(PyExc_ValueError,
91-
"%s must have shape (N, %ld), got (%ld, %ld)",
92-
name, d1, array.shape(0), array.shape(1));
93-
return false;
73+
throw py::value_error(
74+
"%s must have shape (N, %d), got (%d, %d)"_s.format(
75+
name, d1, array.shape(0), array.shape(1)));
9476
}
95-
return true;
9677
}
9778

9879
template<typename T>
99-
inline bool check_trailing_shape(T array, char const* name, long d1, long d2)
80+
inline void check_trailing_shape(T array, char const* name, long d1, long d2)
10081
{
10182
if (array.ndim() != 3) {
102-
PyErr_Format(PyExc_ValueError,
103-
"Expected 3-dimensional array, got %ld",
104-
array.ndim());
105-
return false;
83+
throw py::value_error(
84+
"Expected 3-dimensional array, got %d"_s.format(array.ndim()));
10685
}
10786
if (array.size() == 0) {
10887
// Sometimes things come through as atleast_3d, etc., but they're empty, so
10988
// don't bother enforcing the trailing shape.
110-
return true;
89+
return;
11190
}
11291
if (array.shape(1) != d1 || array.shape(2) != d2) {
113-
PyErr_Format(PyExc_ValueError,
114-
"%s must have shape (N, %ld, %ld), got (%ld, %ld, %ld)",
115-
name, d1, d2, array.shape(0), array.shape(1), array.shape(2));
116-
return false;
92+
throw py::value_error(
93+
"%s must have shape (N, %d, %d), got (%d, %d, %d)"_s.format(
94+
name, d1, d2, array.shape(0), array.shape(1), array.shape(2)));
11795
}
118-
return true;
11996
}
12097

12198
/* In most cases, code should use safe_first_shape(obj) instead of obj.shape(0), since

src/py_adaptors.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,6 @@ class PathIterator
101101
m_iterator = 0;
102102
}
103103

104-
inline int
105-
set(PyObject *vertices, PyObject *codes, bool should_simplify, double simplify_threshold)
106-
{
107-
try {
108-
set(py::reinterpret_borrow<py::object>(vertices),
109-
py::reinterpret_borrow<py::object>(codes),
110-
should_simplify, simplify_threshold);
111-
} catch(const py::error_already_set &) {
112-
return 0;
113-
}
114-
return 1;
115-
}
116-
117104
inline void set(py::object vertices, py::object codes)
118105
{
119106
set(vertices, codes, false, 0.0);
@@ -186,16 +173,6 @@ class PathGenerator
186173
m_npaths = m_paths.size();
187174
}
188175

189-
int set(PyObject *obj)
190-
{
191-
try {
192-
set(py::reinterpret_borrow<py::object>(obj));
193-
} catch(const py::error_already_set &) {
194-
return 0;
195-
}
196-
return 1;
197-
}
198-
199176
Py_ssize_t num_paths() const
200177
{
201178
return m_npaths;

src/py_converters.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,25 @@ void convert_trans_affine(const py::object& transform, agg::trans_affine& affine
2323

2424
inline auto convert_points(py::array_t<double> obj)
2525
{
26-
if (!check_trailing_shape(obj, "points", 2)) {
27-
throw py::error_already_set();
28-
}
26+
check_trailing_shape(obj, "points", 2);
2927
return obj.unchecked<2>();
3028
}
3129

3230
inline auto convert_transforms(py::array_t<double> obj)
3331
{
34-
if (!check_trailing_shape(obj, "transforms", 3, 3)) {
35-
throw py::error_already_set();
36-
}
32+
check_trailing_shape(obj, "transforms", 3, 3);
3733
return obj.unchecked<3>();
3834
}
3935

4036
inline auto convert_bboxes(py::array_t<double> obj)
4137
{
42-
if (!check_trailing_shape(obj, "bbox array", 2, 2)) {
43-
throw py::error_already_set();
44-
}
38+
check_trailing_shape(obj, "bbox array", 2, 2);
4539
return obj.unchecked<3>();
4640
}
4741

4842
inline auto convert_colors(py::array_t<double> obj)
4943
{
50-
if (!check_trailing_shape(obj, "colors", 4)) {
51-
throw py::error_already_set();
52-
}
44+
check_trailing_shape(obj, "colors", 4);
5345
return obj.unchecked<2>();
5446
}
5547

src/py_exceptions.h

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)