From 5575c1d072a127e10cf0077ca9dcb7d54ea06c06 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 8 Mar 2025 09:56:00 +1100 Subject: [PATCH 1/2] Test missing frame size --- Tests/test_file_fli.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Tests/test_file_fli.py b/Tests/test_file_fli.py index 8a95af62d..2f39adc69 100644 --- a/Tests/test_file_fli.py +++ b/Tests/test_file_fli.py @@ -1,5 +1,6 @@ from __future__ import annotations +import io import warnings import pytest @@ -132,6 +133,15 @@ def test_eoferror() -> None: im.seek(n_frames - 1) +def test_missing_frame_size() -> None: + with open(animated_test_file, "rb") as fp: + data = fp.read() + data = data[:6188] + with Image.open(io.BytesIO(data)) as im: + with pytest.raises(EOFError, match="missing frame size"): + im.seek(1) + + def test_seek_tell() -> None: with Image.open(animated_test_file) as im: layer_number = im.tell() From baa299a6f42d691653f3a6dcfb067be567319c27 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 8 Mar 2025 09:54:46 +1100 Subject: [PATCH 2/2] Moved code outside of context manager --- Tests/test_file_jpeg2k.py | 10 +++++----- Tests/test_file_pcx.py | 4 ++-- Tests/test_font_bdf.py | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index 01172fdbb..e429610ad 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -317,12 +317,12 @@ def test_grayscale_four_channels() -> None: with open("Tests/images/rgb_trns_ycbc.jp2", "rb") as fp: data = fp.read() - # Change color space to OPJ_CLRSPC_GRAY - data = data[:76] + b"\x11" + data[77:] + # Change color space to OPJ_CLRSPC_GRAY + data = data[:76] + b"\x11" + data[77:] - with Image.open(BytesIO(data)) as im: - im.load() - assert im.mode == "RGBA" + with Image.open(BytesIO(data)) as im: + im.load() + assert im.mode == "RGBA" @pytest.mark.skipif( diff --git a/Tests/test_file_pcx.py b/Tests/test_file_pcx.py index aa24189f4..21c32268c 100644 --- a/Tests/test_file_pcx.py +++ b/Tests/test_file_pcx.py @@ -40,7 +40,7 @@ def test_sanity(tmp_path: Path) -> None: def test_bad_image_size() -> None: with open("Tests/images/pil184.pcx", "rb") as fp: data = fp.read() - data = data[:4] + b"\xff\xff" + data[6:] + data = data[:4] + b"\xff\xff" + data[6:] b = io.BytesIO(data) with pytest.raises(SyntaxError, match="bad PCX image size"): @@ -51,7 +51,7 @@ def test_bad_image_size() -> None: def test_unknown_mode() -> None: with open("Tests/images/pil184.pcx", "rb") as fp: data = fp.read() - data = data[:3] + b"\xff" + data[4:] + data = data[:3] + b"\xff" + data[4:] b = io.BytesIO(data) with pytest.raises(OSError, match="unknown PCX mode"): diff --git a/Tests/test_font_bdf.py b/Tests/test_font_bdf.py index 2ece5457a..b4155c879 100644 --- a/Tests/test_font_bdf.py +++ b/Tests/test_font_bdf.py @@ -20,8 +20,8 @@ def test_sanity() -> None: def test_zero_width_chars() -> None: with open(filename, "rb") as fp: data = fp.read() - data = data[:2650] + b"\x00\x00" + data[2652:] - BdfFontFile.BdfFontFile(io.BytesIO(data)) + data = data[:2650] + b"\x00\x00" + data[2652:] + BdfFontFile.BdfFontFile(io.BytesIO(data)) def test_invalid_file() -> None: