Merge pull request #7266 from mtreinish/handle-repr-exceptions

Handle exceptions in _repr_jpeg_ and _repr_png_
This commit is contained in:
Andrew Murray 2023-07-31 11:42:44 +10:00 committed by GitHub
commit 07038d7e89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 9 deletions

View File

@ -929,11 +929,10 @@ class TestFileJpeg:
assert repr_jpeg.format == "JPEG"
assert_image_similar(im, repr_jpeg, 17)
def test_repr_jpeg_error(self):
def test_repr_jpeg_error_returns_none(self):
im = hopper("F")
with pytest.raises(ValueError):
im._repr_jpeg_()
assert im._repr_jpeg_() is None
@pytest.mark.skipif(not is_win32(), reason="Windows only")

View File

@ -532,11 +532,10 @@ class TestFilePng:
assert repr_png.format == "PNG"
assert_image_equal(im, repr_png)
def test_repr_png_error(self):
def test_repr_png_error_returns_none(self):
im = hopper("F")
with pytest.raises(ValueError):
im._repr_png_()
assert im._repr_png_() is None
def test_chunk_order(self, tmp_path):
with Image.open("Tests/images/icc_profile.png") as im:

View File

@ -641,9 +641,8 @@ class Image:
b = io.BytesIO()
try:
self.save(b, image_format, **kwargs)
except Exception as e:
msg = f"Could not save to {image_format} for display"
raise ValueError(msg) from e
except Exception:
return None
return b.getvalue()
def _repr_png_(self):