Skip to content

Commit e9190b4

Browse files
committed
Use STL type for FT2Font fallback list
This allows pybind11 to generate the type hints for us, and it also takes care of checking the list and its contents are the right type.
1 parent b3e6500 commit e9190b4

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

lib/matplotlib/tests/test_ft2font.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ def test_ft2font_invalid_args(tmp_path):
152152
with pytest.raises(ValueError, match='hinting_factor must be greater than 0'):
153153
ft2font.FT2Font(file, 0)
154154

155-
with pytest.raises(TypeError, match='Fallback list must be a list'):
155+
with pytest.raises(TypeError, match='incompatible constructor arguments'):
156156
# failing to be a list will fail before the 0
157157
ft2font.FT2Font(file, _fallback_list=(0,)) # type: ignore[arg-type]
158-
with pytest.raises(TypeError, match='Fallback fonts must be FT2Font objects.'):
158+
with pytest.raises(TypeError, match='incompatible constructor arguments'):
159159
ft2font.FT2Font(file, _fallback_list=[0]) # type: ignore[list-item]
160160

161161
# kerning_factor argument.

src/ft2font_wrapper.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
22
#include <pybind11/pybind11.h>
33
#include <pybind11/numpy.h>
4+
#include <pybind11/stl.h>
45

56
#include "ft2font.h"
67
#include "numpy/arrayobject.h"
@@ -171,7 +172,8 @@ const char *PyFT2Font_init__doc__ =
171172

172173
static PyFT2Font *
173174
PyFT2Font_init(py::object filename, long hinting_factor = 8,
174-
py::object fallback_list_or_none = py::none(), int kerning_factor = 0)
175+
std::optional<std::vector<PyFT2Font *>> fallback_list = std::nullopt,
176+
int kerning_factor = 0)
175177
{
176178
if (hinting_factor <= 0) {
177179
throw py::value_error("hinting_factor must be greater than 0");
@@ -191,24 +193,13 @@ PyFT2Font_init(py::object filename, long hinting_factor = 8,
191193
open_args.stream = &self->stream;
192194

193195
std::vector<FT2Font *> fallback_fonts;
194-
if (!fallback_list_or_none.is_none()) {
195-
if (!py::isinstance<py::list>(fallback_list_or_none)) {
196-
throw py::type_error("Fallback list must be a list");
197-
}
198-
auto fallback_list = fallback_list_or_none.cast<py::list>();
199-
200-
// go through fallbacks once to make sure the types are right
201-
for (auto item : fallback_list) {
202-
if (!py::isinstance<PyFT2Font>(item)) {
203-
throw py::type_error("Fallback fonts must be FT2Font objects.");
204-
}
205-
}
206-
// go through a second time to add them to our lists
207-
for (auto item : fallback_list) {
196+
if (fallback_list) {
197+
// go through fallbacks to add them to our lists
198+
for (auto item : fallback_list.value()) {
208199
self->fallbacks.append(item);
209200
// Also (locally) cache the underlying FT2Font objects. As long as
210201
// the Python objects are kept alive, these pointer are good.
211-
FT2Font *fback = py::cast<PyFT2Font *>(item)->x;
202+
FT2Font *fback = item->x;
212203
fallback_fonts.push_back(fback);
213204
}
214205
}

0 commit comments

Comments
 (0)