8000 Change how array converter functions are defined · matplotlib/matplotlib@6d07179 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d07179

Browse files
committed
Change how array converter functions are defined
1 parent ba40160 commit 6d07179

File tree

6 files changed

+91
-58
lines changed

6 files changed

+91
-58
lines changed

src/_backend_agg_wrapper.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static PyObject *PyRendererAgg_draw_text_image(PyRendererAgg *self, PyObject *ar
206206

207207
if (!PyArg_ParseTuple(args,
208208
"O&dddO&:draw_text_image",
209-
&numpy::convert_array_contiguous<agg::int8u, 2>,
209+
&image.converter_contiguous,
210210
&image,
211211
&x,
212212
&y,
@@ -274,7 +274,7 @@ static PyObject *PyRendererAgg_draw_image(PyRendererAgg *self, PyObject *args, P
274274
&gc,
275275
&x,
276276
&y,
277-
&numpy::convert_array_contiguous<agg::int8u, 3>,
277+
&image.converter_contiguous,
278278
&image,
279279
&w,
280280
&h,
@@ -319,21 +319,21 @@ PyRendererAgg_draw_path_collection(PyRendererAgg *self, PyObject *args, PyObject
319319
&convert_trans_affine,
320320
&master_transform,
321321
&pathobj,
322-
&numpy::convert_array<const double, 3>,
322+
&transforms.converter,
323323
&transforms,
324-
&numpy::convert_array<const double, 2>,
324+
&offsets.converter,
325325
&offsets,
326326
&convert_trans_affine,
327327
&offset_trans,
328-
&numpy::convert_array<const double, 2>,
328+
&facecolors.converter,
329329
&facecolors,
330-
&numpy::convert_array<const double, 2>,
330+
&edgecolors.converter,
331331
&edgecolors,
332-
&numpy::convert_array<const double, 1>,
332+
&linewidths.converter,
333333
&linewidths,
334334
&convert_dashes_vector,
335335
&dashes,
336-
&numpy::convert_array<const uint8_t, 1>,
336+
&antialiaseds.converter,
337337
&antialiaseds,
338338
&ignored,
339339
&convert_offset_position,
@@ -388,16 +388,16 @@ static PyObject *PyRendererAgg_draw_quad_mesh(PyRendererAgg *self, PyObject *arg
388388
&master_transform,
389389
&mesh_width,
390390
&mesh_height,
391-
&numpy::convert_array<const double, 3>,
391+
&coordinates.converter,
392392
&coordinates,
393-
&numpy::convert_array<const double, 2>,
393+
&offsets.converter,
394394
&offsets,
395395
&convert_trans_affine,
396396
&offset_trans,
397-
&numpy::convert_array<const double, 2>,
397+
&facecolors.converter,
398398
&facecolors,
399399
&antialiased,
400-
&numpy::convert_array<const double, 2>,
400+
&edgecolors.converter,
401401
&edgecolors)) {
402402
return NULL;
403403
}
@@ -429,9 +429,9 @@ PyRendererAgg_draw_gouraud_triangle(PyRendererAgg *self, PyObject *args, PyObjec
429429
"O&O&O&O&|O:draw_gouraud_triangle",
430430
&convert_gcagg,
431431
&gc,
432-
&numpy::convert_array<const double, 2>,
432+
&points.converter,
433433
&points,
434-
&numpy::convert_array<const double, 2>,
434+
&colors.converter,
435435
&colors,
436436
&convert_trans_affine,
437437
&trans)) {
@@ -455,9 +455,9 @@ PyRendererAgg_draw_gouraud_triangles(PyRendererAgg *self, PyObject *args, PyObje
455455
"O&O&O&O&|O:draw_gouraud_triangles",
456456
&convert_gcagg,
457457
&gc,
458-
&numpy::convert_array<const double, 3>,
458+
&points.converter,
459459
&points,
460-
&numpy::convert_array<const double, 3>,
460+
&colors.converter,
461461
&colors,
462462
&convert_trans_affine,
463463
&trans)) {

src/_gtkagg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static PyObject *Py_agg_to_gtk_drawable(PyObject *self, PyObject *args, PyObject
3131
if (!PyArg_ParseTuple(args,
3232
"OO&O&:agg_to_gtk_drawable",
3333
&py_drawable,
34-
&numpy::convert_array<agg::int8u, 3>,
34+
&buffer.converter,
3535
&buffer,
3636
&convert_rect,
3737
&rect)) {

src/_image_wrapper.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,9 @@ static PyObject *image_fromarray(PyObject *self, PyObject *args, PyObject *kwds)
530530
numpy::array_view<const double, 2> grey_array;
531531
Image *result = NULL;
532532

533-
if (numpy::convert_array<const double, 3>(array, &color_array)) {
533+
if (color_array.converter(array, &color_array)) {
534534
CALL_CPP("fromarray", result = from_color_array(color_array, isoutput));
535-
} else if (numpy::convert_array<const double, 2>(array, &grey_array)) {
535+
} else if (grey_array.converter(array, &grey_array)) {
536536
CALL_CPP("fromarray", result = from_grey_array(grey_array, isoutput));
537537
} else {
538538
PyErr_SetString(PyExc_ValueError, "invalid array");
@@ -561,7 +561,7 @@ static PyObject *image_frombyte(PyObject *self, PyObject *args, PyObject *kwds)
561561
kwds,
562562
"O&|i:frombyte",
563563
(char **)names,
564-
&numpy::convert_array_contiguous<const uint8_t, 3>,
564+
&array.converter,
565565
&array,
566566
&isoutput)) {
567567
return NULL;
@@ -644,11 +644,11 @@ static PyObject *image_pcolor(PyObject *self, PyObject *args, PyObject *kwds)
644644

645645
if (!PyArg_ParseTuple(args,
646646
"O&O&O&II(ffff)i:pcolor",
647-
&numpy::convert_array_contiguous<const float, 1>,
647+
&x.converter,
648648
&x,
649-
&numpy::convert_array_contiguous<const float, 1>,
649+
&y.converter,
650650
&y,
651-
&numpy::convert_array_contiguous<const agg::int8u, 3>,
651+
&d.converter_contiguous,
652652
&d,
653653
&rows,
654654
&cols,
@@ -686,19 +686,19 @@ static PyObject *image_pcolor2(PyObject *self, PyObject *args, PyObject *kwds)
686686

687687
if (!PyArg_ParseTuple(args,
688688
"O&O&O&II(ffff)O&:pcolor2",
689-
&numpy::convert_array_contiguous<const double, 1>,
689+
&x.converter,
690690
&x,
691-
&numpy::convert_array_contiguous<const double, 1>,
691+
&y.converter,
692692
&y,
693-
&numpy::convert_array<const agg::int8u, 3>,
693+
&d.converter_contiguous,
694694
&d,
695695
&rows,
696696
&cols,
697697
&bounds[0],
698698
&bounds[1],
699699
&bounds[2],
700700
&bounds[3],
701-
&numpy::convert_array<const agg::int8u, 1>,
701+
&bg.converter,
702702
&bg)) {
703703
return NULL;
704704
}

src/_path_wrapper.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static PyObject *Py_points_in_path(PyObject *self, PyObject *args, PyObject *kwd
6767

6868
if (!PyArg_ParseTuple(args,
6969
"O&dO&O&:points_in_path",
70-
&numpy::convert_array<const double, 2>,
70+
&points.converter,
7171
&points,
7272
&r,
7373
&convert_path,
@@ -126,7 +126,7 @@ static PyObject *Py_points_on_path(PyObject *self, PyObject *args, PyObject *kwd
126126

127127
if (!PyArg_ParseTuple(args,
128128
"O&dO&O&:points_on_path",
129-
&numpy::convert_array<const double, 2>,
129+
&points.converter,
130130
&points,
131131
&r,
132132
&convert_path,
@@ -191,7 +191,7 @@ static PyObject *Py_update_path_extents(PyObject *self, PyObject *args, PyObject
191191
&trans,
192192
&convert_rect,
193193
&rect,
194-
&numpy::convert_array<double, 1>,
194+
&minpos.converter,
195195
&minpos,
196196
&ignore)) {
197197
return NULL;
@@ -261,9 +261,9 @@ static PyObject *Py_get_path_collection_extents(PyObject *self, PyObject *args,
261261
&convert_trans_affine,
262262
&master_transform,
263263
&pathsobj,
264-
&numpy::convert_array<const double, 3>,
264+
&transforms.converter,
265265
&transforms,
266-
&numpy::convert_array<const double, 2>,
266+
&offsets.converter,
267267
&offsets,
268268
&convert_trans_affine,
269269
&offset_trans)) {
@@ -317,9 +317,9 @@ static PyObject *Py_point_in_path_collection(PyObject *self, PyObject *args, PyO
317317
&convert_trans_affine,
318318
&master_transform,
319319
&pathsobj,
320-
&numpy::convert_array<const double, 3>,
320+
&transforms.converter,
321321
&transforms,
322-
&numpy::convert_array<const double, 2>,
322+
&offsets.converter,
323323
&offsets,
324324
&convert_trans_affine,
325325
&offset_trans,
@@ -422,7 +422,7 @@ static PyObject *Py_affine_transform(PyObject *self, PyObject *args, PyObject *k
422422

423423
if (!PyArg_ParseTuple(args,
424424
"O&O&:affine_transform",
425-
&numpy::convert_array<const double, 2>,
425+
&vertices.converter,
426426
&vertices,
427427
&convert_trans_affine,
428428
&trans)) {
@@ -449,7 +449,7 @@ static PyObject *Py_count_bboxes_overlapping_bbox(PyObject *self, PyObject *args
449449
"O&O&:count_bboxes_overlapping_bbox",
450450
&convert_rect,
451451
&bbox,
452-
&numpy::convert_array<const double, 3>,
452+
&bboxes.converter,
453453
&bboxes)) {
454454
return NULL;
455455
}
@@ -662,7 +662,7 @@ extern "C" {
662662
{"convert_path_to_polygons", (PyCFunction)Py_convert_path_to_polygons, METH_VARARGS, Py_convert_path_to_polygons__doc__},
663663
{"cleanup_path", (PyCFunction)Py_cleanup_path, METH_VARARGS, Py_cleanup_path__doc__},
664664
{"convert_to_svg", (PyCFunction)Py_convert_to_svg, METH_VARARGS, Py_convert_to_svg__doc__},
665-
{NULL, NULL, NULL, NULL}
665+
{NULL}
666666
};
667667

668668
struct module_state

src/_png.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
7979
kwds,
8080
"O&O|d:write_png",
8181
(char **)names,
82-
&numpy::convert_array<unsigned char, 3>,
82+
&buffer.converter,
8383
&buffer,
8484
&filein,
8585
&dpi)) {

src/numpy_cpp.h

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,43 @@ class array_view_accessors<AV, T, 3>
309309
self->m_shape + 1,
310310
self->m_strides + 1);
311311
}
312+
313+
312314
};
315+
316+
// /* These are converter functions designed for use with the "O&"
317+
// functionality of PyArg_ParseTuple and friends. */
318+
319+
// template<class T>
320+
// class array_converter {
321+
// public:
322+
// int operator()(PyObject *obj, void *arrp)
323+
// {
324+
// T *arr = (T *)arrp;
325+
326+
// if (!arr->set(obj)) {
327+
// return 0;
328+
// }
329+
330+
// return 1;
331+
// }
332+
// };
333+
334+
// template <class T>
335+
// class array_converter_contiguous {
336+
// public:
337+
// int operator()(PyObject *obj, void *arrp)
338+
// {
339+
// T *arr = (T *)arrp;
340+
341+
// if (!arr->set(obj, true)) {
342+
// return 0;
343+
// }
344+
345+
// return 1;
346+
// }
347+
// };
348+
313349
}
314350

315351
static npy_intp zeros[] = { 0, 0, 0 };
@@ -328,6 +364,7 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
328364

329365
public:
330366
typedef T value_type;
367+
331368
enum {
332369
ndim = ND
333370
};
@@ -446,35 +483,31 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
446483
Py_INCREF(m_arr);
447484
return (PyObject *)m_arr;
448485
}
449-
};
450486

451-
/* These are converter functions designed for use with the "O&"
452-
functionality of PyArg_ParseTuple and friends. */
487+
static int converter(PyObject *obj, void *arrp)
488+
{
489+
array_view<T, ND> *arr = (array_view<T, ND> *)arrp;
453490

454-
template <class T, int ND>
455-
int convert_array(PyObject *obj, void *arrp)
456-
{
457-
numpy::array_view<T, ND> *arr = (numpy::array_view<T, ND> *)arrp;
491+
if (!arr->set(obj)) {
492+
return 0;
493+
}
458494

459-
if (!arr->set(obj)) {
460-
return 0;
495+
return 1;
461496
}
462497

463-
return 1;
464-
}
498+
static int converter_contiguous(PyObject *obj, void *arrp)
499+
{
500+
array_view<T, ND> *arr = (array_view<T, ND> *)arrp;
465501

466-
template <class T, int ND>
467-
int convert_array_contiguous(PyObject *obj, void *arrp)
468-
{
469-
numpy::array_view<T, ND> *arr = (numpy::array_view<T, ND> *)arrp;
502+
if (!arr->set(obj, true)) {
503+
return 0;
504+
}
470505

471-
if (!arr->set(obj, true)) {
472-
return 0;
506+
return 1;
473507
}
474-
475-
return 1;
476-
}
508+
};
477509

478510
} // namespace numpy
479511

512+
480513
#endif

0 commit comments

Comments
 (0)
0