Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/matplotlib/tests/test_font_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from io import BytesIO
from io import BytesIO, StringIO
import multiprocessing
import os
from pathlib import Path
Expand Down Expand Up @@ -128,6 +128,24 @@ def test_find_ttc():
fig.savefig(BytesIO(), format="ps")


def test_find_invalid(tmpdir):
tmp_path = Path(tmpdir)

with pytest.raises(FileNotFoundError):
get_font(tmp_path / 'non-existent-font-name.ttf')

with pytest.raises(FileNotFoundError):
get_font(str(tmp_path / 'non-existent-font-name.ttf'))

with pytest.raises(FileNotFoundError):
get_font(bytes(tmp_path / 'non-existent-font-name.ttf'))

# Not really public, but get_font doesn't expose non-filename constructor.
from matplotlib.ft2font import FT2Font
with pytest.raises(TypeError, match='path or binary-mode file'):
FT2Font(StringIO())


@pytest.mark.skipif(sys.platform != 'linux', reason='Linux only')
def test_user_fonts_linux(tmpdir, monkeypatch):
font_test_file = 'mpltest.ttf'
Expand Down
6 changes: 3 additions & 3 deletions src/ft2font_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,14 @@ static int PyFT2Font_init(PyFT2Font *self, PyObject *args, PyObject *kwds)
|| !PyBytes_Check(data)) {
PyErr_SetString(PyExc_TypeError,
"First argument must be a path or binary-mode file object");
Py_CLEAR(data);
goto exit;
} else {
self->py_file = filename;
self->stream.close = NULL;
Py_INCREF(filename);
}
Py_CLEAR(data);

CALL_CPP_FULL(
"FT2Font", (self->x = new FT2Font(open_args, hinting_factor)),
Expand All @@ -505,9 +507,7 @@ static int PyFT2Font_init(PyFT2Font *self, PyObject *args, PyObject *kwds)
self->fname = filename;

exit:
Py_XDECREF(data);

return 0;
return PyErr_Occurred() ? -1 : 0;
}

static void PyFT2Font_dealloc(PyFT2Font *self)
Expand Down