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

Sort extensions alphabetically in error message
This commit is contained in:
Yngve Mardal Moe 2024-09-10 20:45:53 +02:00 committed by GitHub
commit 7d223fbcaa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 17 deletions

View File

@ -461,15 +461,16 @@ def test_free_type_font_get_mask(font: ImageFont.FreeTypeFont) -> None:
assert mask.size == (108, 13)
def test_load_when_image_not_found(tmp_path: Path) -> None:
tmpfile = tmp_path / "file.font"
tmpfile.write_bytes(b"")
tempfile = str(tmpfile)
def test_load_when_image_not_found() -> None:
with tempfile.NamedTemporaryFile(delete=False) as tmp:
pass
with pytest.raises(OSError) as e:
ImageFont.load(tempfile)
ImageFont.load(tmp.name)
root = os.path.splitext(tempfile)[0]
assert str(e.value) == f"cannot find glyph data file {root}.{{png|gif|pbm}}"
os.unlink(tmp.name)
root = os.path.splitext(tmp.name)[0]
assert str(e.value) == f"cannot find glyph data file {root}.{{gif|pbm|png}}"
def test_load_path_not_found() -> None:
@ -492,8 +493,8 @@ def test_load_path_existing_path() -> None:
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)
# 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)

View File

@ -115,7 +115,7 @@ class ImageFont:
if image:
image.close()
msg = f"cannot find glyph data file {root}.{{png|gif|pbm}}"
msg = f"cannot find glyph data file {root}.{{gif|pbm|png}}"
raise OSError(msg)
self.file = fullname
@ -771,14 +771,13 @@ class TransposedFont:
def load(filename: str) -> ImageFont:
"""
Load a font file. This function loads a font object from the given
bitmap font file, and returns the corresponding font object.
Load a font file. This function loads a font object from the given
bitmap font file, and returns the corresponding font object. For loading TrueType
or OpenType fonts instead, see :py:func:`~PIL.ImageFont.truetype`.
:param filename: Name of font file.
:return: A font object.
:exception OSError: If the file could not be read.
.. seealso:: :py:func:`PIL.ImageFont.truetype`
"""
f = ImageFont()
f._load_pilfont(filename)
@ -794,7 +793,8 @@ def truetype(
) -> FreeTypeFont:
"""
Load a TrueType or OpenType font from a file or file-like object,
and create a font object.
and create a font object. For loading bitmap fonts instead,
see :py:func:`~PIL.ImageFont.load` and :py:func:`~PIL.ImageFont.load_path`.
This function loads a font object from the given file or file-like
object, and creates a font object for a font of the given size.
@ -855,8 +855,6 @@ def truetype(
:return: A font object.
:exception OSError: If the file could not be read.
:exception ValueError: If the font size is not greater than zero.
.. seealso:: :py:func:`PIL.ImageFont.load`
"""
def freetype(font: StrOrBytesPath | BinaryIO | None) -> FreeTypeFont: