Skip to content

Commit 3bb180a

Browse files
authored
Merge pull request #1 from radarhere/improve-error-messages
Renamed variable for first part of splitext to root
2 parents fcca8a3 + dbe78a0 commit 3bb180a

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

Tests/test_imagefont.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import re
66
import shutil
77
import sys
8+
import tempfile
89
from io import BytesIO
910
from pathlib import Path
1011
from typing import Any, BinaryIO
@@ -460,42 +461,40 @@ def test_free_type_font_get_mask(font: ImageFont.FreeTypeFont) -> None:
460461
assert mask.size == (108, 13)
461462

462463

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)
464+
def test_load_when_image_not_found(tmp_path: Path) -> None:
465+
tmpfile = tmp_path / "file.font"
466+
tmpfile.write_bytes(b"")
467+
tempfile = str(tmpfile)
468+
with pytest.raises(OSError) as e:
469+
ImageFont.load(tempfile)
468470

469-
pre = tmp_path / "file"
470-
msg = f"cannot find glyph data file {pre}.{{png|gif|pbm}}"
471-
assert msg in str(excinfo.value)
471+
root = os.path.splitext(tempfile)[0]
472+
assert str(e.value) == f"cannot find glyph data file {root}.{{png|gif|pbm}}"
472473

473474

474475
def test_load_path_not_found() -> None:
475476
# Arrange
476477
filename = "somefilenamethatdoesntexist.ttf"
477478

478479
# Act/Assert
479-
with pytest.raises(OSError):
480+
with pytest.raises(OSError) as e:
480481
ImageFont.load_path(filename)
482+
483+
# The file doesn't exist, so don't suggest `load`
484+
assert filename in str(e.value)
485+
assert "did you mean" not in str(e.value)
481486
with pytest.raises(OSError):
482487
ImageFont.truetype(filename)
483488

484489

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)
490+
def test_load_path_existing_path() -> None:
491+
with tempfile.NamedTemporaryFile() as tmp:
492+
with pytest.raises(OSError) as e:
493+
ImageFont.load_path(tmp.name)
494+
495+
# The file exists, so the error message suggests to use `load` instead
496+
assert tmp.name in str(e.value)
497+
assert " did you mean" in str(e.value)
499498

500499

501500
def test_load_non_font_bytes() -> None:

src/PIL/ImageFont.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +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]
101+
root = os.path.splitext(filename)[0]
102102

103103
for ext in (".png", ".gif", ".pbm"):
104104
if image:
105105
image.close()
106106
try:
107-
fullname = filename_body + ext
107+
fullname = root + ext
108108
image = Image.open(fullname)
109109
except Exception:
110110
pass
@@ -115,8 +115,7 @@ def _load_pilfont(self, filename: str) -> None:
115115
if image:
116116
image.close()
117117

118-
pre = filename_body
119-
msg = f"cannot find glyph data file {pre}.{{png|gif|pbm}}"
118+
msg = f"cannot find glyph data file {root}.{{png|gif|pbm}}"
120119
raise OSError(msg)
121120

122121
self.file = fullname
@@ -937,7 +936,7 @@ def load_path(filename: str | bytes) -> ImageFont:
937936
pass
938937
msg = f"cannot find font file '{filename}' in `sys.path`"
939938
if os.path.exists(filename):
940-
msg += f" did you mean `ImageFont.load({filename})` instead?"
939+
msg += f", did you mean `ImageFont.load({filename})` instead?"
941940

942941
raise OSError(msg)
943942

0 commit comments

Comments
 (0)