Merge pull request #1 from radarhere/improve-error-messages

Renamed variable for first part of splitext to root
This commit is contained in:
Yngve Mardal Moe 2024-09-01 21:47:08 +02:00 committed by GitHub
commit 3bb180a254
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 27 deletions

View File

@ -5,6 +5,7 @@ import os
import re import re
import shutil import shutil
import sys import sys
import tempfile
from io import BytesIO from io import BytesIO
from pathlib import Path from pathlib import Path
from typing import Any, BinaryIO from typing import Any, BinaryIO
@ -460,15 +461,15 @@ def test_free_type_font_get_mask(font: ImageFont.FreeTypeFont) -> None:
assert mask.size == (108, 13) assert mask.size == (108, 13)
def test_load_raises_if_image_not_found(tmp_path) -> None: def test_load_when_image_not_found(tmp_path: Path) -> None:
font_path = tmp_path / "file.font" tmpfile = tmp_path / "file.font"
font_path.write_bytes(b"") tmpfile.write_bytes(b"")
with pytest.raises(OSError) as excinfo: tempfile = str(tmpfile)
ImageFont.load(font_path) with pytest.raises(OSError) as e:
ImageFont.load(tempfile)
pre = tmp_path / "file" root = os.path.splitext(tempfile)[0]
msg = f"cannot find glyph data file {pre}.{{png|gif|pbm}}" assert str(e.value) == f"cannot find glyph data file {root}.{{png|gif|pbm}}"
assert msg in str(excinfo.value)
def test_load_path_not_found() -> None: def test_load_path_not_found() -> None:
@ -476,26 +477,24 @@ def test_load_path_not_found() -> None:
filename = "somefilenamethatdoesntexist.ttf" filename = "somefilenamethatdoesntexist.ttf"
# Act/Assert # Act/Assert
with pytest.raises(OSError): with pytest.raises(OSError) as e:
ImageFont.load_path(filename) 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): with pytest.raises(OSError):
ImageFont.truetype(filename) ImageFont.truetype(filename)
def test_load_path_exisitng_path(tmp_path) -> None: def test_load_path_existing_path() -> None:
# First, the file doens't exist, so we don't suggest `load` with tempfile.NamedTemporaryFile() as tmp:
some_path = tmp_path / "file.ttf" with pytest.raises(OSError) as e:
with pytest.raises(OSError) as excinfo: ImageFont.load_path(tmp.name)
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 # The file exists, so the error message suggests to use `load` instead
some_path.write_bytes(b"") assert tmp.name in str(e.value)
with pytest.raises(OSError) as excinfo: assert " did you mean" in str(e.value)
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_non_font_bytes() -> None: def test_load_non_font_bytes() -> None:

View File

@ -98,13 +98,13 @@ class ImageFont:
def _load_pilfont(self, filename: str) -> None: def _load_pilfont(self, filename: str) -> None:
with open(filename, "rb") as fp: with open(filename, "rb") as fp:
image: ImageFile.ImageFile | None = None image: ImageFile.ImageFile | None = None
filename_body = os.path.splitext(filename)[0] root = os.path.splitext(filename)[0]
for ext in (".png", ".gif", ".pbm"): for ext in (".png", ".gif", ".pbm"):
if image: if image:
image.close() image.close()
try: try:
fullname = filename_body + ext fullname = root + ext
image = Image.open(fullname) image = Image.open(fullname)
except Exception: except Exception:
pass pass
@ -115,8 +115,7 @@ class ImageFont:
if image: if image:
image.close() image.close()
pre = filename_body msg = f"cannot find glyph data file {root}.{{png|gif|pbm}}"
msg = f"cannot find glyph data file {pre}.{{png|gif|pbm}}"
raise OSError(msg) raise OSError(msg)
self.file = fullname self.file = fullname
@ -937,7 +936,7 @@ def load_path(filename: str | bytes) -> ImageFont:
pass pass
msg = f"cannot find font file '{filename}' in `sys.path`" msg = f"cannot find font file '{filename}' in `sys.path`"
if os.path.exists(filename): 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) raise OSError(msg)