Skip to content

Commit cdadf93

Browse files
committed
Improve error messages
1 parent 44c2ff3 commit cdadf93

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

Tests/test_imagefont.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,17 @@ def test_free_type_font_get_mask(font: ImageFont.FreeTypeFont) -> None:
460460
assert mask.size == (108, 13)
461461

462462

463+
def test_load_raises_if_image_not_found(tmp_path) -> None:
464+
font_path = tmp_path / "file.font"
465+
font_path.write_bytes(b"")
466+
with pytest.raises(OSError) as excinfo:
467+
ImageFont.load(font_path)
468+
469+
pre = tmp_path / "file"
470+
msg = f"cannot find glyph data file {pre}.{{png|gif|pbm}}"
471+
assert msg in str(excinfo.value)
472+
473+
463474
def test_load_path_not_found() -> None:
464475
# Arrange
465476
filename = "somefilenamethatdoesntexist.ttf"
@@ -471,6 +482,22 @@ def test_load_path_not_found() -> None:
471482
ImageFont.truetype(filename)
472483

473484

485+
def test_load_path_exisitng_path(tmp_path) -> None:
486+
# First, the file doens't exist, so we don't suggest `load`
487+
some_path = tmp_path / "file.ttf"
488+
with pytest.raises(OSError) as excinfo:
489+
ImageFont.load_path(str(some_path))
490+
assert str(some_path) in str(excinfo.value)
491+
assert "did you mean" not in str(excinfo.value)
492+
493+
# The file exists, so the error message suggests to use `load` instead
494+
some_path.write_bytes(b"")
495+
with pytest.raises(OSError) as excinfo:
496+
ImageFont.load_path(str(some_path))
497+
assert str(some_path) in str(excinfo.value)
498+
assert " did you mean" in str(excinfo.value)
499+
500+
474501
def test_load_non_font_bytes() -> None:
475502
with open("Tests/images/hopper.jpg", "rb") as f:
476503
with pytest.raises(OSError):

src/PIL/ImageFont.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,13 @@ class ImageFont:
9898
def _load_pilfont(self, filename: str) -> None:
9999
with open(filename, "rb") as fp:
100100
image: ImageFile.ImageFile | None = None
101+
filename_body = os.path.splitext(filename)[0]
102+
101103
for ext in (".png", ".gif", ".pbm"):
102104
if image:
103105
image.close()
104106
try:
105-
fullname = os.path.splitext(filename)[0] + ext
107+
fullname = filename_body + ext
106108
image = Image.open(fullname)
107109
except Exception:
108110
pass
@@ -112,7 +114,9 @@ def _load_pilfont(self, filename: str) -> None:
112114
else:
113115
if image:
114116
image.close()
115-
msg = "cannot find glyph data file"
117+
118+
pre = filename_body
119+
msg = f"cannot find glyph data file {pre}.{{png|gif|pbm}}"
116120
raise OSError(msg)
117121

118122
self.file = fullname
@@ -224,7 +228,7 @@ def __init__(
224228
raise core.ex
225229

226230
if size <= 0:
227-
msg = "font size must be greater than 0"
231+
msg = f"font size must be greater than 0, not {size}"
228232
raise ValueError(msg)
229233

230234
self.path = font
@@ -774,6 +778,8 @@ def load(filename: str) -> ImageFont:
774778
:param filename: Name of font file.
775779
:return: A font object.
776780
:exception OSError: If the file could not be read.
781+
782+
.. seealso:: :py:func:`PIL.ImageFont.truetype`
777783
"""
778784
f = ImageFont()
779785
f._load_pilfont(filename)
@@ -850,6 +856,8 @@ def truetype(
850856
:return: A font object.
851857
:exception OSError: If the file could not be read.
852858
:exception ValueError: If the font size is not greater than zero.
859+
860+
.. seealso:: :py:func:`PIL.ImageFont.load`
853861
"""
854862

855863
def freetype(font: StrOrBytesPath | BinaryIO | None) -> FreeTypeFont:
@@ -927,7 +935,10 @@ def load_path(filename: str | bytes) -> ImageFont:
927935
return load(os.path.join(directory, filename))
928936
except OSError:
929937
pass
930-
msg = "cannot find font file"
938+
msg = f"cannot find font file '{filename}' in `sys.path`"
939+
if os.path.exists(filename):
940+
msg += f" did you mean `ImageFont.load({filename})` instead?"
941+
931942
raise OSError(msg)
932943

933944

0 commit comments

Comments
 (0)