Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort extensions alphabetically in error message #2

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)


Expand Down
14 changes: 6 additions & 8 deletions src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def _load_pilfont(self, filename: str) -> None:
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
Expand Down Expand Up @@ -771,14 +771,13 @@ def getlength(self, text: str | bytes, *args: Any, **kwargs: Any) -> float:

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)
Expand All @@ -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.

Expand Down Expand Up @@ -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:
Expand Down