Skip to content

Commit

Permalink
Merge pull request #7266 from mtreinish/handle-repr-exceptions
Browse files Browse the repository at this point in the history
Handle exceptions in _repr_jpeg_ and _repr_png_
  • Loading branch information
radarhere authored Jul 31, 2023
2 parents 07623d1 + 2a55b14 commit 07038d7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
5 changes: 2 additions & 3 deletions Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,11 +929,10 @@ def test_repr_jpeg(self):
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")
Expand Down
5 changes: 2 additions & 3 deletions Tests/test_file_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,10 @@ def test_repr_png(self):
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:
Expand Down
5 changes: 2 additions & 3 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,8 @@ def _repr_image(self, image_format, **kwargs):
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):
Expand Down

0 comments on commit 07038d7

Please sign in to comment.