Skip to content

BUG: Raise a clear error for a rectangle without four values - #4123

Merged
stefan6419846 merged 2 commits into
py-pdf:mainfrom
RavSinghChandan:rectangle-four-values
Sep 30, 2026
Merged

stefan6419846 merged 2 commits into
py-pdf:mainfrom
RavSinghChandan:rectangle-four-values

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

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.

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

codecov Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.10%. Comparing base (ebae1a5) to head (d1b1e36).
⚠️ Report is 2 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread pypdf/generic/_rectangle.py Resolved
@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

Both applied, thanks.

The walrus reads better and _page.py already does exactly this a few lines
away, which I should have spotted when I matched the message wording.

list() was left over from a draft where I was formatting a range. For a tuple
or list it changes nothing, and _page.py formats the value directly, so it is
gone.

@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

Both addressed in d1b1e36:

  • The length is now bound with a walrus operator, so it is only
    calculated once.
  • list(arr) is gone — the sequence is formatted directly.
if (length := len(arr)) != 4:
    raise ValueError(f"Expected four values for a rectangle, got {length}: {arr}")

@stefan6419846

Copy link
Copy Markdown
Collaborator

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.

@stefan6419846
stefan6419846 merged commit 59c1d74 into py-pdf:main Sep 30, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants