Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Image.WARN_POSSIBLE_FORMATS #8063

Merged
merged 5 commits into from
Jun 28, 2024
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
9 changes: 9 additions & 0 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@
assert im.mode == "RGB"
assert im.size == (128, 128)

def test_open_verbose_failure(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(Image, "WARN_POSSIBLE_FORMATS", True)

im = io.BytesIO(b"")
with pytest.warns(UserWarning):
with pytest.raises(UnidentifiedImageError):
with Image.open(im):
pass

Check warning on line 126 in Tests/test_image.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_image.py#L126

Added line #L126 was not covered by tests

def test_width_height(self) -> None:
im = Image.new("RGB", (1, 2))
assert im.width == 1
Expand Down
5 changes: 5 additions & 0 deletions docs/reference/Image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ Constants
Set to 89,478,485, approximately 0.25GB for a 24-bit (3 bpp) image.
See :py:meth:`~PIL.Image.open` for more information about how this is used.

.. data:: WARN_POSSIBLE_FORMATS

Set to false. If true, when an image cannot be identified, warnings will be raised
from formats that attempted to read the data.

Transpose methods
^^^^^^^^^^^^^^^^^

Expand Down
16 changes: 8 additions & 8 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
pass


WARN_POSSIBLE_FORMATS: bool = False

Check warning on line 79 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L79

Added line #L79 was not covered by tests

# Limit to around a quarter gigabyte for a 24-bit (3 bpp) image
MAX_IMAGE_PIXELS: int | None = int(1024 * 1024 * 1024 // 4 // 3)

Expand Down Expand Up @@ -3344,7 +3346,7 @@

preinit()

accept_warnings: list[str] = []
warning_messages: list[str] = []

Check warning on line 3349 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L3349

Added line #L3349 was not covered by tests

def _open_core(
fp: IO[bytes],
Expand All @@ -3360,17 +3362,15 @@
factory, accept = OPEN[i]
result = not accept or accept(prefix)
if isinstance(result, str):
accept_warnings.append(result)
warning_messages.append(result)

Check warning on line 3365 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L3365

Added line #L3365 was not covered by tests
elif result:
fp.seek(0)
im = factory(fp, filename)
_decompression_bomb_check(im.size)
return im
except (SyntaxError, IndexError, TypeError, struct.error):
# Leave disabled by default, spams the logs with image
# opening failures that are entirely expected.
# logger.debug("", exc_info=True)
continue
except (SyntaxError, IndexError, TypeError, struct.error) as e:
if WARN_POSSIBLE_FORMATS:
warning_messages.append(i + " opening failed. " + str(e))

Check warning on line 3373 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L3371-L3373

Added lines #L3371 - L3373 were not covered by tests
except BaseException:
if exclusive_fp:
fp.close()
Expand All @@ -3395,7 +3395,7 @@

if exclusive_fp:
fp.close()
for message in accept_warnings:
for message in warning_messages:

Check warning on line 3398 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L3398

Added line #L3398 was not covered by tests
warnings.warn(message)
msg = "cannot identify image file %r" % (filename if filename else fp)
raise UnidentifiedImageError(msg)
Expand Down
Loading