Skip to content

Commit

Permalink
Merge pull request #1 from radarhere/improve-error-messages
Browse files Browse the repository at this point in the history
Renamed variable for first part of splitext to root
  • Loading branch information
yngvem authored Sep 1, 2024
2 parents fcca8a3 + dbe78a0 commit 3bb180a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
45 changes: 22 additions & 23 deletions Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import shutil
import sys
import tempfile
from io import BytesIO
from pathlib import Path
from typing import Any, BinaryIO
Expand Down Expand Up @@ -460,42 +461,40 @@ def test_free_type_font_get_mask(font: ImageFont.FreeTypeFont) -> None:
assert mask.size == (108, 13)


def test_load_raises_if_image_not_found(tmp_path) -> None:
font_path = tmp_path / "file.font"
font_path.write_bytes(b"")
with pytest.raises(OSError) as excinfo:
ImageFont.load(font_path)
def test_load_when_image_not_found(tmp_path: Path) -> None:
tmpfile = tmp_path / "file.font"
tmpfile.write_bytes(b"")
tempfile = str(tmpfile)
with pytest.raises(OSError) as e:
ImageFont.load(tempfile)

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


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

# Act/Assert
with pytest.raises(OSError):
with pytest.raises(OSError) as e:
ImageFont.load_path(filename)

# The file doesn't exist, so don't suggest `load`
assert filename in str(e.value)
assert "did you mean" not in str(e.value)
with pytest.raises(OSError):
ImageFont.truetype(filename)


def test_load_path_exisitng_path(tmp_path) -> None:
# First, the file doens't exist, so we don't suggest `load`
some_path = tmp_path / "file.ttf"
with pytest.raises(OSError) as excinfo:
ImageFont.load_path(str(some_path))
assert str(some_path) in str(excinfo.value)
assert "did you mean" not in str(excinfo.value)

# The file exists, so the error message suggests to use `load` instead
some_path.write_bytes(b"")
with pytest.raises(OSError) as excinfo:
ImageFont.load_path(str(some_path))
assert str(some_path) in str(excinfo.value)
assert " did you mean" in str(excinfo.value)
def test_load_path_existing_path() -> None:
with tempfile.NamedTemporaryFile() as tmp:
with pytest.raises(OSError) as e:
ImageFont.load_path(tmp.name)

# The file exists, so the error message suggests to use `load` instead
assert tmp.name in str(e.value)
assert " did you mean" in str(e.value)


def test_load_non_font_bytes() -> None:
Expand Down
9 changes: 4 additions & 5 deletions src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ class ImageFont:
def _load_pilfont(self, filename: str) -> None:
with open(filename, "rb") as fp:
image: ImageFile.ImageFile | None = None
filename_body = os.path.splitext(filename)[0]
root = os.path.splitext(filename)[0]

for ext in (".png", ".gif", ".pbm"):
if image:
image.close()
try:
fullname = filename_body + ext
fullname = root + ext
image = Image.open(fullname)
except Exception:
pass
Expand All @@ -115,8 +115,7 @@ def _load_pilfont(self, filename: str) -> None:
if image:
image.close()

pre = filename_body
msg = f"cannot find glyph data file {pre}.{{png|gif|pbm}}"
msg = f"cannot find glyph data file {root}.{{png|gif|pbm}}"
raise OSError(msg)

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

raise OSError(msg)

Expand Down

0 comments on commit 3bb180a

Please sign in to comment.