8000 Merge branch 'pybind11-cleanup' into freethread · matplotlib/matplotlib@fd1acad · GitHub
[go: up one dir, main page]

Skip to content

Commit fd1acad

Browse files
committed
Merge branch 'pybind11-cleanup' into freethread
2 parents 78b80a5 + 3e3c5bd commit fd1acad

19 files changed

+185
-1576
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ jobs:
261261
# Preinstall build requirements to enable no-build-isolation builds.
262262
python -m pip install --upgrade $PRE \
263263
'contourpy>=1.0.1' cycler fonttools kiwisolver importlib_resources \
264-
numpy packaging pillow 'pyparsing!=3.1.0' python-dateutil setuptools-scm \
264+
packaging pillow 'pyparsing!=3.1.0' python-dateutil setuptools-scm \
265265
'meson-python>=0.13.1' 'pybind11>=2.6' \
266266
-r requirements/testing/all.txt \
267267
${{ matrix.extra-requirements }}

pyproject.toml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ requires-python = ">=3.10"
4646
# Should be a copy of the build dependencies below.
4747
dev = [
4848
"meson-python>=0.13.1",
49-
"numpy>=1.25",
5049
"pybind11>=2.6,!=2.13.3",
5150
"setuptools_scm>=7",
5251
# Not required by us but setuptools_scm without a version, cso _if_
@@ -73,18 +72,6 @@ requires = [
7372
"meson-python>=0.13.1",
7473
"pybind11>=2.6,!=2.13.3",
7574
"setuptools_scm>=7",
76-
77-
# Comments on numpy build requirement range:
78-
#
79-
# 1. >=2.0.x is the numpy requirement for wheel builds for distribution
80-
# on PyPI - building against 2.x yields wheels that are also
81-
# ABI-compatible with numpy 1.x at runtime.
82-
# 2. Note that building against numpy 1.x works fine too - users and
83-
# redistributors can do this by installing the numpy version they like
84-
# and disabling build isolation.
85-
# 3. The <2.3 upper bound is for matching the numpy deprecation policy,
86-
# it should not be loosened.
87-
"numpy>=2.0.0rc1,<2.3",
8875
]
8976

9077
[tool.meson-python.args]
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
pybind11!=2.13.3
22
meson-python
3-
numpy<2.1.0
43
setuptools-scm

requirements/testing/mypy.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ contourpy>=1.0.1
1818
cycler>=0.10
1919
fonttools>=4.22.0
2020
kiwisolver>=1.3.1
21-
numpy>=1.19
2221
packaging>=20.0
2322
pillow>=8
2423
pyparsing>=2.3.1

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/_backend_agg_wrapper.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
#include <pybind11/numpy.h>
33
#include <pybind11/stl.h>
44
#include "mplutils.h"
5-
#include "numpy_cpp.h"
65
#include "py_converters.h"
76
#include "_backend_agg.h"
8-
#include "py_converters_11.h"
97

108
namespace py = pybind11;
119
using namespace pybind11::literals;
@@ -189,14 +187,6 @@ PyRendererAgg_draw_gouraud_triangles(RendererAgg *self,
189187

190188
PYBIND11_MODULE(_backend_agg, m)
191189
{
192-
auto ia = [m]() -> const void* {
193-
import_array();
194-
return &m;
195-
};
196-
if (ia() == NULL) {
197-
throw py::error_already_set();
198-
}
199-
200190
py::class_<RendererAgg>(m, "RendererAgg", py::buffer_protocol())
201191
.def(py::init<unsigned int, unsigned int, double>(),
202192
"width"_a, "height"_a, "dpi"_a)

src/_image_wrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <pybind11/numpy.h>
33

44
#include "_image_resample.h"
5-
#include "py_converters_11.h"
5+
#include "py_converters.h"
66

77
namespace py = pybind11;
88
using namespace pybind11::literals;

src/_path.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include "path_converters.h"
2020
#include "_backend_agg_basic_types.h"
21-
#include "numpy_cpp.h"
2221

2322
const size_t NUM_VERTICES[] = { 1, 1, 1, 2, 3 };
2423

@@ -1004,15 +1003,15 @@ void convert_path_to_polygons(PathIterator &path,
10041003

10051004
template <class VertexSource>
10061005
void
1007-
__cleanup_path(VertexSource &source, std::vector<double> &vertices, std::vector<npy_uint8> &codes)
1006+
__cleanup_path(VertexSource &source, std::vector<double> &vertices, std::vector<uint8_t> &codes)
10081007
{
10091008
unsigned code;
10101009
double x, y;
10111010
do {
10121011
code = source.vertex(&x, &y);
10131012
vertices.push_back(x);
10141013
vertices.push_back(y);
1015-
codes.push_back((npy_uint8)code);
1014+
codes.push_back(static_cast<uint8_t>(code));
10161015
} while (code != agg::path_cmd_stop);
10171016
}
10181017

src/_path_wrapper.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77
#include <string>
88
#include <vector>
99

10-
#include "numpy_cpp.h"
11-
1210
#include "_path.h"
1311

1412
#include "_backend_agg_basic_types.h"
1513
#include "py_adaptors.h"
1614
#include "py_converters.h"
17-
#include "py_converters_11.h"
1815

1916
namespace py = pybind11;
2017
using namespace pybind11::literals;
@@ -184,9 +181,7 @@ Py_affine_transform(py::array_t<double, py::array::c_style | py::array::forcecas
184181
if (vertices_arr.ndim() == 2) {
185182
auto vertices = vertices_arr.unchecked<2>();
186183

187-
if(!check_trailing_shape(vertices, "vertices", 2)) {
188-
throw py::error_already_set();
189-
}
184+
check_trailing_shape(vertices, "vertices", 2);
190185

191186
py::ssize_t dims[] = { vertices.shape(0), 2 };
192187
py::array_t<double> result(dims);
@@ -267,7 +262,7 @@ Py_cleanup_path(mpl::PathIterator path, agg::trans_affine trans, bool remove_nan
267262
bool do_clip = (clip_rect.x1 < clip_rect.x2 && clip_rect.y1 < clip_rect.y2);
268263

269264
std::vector<double> vertices;
270-
std::vector<npy_uint8> codes;
265+
std::vector<uint8_t> codes;
271266

272267
cleanup_path(path, trans, remove_nans, do_clip, clip_rect, snap_mode, stroke_width,
273268
*simplify, return_curves, sketch, vertices, codes);
@@ -375,14 +370,6 @@ Py_is_sorted_and_has_non_nan(py::object obj)
375370

376371
PYBIND11_MODULE(_path, m)
377372
{
378-
auto ia = [m]() -> const void* {
379-
import_array();
380-
return &m;
381-
};
382-
if (ia() == NULL) {
383-
throw py::error_already_set();
384-
}
385-
386373
m.def("point_in_path", &Py_point_in_path,
387374
"x"_a, "y"_a, "radius"_a, "path"_a, "trans"_a);
388375
m.def("points_in_path", &Py_points_in_path,

src/ft2font_wrapper.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
#include "ft2font.h"
77
#include "_enums.h"
8-
#include "numpy/arrayobject.h"
98

109
#include <set>
1110
#include <sstream>
@@ -1597,14 +1596,6 @@ ft2font__getattr__(std::string name) {
15971596

15981597
PYBIND11_MODULE(ft2font, m)
15991598
{
1600-
auto ia = [m]() -> const void* {
1601-
import_array();
1602-
return &m;
1603-
};
1604-
if (ia() == NULL) {
1605-
throw py::error_already_set();
1606-
}
1607-
16081599
if (FT_Init_FreeType(&_ft2Library)) { // initialize library
16091600
throw std::runtime_error("Could not initialize the freetype2 library");
16101601
}

src/meson.build

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,3 @@
1-
# NumPy include directory - needed in all submodules
2-
# The try-except is needed because when things are split across drives on Windows, there
3-
# is no relative path and an exception gets raised. There may be other such cases, so add
4-
# a catch-all and switch to an absolute path. Relative paths are needed when for example
5-
# a virtualenv is placed inside the source tree; Meson rejects absolute paths to places
6-
# inside the source tree.
7-
# For cross-compilation it is often not possible to run the Python interpreter in order
8-
# to retrieve numpy's include directory. It can be specified in the cross file instead:
9-
#
10-
# [properties]
11-
# numpy-include-dir = /abspath/to/host-pythons/site-packages/numpy/core/include
12-
#
13-
# This uses the path as is, and avoids running the interpreter.
14-
incdir_numpy = meson.get_external_property('numpy-include-dir', 'not-given')
15-
if incdir_numpy == 'not-given'
16-
incdir_numpy = run_command(py3,
17-
[
18-
'-c',
19-
'''import os
20-
import numpy as np
21-
try:
22-
incdir = os.path.relpath(np.get_include())
23-
except Exception:
24-
incdir = np.get_include()
25-
print(incdir)'''
26-
],
27-
check: true
28-
).stdout().strip()
29-
endif
30-
numpy_dep = declare_dependency(
31-
compile_args: [
32-
'-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION',
33-
# Allow NumPy's printf format specifiers in C++.
34-
'-D__STDC_FORMAT_MACROS=1',
35-
],
36-
include_directories: include_directories(incdir_numpy),
37-
dependencies: py3_dep,
38-
)
39-
401
# For cross-compilation it is often not possible to run the Python interpreter in order
412
# to retrieve the platform-specific /dev/null. It can be specified in the cross file
423
# instead:
@@ -73,12 +34,10 @@ extension_data = {
7334
'_backend_agg': {
7435
'subdir': 'matplotlib/backends',
7536
'sources': files(
76-
'py_converters.cpp',
77-
'py_converters_11.cpp',
7837
'_backend_agg.cpp',
7938
'_backend_agg_wrapper.cpp',
8039
),
81-
'dependencies': [agg_dep, numpy_dep, freetype_dep, pybind11_dep],
40+
'dependencies': [agg_dep, freetype_dep, pybind11_dep],
8241
},
8342
'_c_internal_utils': {
8443
'subdir': 'matplotlib',
@@ -92,10 +51,9 @@ extension_data = {
9251
'sources': files(
9352
'ft2font.cpp',
9453
'ft2font_wrapper.cpp',
95-
'py_converters.cpp',
9654
),
9755
'dependencies': [
98-
freetype_dep, pybind11_dep, numpy_dep, agg_dep.partial_dependency(includes: true),
56+
freetype_dep, pybind11_dep, agg_dep.partial_dependency(includes: true),
9957
],
10058
'cpp_args': [
10159
'-DFREETYPE_BUILD_TYPE="@0@"'.format(
@@ -107,7 +65,7 @@ extension_data = {
10765
'subdir': 'matplotlib',
10866
'sources': files(
10967
'_image_wrapper.cpp',
110-
'py_converters_11.cpp',
68+
'py_converters.cpp',
11169
),
11270
'dependencies': [
11371
pybind11_dep,
@@ -118,11 +76,9 @@ extension_data = {
11876
'_path': {
11977
'subdir': 'matplotlib',
12078
'sources': files(
121-
'py_converters.cpp',
122-
'py_converters_11.cpp',
12379
'_path_wrapper.cpp',
12480
),
125-
'dependencies': [numpy_dep, agg_dep, pybind11_dep],
81+
'dependencies': [agg_dep, pybind11_dep],
12682
},
12783
'_qhull': {
12884
'subdir': 'matplotlib',

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

0 commit comments

Comments
 (0)
0