diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 3388c2055..0ee3b979e 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -420,6 +420,10 @@ class TestImageFont(PillowTestCase): self.assertRaises(IOError, ImageFont.load_path, filename) self.assertRaises(IOError, ImageFont.truetype, filename) + def test_load_non_font_bytes(self): + with open("Tests/images/hopper.jpg", "rb") as f: + self.assertRaises(IOError, ImageFont.truetype, f) + def test_default_font(self): # Arrange txt = 'This is a "better than nothing" default font.' diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 7074a70c0..f43f95b9a 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -545,6 +545,8 @@ def truetype(font=None, size=10, index=0, encoding="", layout_engine=None): try: return freetype(font) except IOError: + if not isPath(font): + raise ttf_filename = os.path.basename(font) dirs = [] diff --git a/src/_imagingft.c b/src/_imagingft.c index f6bd787ef..28e6d2b5e 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -315,6 +315,7 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw) if (error) { if (self->font_bytes) { PyMem_Free(self->font_bytes); + self->font_bytes = NULL; } Py_DECREF(self); return geterror(error);