mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-13 05:06:49 +03:00
Improve error messages
This commit is contained in:
parent
44c2ff3f0b
commit
cdadf931e3
|
@ -460,6 +460,17 @@ 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:
|
||||||
|
font_path = tmp_path / "file.font"
|
||||||
|
font_path.write_bytes(b"")
|
||||||
|
with pytest.raises(OSError) as excinfo:
|
||||||
|
ImageFont.load(font_path)
|
||||||
|
|
||||||
|
pre = tmp_path / "file"
|
||||||
|
msg = f"cannot find glyph data file {pre}.{{png|gif|pbm}}"
|
||||||
|
assert msg in str(excinfo.value)
|
||||||
|
|
||||||
|
|
||||||
def test_load_path_not_found() -> None:
|
def test_load_path_not_found() -> None:
|
||||||
# Arrange
|
# Arrange
|
||||||
filename = "somefilenamethatdoesntexist.ttf"
|
filename = "somefilenamethatdoesntexist.ttf"
|
||||||
|
@ -471,6 +482,22 @@ def test_load_path_not_found() -> None:
|
||||||
ImageFont.truetype(filename)
|
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_non_font_bytes() -> None:
|
def test_load_non_font_bytes() -> None:
|
||||||
with open("Tests/images/hopper.jpg", "rb") as f:
|
with open("Tests/images/hopper.jpg", "rb") as f:
|
||||||
with pytest.raises(OSError):
|
with pytest.raises(OSError):
|
||||||
|
|
|
@ -98,11 +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]
|
||||||
|
|
||||||
for ext in (".png", ".gif", ".pbm"):
|
for ext in (".png", ".gif", ".pbm"):
|
||||||
if image:
|
if image:
|
||||||
image.close()
|
image.close()
|
||||||
try:
|
try:
|
||||||
fullname = os.path.splitext(filename)[0] + ext
|
fullname = filename_body + ext
|
||||||
image = Image.open(fullname)
|
image = Image.open(fullname)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
@ -112,7 +114,9 @@ class ImageFont:
|
||||||
else:
|
else:
|
||||||
if image:
|
if image:
|
||||||
image.close()
|
image.close()
|
||||||
msg = "cannot find glyph data file"
|
|
||||||
|
pre = filename_body
|
||||||
|
msg = f"cannot find glyph data file {pre}.{{png|gif|pbm}}"
|
||||||
raise OSError(msg)
|
raise OSError(msg)
|
||||||
|
|
||||||
self.file = fullname
|
self.file = fullname
|
||||||
|
@ -224,7 +228,7 @@ class FreeTypeFont:
|
||||||
raise core.ex
|
raise core.ex
|
||||||
|
|
||||||
if size <= 0:
|
if size <= 0:
|
||||||
msg = "font size must be greater than 0"
|
msg = f"font size must be greater than 0, not {size}"
|
||||||
raise ValueError(msg)
|
raise ValueError(msg)
|
||||||
|
|
||||||
self.path = font
|
self.path = font
|
||||||
|
@ -774,6 +778,8 @@ def load(filename: str) -> ImageFont:
|
||||||
:param filename: Name of font file.
|
:param filename: Name of font file.
|
||||||
:return: A font object.
|
:return: A font object.
|
||||||
:exception OSError: If the file could not be read.
|
:exception OSError: If the file could not be read.
|
||||||
|
|
||||||
|
.. seealso:: :py:func:`PIL.ImageFont.truetype`
|
||||||
"""
|
"""
|
||||||
f = ImageFont()
|
f = ImageFont()
|
||||||
f._load_pilfont(filename)
|
f._load_pilfont(filename)
|
||||||
|
@ -850,6 +856,8 @@ def truetype(
|
||||||
:return: A font object.
|
:return: A font object.
|
||||||
:exception OSError: If the file could not be read.
|
:exception OSError: If the file could not be read.
|
||||||
:exception ValueError: If the font size is not greater than zero.
|
:exception ValueError: If the font size is not greater than zero.
|
||||||
|
|
||||||
|
.. seealso:: :py:func:`PIL.ImageFont.load`
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def freetype(font: StrOrBytesPath | BinaryIO | None) -> FreeTypeFont:
|
def freetype(font: StrOrBytesPath | BinaryIO | None) -> FreeTypeFont:
|
||||||
|
@ -927,7 +935,10 @@ def load_path(filename: str | bytes) -> ImageFont:
|
||||||
return load(os.path.join(directory, filename))
|
return load(os.path.join(directory, filename))
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
msg = "cannot find font file"
|
msg = f"cannot find font file '{filename}' in `sys.path`"
|
||||||
|
if os.path.exists(filename):
|
||||||
|
msg += f" did you mean `ImageFont.load({filename})` instead?"
|
||||||
|
|
||||||
raise OSError(msg)
|
raise OSError(msg)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user