Repository navigation
BUG: Raise a clear error for a rectangle without four values - #4123
Conversation
RectangleObject validated its input with a bare assert:
assert len(arr) == 4
An AssertionError carrying no message is hard to act on, and it is the only
check on a public constructor that pypdf itself calls for every page box. Under
python -O the statement is removed altogether, so RectangleObject((0, 0)) was
accepted and the problem surfaced later as "IndexError: list index out of
range" from .width, well away from the cause.
The check now raises ValueError with the number of values it received, matching
the message _page.py already uses when a box read from a file has the wrong
length.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4123 +/- ##
=======================================
Coverage 98.10% 98.10%
=======================================
Files 59 59
Lines 11562 11566 +4
Branches 2162 2165 +3
=======================================
+ Hits 11343 11347 +4
Misses 124 124
Partials 95 95 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Both applied, thanks. The walrus reads better and _page.py already does exactly this a few lines list() was left over from a draft where I was formatting a range. For a tuple |
|
Both addressed in d1b1e36:
if (length := len(arr)) != 4:
raise ValueError(f"Expected four values for a rectangle, got {length}: {arr}") |
|
I have been mostly unavailable lately, which delayed non-critical reviews. I hope to be able to have another proper look at these changes in the next days. |
RectangleObject validated its input with a bare assert:
An AssertionError carrying no message is hard to act on, and it is the only check on a public constructor that pypdf itself calls for every page box. Under python -O the statement is removed altogether, so RectangleObject((0, 0)) was accepted and the problem surfaced later as "IndexError: list index out of range" from .width, well away from the cause.
The check now raises ValueError with the number of values it received, matching the message _page.py already uses when a box read from a file has the wrong length.