From e14072e9738275ffe2898f32d36cbd2f3c687cf3 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 7 Sep 2024 19:08:07 +1000 Subject: [PATCH 1/3] Added further detail --- src/PIL/ImageFont.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 43e86a7ed..abfa4aa80 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -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: From dbe979d032afed04b93f9ce1aaa9c7f8e23542b1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 7 Sep 2024 19:09:01 +1000 Subject: [PATCH 2/3] Sort extensions alphabetically in error message --- Tests/test_imagefont.py | 2 +- src/PIL/ImageFont.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 66e6947c2..32cd1db8a 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -469,7 +469,7 @@ def test_load_when_image_not_found(tmp_path: Path) -> None: ImageFont.load(tempfile) root = os.path.splitext(tempfile)[0] - assert str(e.value) == f"cannot find glyph data file {root}.{{png|gif|pbm}}" + assert str(e.value) == f"cannot find glyph data file {root}.{{gif|pbm|png}}" def test_load_path_not_found() -> None: diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index abfa4aa80..b6d69019d 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -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 From 547832fd592ee1392533be5bf935c19c690a665f Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 7 Sep 2024 19:49:21 +1000 Subject: [PATCH 3/3] Use tempfile.NamedTemporaryFile --- Tests/test_imagefont.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 32cd1db8a..e3d847567 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -461,14 +461,15 @@ 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] + 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}}" @@ -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)