Skip to content
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
6 changes: 2 additions & 4 deletions Tests/test_file_libtiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,11 +1140,9 @@ def test_sampleformat_not_corrupted(self) -> None:
def test_realloc_overflow(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(TiffImagePlugin, "READ_LIBTIFF", True)
with Image.open("Tests/images/tiff_overflow_rows_per_strip.tif") as im:
with pytest.raises(OSError) as e:
im.load()

# Assert that the error code is IMAGING_CODEC_MEMORY
assert str(e.value) == "decoder error -9"
with pytest.raises(OSError, match="decoder error -9"):
im.load()

@pytest.mark.parametrize("compression", ("tiff_adobe_deflate", "jpeg"))
def test_save_multistrip(self, compression: str, tmp_path: Path) -> None:
Expand Down
14 changes: 5 additions & 9 deletions Tests/test_file_ppm.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,25 +293,21 @@ def test_header_token_too_long(tmp_path: Path) -> None:
with open(path, "wb") as f:
f.write(b"P6\n 01234567890")

with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Token too long in file header: 01234567890"):
with Image.open(path):
pass

assert str(e.value) == "Token too long in file header: 01234567890"


def test_truncated_file(tmp_path: Path) -> None:
# Test EOF in header
path = str(tmp_path / "temp.pgm")
with open(path, "wb") as f:
f.write(b"P6")

with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Reached EOF while reading header"):
with Image.open(path):
pass

assert str(e.value) == "Reached EOF while reading header"

# Test EOF for PyDecoder
fp = BytesIO(b"P5 3 1 4")
with Image.open(fp) as im:
Expand All @@ -335,12 +331,12 @@ def test_invalid_maxval(maxval: bytes, tmp_path: Path) -> None:
with open(path, "wb") as f:
f.write(b"P6\n3 1 " + maxval)

with pytest.raises(ValueError) as e:
with pytest.raises(
ValueError, match="maxval must be greater than 0 and less than 65536"
):
with Image.open(path):
pass

assert str(e.value) == "maxval must be greater than 0 and less than 65536"


def test_neg_ppm() -> None:
# Storage.c accepted negative values for xsize, ysize. the
Expand Down
3 changes: 1 addition & 2 deletions Tests/test_file_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ def test_seek_too_large(self) -> None:

def test_set_legacy_api(self) -> None:
ifd = TiffImagePlugin.ImageFileDirectory_v2()
with pytest.raises(Exception) as e:
with pytest.raises(Exception, match="Not allowing setting of legacy api"):
ifd.legacy_api = False
assert str(e.value) == "Not allowing setting of legacy api"

def test_xyres_tiff(self) -> None:
filename = "Tests/images/pil168.tif"
Expand Down
3 changes: 1 addition & 2 deletions Tests/test_file_webp.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@
@pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system")
def test_write_encoding_error_message(self, tmp_path: Path) -> None:
im = Image.new("RGB", (15000, 15000))
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="encoding error 6"):

Check warning on line 157 in Tests/test_file_webp.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_file_webp.py#L157

Added line #L157 was not covered by tests
im.save(tmp_path / "temp.webp", method=0)
assert str(e.value) == "encoding error 6"

@pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system")
def test_write_encoding_error_bad_dimension(self, tmp_path: Path) -> None:
Expand Down
3 changes: 1 addition & 2 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ def test_image_modes_success(self, mode: str) -> None:

@pytest.mark.parametrize("mode", ("", "bad", "very very long"))
def test_image_modes_fail(self, mode: str) -> None:
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="unrecognized image mode"):
Image.new(mode, (1, 1))
assert str(e.value) == "unrecognized image mode"

def test_exception_inheritance(self) -> None:
assert issubclass(UnidentifiedImageError, OSError)
Expand Down
7 changes: 3 additions & 4 deletions Tests/test_imagedraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ def test_compute_regular_polygon_vertices(
0,
ValueError,
"bounding_circle should contain 2D coordinates "
"and a radius (e.g. (x, y, r) or ((x, y), r) )",
r"and a radius \(e.g. \(x, y, r\) or \(\(x, y\), r\) \)",
),
(
3,
Expand All @@ -1640,7 +1640,7 @@ def test_compute_regular_polygon_vertices(
((50, 50, 50), 25),
0,
ValueError,
"bounding_circle centre should contain 2D coordinates (e.g. (x, y))",
r"bounding_circle centre should contain 2D coordinates \(e.g. \(x, y\)\)",
),
(
3,
Expand All @@ -1665,9 +1665,8 @@ def test_compute_regular_polygon_vertices_input_error_handling(
expected_error: type[Exception],
error_message: str,
) -> None:
with pytest.raises(expected_error) as e:
with pytest.raises(expected_error, match=error_message):
ImageDraw._compute_regular_polygon_vertices(bounding_circle, n_sides, rotation) # type: ignore[arg-type]
assert str(e.value) == error_message


def test_continuous_horizontal_edges_polygon() -> None:
Expand Down
3 changes: 1 addition & 2 deletions Tests/test_imagefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,8 @@ def test_truncated(self) -> None:
b"0" * ImageFile.SAFEBLOCK
) # only SAFEBLOCK bytes, so that the header is truncated
)
with pytest.raises(OSError) as e:
with pytest.raises(OSError, match="Truncated File Read"):
BmpImagePlugin.BmpImageFile(b)
assert str(e.value) == "Truncated File Read"

@skip_unless_feature("zlib")
def test_truncated_with_errors(self) -> None:
Expand Down
26 changes: 10 additions & 16 deletions Tests/test_imagemorph.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,12 @@ def test_lut(op: str) -> None:
def test_no_operator_loaded() -> None:
im = Image.new("L", (1, 1))
mop = ImageMorph.MorphOp()
with pytest.raises(Exception) as e:
with pytest.raises(Exception, match="No operator loaded"):
mop.apply(im)
assert str(e.value) == "No operator loaded"
with pytest.raises(Exception) as e:
with pytest.raises(Exception, match="No operator loaded"):
mop.match(im)
assert str(e.value) == "No operator loaded"
with pytest.raises(Exception) as e:
with pytest.raises(Exception, match="No operator loaded"):
mop.save_lut("")
assert str(e.value) == "No operator loaded"


# Test the named patterns
Expand Down Expand Up @@ -238,15 +235,12 @@ def test_incorrect_mode() -> None:
im = hopper("RGB")
mop = ImageMorph.MorphOp(op_name="erosion8")

with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Image mode must be L"):
mop.apply(im)
assert str(e.value) == "Image mode must be L"
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Image mode must be L"):
mop.match(im)
assert str(e.value) == "Image mode must be L"
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Image mode must be L"):
mop.get_on_pixels(im)
assert str(e.value) == "Image mode must be L"


def test_add_patterns() -> None:
Expand Down Expand Up @@ -279,9 +273,10 @@ def test_pattern_syntax_error() -> None:
lb.add_patterns(new_patterns)

# Act / Assert
with pytest.raises(Exception) as e:
with pytest.raises(
Exception, match='Syntax error in pattern "a pattern with a syntax error"'
):
lb.build_lut()
assert str(e.value) == 'Syntax error in pattern "a pattern with a syntax error"'


def test_load_invalid_mrl() -> None:
Expand All @@ -290,9 +285,8 @@ def test_load_invalid_mrl() -> None:
mop = ImageMorph.MorphOp()

# Act / Assert
with pytest.raises(Exception) as e:
with pytest.raises(Exception, match="Wrong size operator file!"):
mop.load_lut(invalid_mrl)
assert str(e.value) == "Wrong size operator file!"


def test_roundtrip_mrl(tmp_path: Path) -> None:
Expand Down
12 changes: 2 additions & 10 deletions Tests/test_imagepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,9 @@ def test_path_constructors(
def test_invalid_path_constructors(
coords: tuple[str, str] | Sequence[Sequence[int]],
) -> None:
# Act
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="incorrect coordinate type"):
ImagePath.Path(coords)

# Assert
assert str(e.value) == "incorrect coordinate type"


@pytest.mark.parametrize(
"coords",
Expand All @@ -99,13 +95,9 @@ def test_invalid_path_constructors(
),
)
def test_path_odd_number_of_coordinates(coords: Sequence[int]) -> None:
# Act
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="wrong number of coordinates"):
ImagePath.Path(coords)

# Assert
assert str(e.value) == "wrong number of coordinates"


@pytest.mark.parametrize(
"coords, expected",
Expand Down
Loading