-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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 argument to raise warnings from factory errors on UnidentifiedImageError #8033
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3251,6 +3251,7 @@ def open( | |||||||||||||||
fp: StrOrBytesPath | IO[bytes], | ||||||||||||||||
mode: Literal["r"] = "r", | ||||||||||||||||
formats: list[str] | tuple[str, ...] | None = None, | ||||||||||||||||
warn_possible_formats: bool = False, | ||||||||||||||||
) -> ImageFile.ImageFile: | ||||||||||||||||
""" | ||||||||||||||||
Opens and identifies the given image file. | ||||||||||||||||
|
@@ -3272,6 +3273,8 @@ def open( | |||||||||||||||
Pass ``None`` to try all supported formats. You can print the set of | ||||||||||||||||
available formats by running ``python3 -m PIL`` or using | ||||||||||||||||
the :py:func:`PIL.features.pilinfo` function. | ||||||||||||||||
:param warn_possible_formats: If an image cannot be identified, raise warnings | ||||||||||||||||
from formats that attempted to read the data. | ||||||||||||||||
:returns: An :py:class:`~PIL.Image.Image` object. | ||||||||||||||||
:exception FileNotFoundError: If the file cannot be found. | ||||||||||||||||
:exception PIL.UnidentifiedImageError: If the image cannot be opened and | ||||||||||||||||
|
@@ -3318,7 +3321,7 @@ def open( | |||||||||||||||
|
||||||||||||||||
preinit() | ||||||||||||||||
|
||||||||||||||||
accept_warnings: list[str] = [] | ||||||||||||||||
warning_messages: list[str] = [] | ||||||||||||||||
|
||||||||||||||||
def _open_core( | ||||||||||||||||
fp: IO[bytes], | ||||||||||||||||
|
@@ -3334,16 +3337,18 @@ def _open_core( | |||||||||||||||
factory, accept = OPEN[i] | ||||||||||||||||
result = not accept or accept(prefix) | ||||||||||||||||
if isinstance(result, str): | ||||||||||||||||
accept_warnings.append(result) | ||||||||||||||||
warning_messages.append(result) | ||||||||||||||||
elif result: | ||||||||||||||||
fp.seek(0) | ||||||||||||||||
im = factory(fp, filename) | ||||||||||||||||
_decompression_bomb_check(im.size) | ||||||||||||||||
return im | ||||||||||||||||
except (SyntaxError, IndexError, TypeError, struct.error): | ||||||||||||||||
except (SyntaxError, IndexError, TypeError, struct.error) as e: | ||||||||||||||||
# Leave disabled by default, spams the logs with image | ||||||||||||||||
# opening failures that are entirely expected. | ||||||||||||||||
# logger.debug("", exc_info=True) | ||||||||||||||||
if warn_possible_formats: | ||||||||||||||||
warning_messages.append(i + " opening failed. " + str(e)) | ||||||||||||||||
Comment on lines
3347
to
+3351
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The comment is probably no longer relevant with this change. |
||||||||||||||||
continue | ||||||||||||||||
except BaseException: | ||||||||||||||||
if exclusive_fp: | ||||||||||||||||
|
@@ -3369,7 +3374,7 @@ def _open_core( | |||||||||||||||
|
||||||||||||||||
if exclusive_fp: | ||||||||||||||||
fp.close() | ||||||||||||||||
for message in accept_warnings: | ||||||||||||||||
for message in warning_messages: | ||||||||||||||||
warnings.warn(message) | ||||||||||||||||
msg = "cannot identify image file %r" % (filename if filename else fp) | ||||||||||||||||
raise UnidentifiedImageError(msg) | ||||||||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these can all be in the same
with
statement, separated by commas. Though that might make the line too long.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I do that, and then run black, I get
This does not look clearer to me.