Fix UnboundLocalError in _border and document rgba() color format#9426
Fix UnboundLocalError in _border and document rgba() color format#9426veeceey wants to merge 1 commit intopython-pillow:mainfrom
Conversation
|
Hi. These seem like two unrelated changes. In the future, it's worth considering whether these should be two separate PRs, or at least two separate commits. I'm not sure how much of this you used AI for, but if you wouldn't mind explaining your human motivation a bit - did you actually find yourself triggering the |
|
Fair point on splitting, noted for next time. On the motivation -- I was looking at the ImageOps.expand code and noticed the border validation raises ValueError for negative values, but if you pass something like a 3-tuple (which is not a valid border spec), the unpacking just silently fails and Will apply the test simplification you suggested. |
…nt rgba() color format The _border helper in ImageOps raised UnboundLocalError when given a tuple with a length other than 2 or 4 (e.g. 1-tuple or 3-tuple). This changes it to raise a clear ValueError instead. Also adds documentation for the rgba() color format in ImageColor, which was supported in code and tested but missing from the docs.
61ce550 to
f708c00
Compare
|
Hi @radarhere, just checking in — I've applied your suggested test simplification and responded to your earlier questions. All CI checks are green. Would you be able to take another look when you get a chance? Thanks! |
radarhere
left a comment
There was a problem hiding this comment.
#9433 (comment) might like us to consider our policy towards AI before merging
| elif len(border) == 4: | ||
| left, top, right, bottom = border | ||
| else: | ||
| msg = "border must be an integer or a 2- or 4-tuple" |
There was a problem hiding this comment.
| msg = "border must be an integer or a 2- or 4-tuple" | |
| msg = "border must be an integer, or a tuple of two or four elements" |
Feel free to ignore this idea if you want, it just seems a bit more natural to me.
Summary
Fix bug in
ImageOps._border(): When given a tuple with a length other than 2 or 4 (e.g. a 1-tuple or 3-tuple), the function raised an unhelpfulUnboundLocalErrorbecause theleft,top,right,bottomvariables were never assigned. This now raises a clearValueErrorwith a descriptive message instead.Document
rgba()color format inImageColor: Thergba(red, green, blue, alpha)color string format has been supported in code and tested for a long time, but was missing from theImageColordocumentation. This adds it to the list of supported color string formats.Test plan
test_expand_invalid_borderthat verifiesValueErroris raised for 1-tuple, 3-tuple, and 5-tuple border valuesUnboundLocalError