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

Skip to content

Commit 35458ae

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 6394dc6 commit 35458ae

File tree

2 files changed

+8
-18
lines changed

2 files changed

+8
-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: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ const char *PyFT2Font_init__doc__ =
172172

173173
static PyFT2Font *
174174
PyFT2Font_init(py::object filename, long hinting_factor = 8,
175-
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)
176177
{
177178
if (hinting_factor <= 0) {
178179
throw py::value_error("hinting_factor must be greater than 0");
@@ -192,24 +193,13 @@ PyFT2Font_init(py::object filename, long hinting_factor = 8,
192193
open_args.stream = &self->stream;
193194

194195
std::vector<FT2Font *> fallback_fonts;
195-
if (!fallback_list_or_none.is_none()) {
196-
if (!py::isinstance<py::list>(fallback_list_or_none)) {
197-
throw py::type_error("Fallback list must be a list");
198-
}
199-
auto fallback_list = fallback_list_or_none.cast<py::list>();
200-
201-
// go through fallbacks once to make sure the types are right
202-
for (auto item : fallback_list) {
203-
if (!py::isinstance<PyFT2Font>(item)) {
204-
throw py::type_error("Fallback fonts must be FT2Font objects.");
205-
}
206-
}
207-
// go through a second time to add them to our lists
208-
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()) {
209199
self->fallbacks.append(item);
210200
// Also (locally) cache the underlying FT2Font objects. As long as
211201
// the Python objects are kept alive, these pointer are good.
212-
FT2Font *fback = py::cast<PyFT2Font *>(item)->x;
202+
FT2Font *fback = item->x;
213203
fallback_fonts.push_back(fback);
214204
}
215205
}

0 commit comments

Comments
 (0)