Raise ValueError for zero or negative sizes in ImageOps contain/cover/pad/fit - #9856
Draft
hdimer wants to merge 1 commit into
Draft
Raise ValueError for zero or negative sizes in ImageOps contain/cover/pad/fit#9856hdimer wants to merge 1 commit into
hdimer wants to merge 1 commit into
Conversation
…/pad/fit These functions compute size[0] / size[1] before Image.resize can validate the size, so a zero height raised ZeroDivisionError. cover also skipped validation for a zero or negative width, and contain returned a wrong-sized image for a negative height. Validate the requested size up front and raise the core's own "height and width must be > 0" ValueError, so all four behave consistently.
Member
|
This topic has had some recent activity in other PRs. #9672 suggested resolving the I may investigate expanding #9673 to address these other operations. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
ImageOps.contain,cover,padandfithandle invalid output sizes inconsistently. Because they computesize[0] / size[1]beforeImage.resizevalidates the size, a zero height raisesZeroDivisionError;coverskips validation entirely for a zero or negative width (returning a wrong-sized image); andcontainreturns a wrong-sized image for a negative height.Before, on a 100x50 image:
(0, 10)ValueError(20, 10)ValueErrorValueError(10, 0)ZeroDivisionErrorZeroDivisionErrorZeroDivisionErrorZeroDivisionError(-1, 10)ValueError(20, 10)ValueErrorValueError(10, -1)(10, 5)ValueErrorValueErrorValueErrorThis adds an up-front size check to
contain,coverandfit(paddelegates tocontain) that raises the core's ownValueError: height and width must be > 0for any dimension<= 0, so all four behave consistently.Testing
Added a parametrized test over the four functions and the zero/negative sizes above; it asserts a
ValueErrorrather than a crash or a silently wrong result. The fullTests/test_imageops.pysuite passes (62 tests) andruffis clean. A release note is included.Used AI assistance on this; I reviewed and tested the change myself.