Skip to content

gh-153406: Raise ValueError, not OverflowError, for out-of-range dates in email.utils.parsedate_to_datetime#153407

Merged
bitdancer merged 1 commit into
python:mainfrom
tonghuaroot:email-date-overflow
Jul 9, 2026
Merged

gh-153406: Raise ValueError, not OverflowError, for out-of-range dates in email.utils.parsedate_to_datetime#153407
bitdancer merged 1 commit into
python:mainfrom
tonghuaroot:email-date-overflow

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

email.utils.parsedate_to_datetime builds a datetime (and a timezone offset) from the parsed date tuple without catching OverflowError, so an out-of-range year or timezone offset escapes as OverflowError instead of the documented ValueError.

email.headerregistry.DateHeader.parse only catches ValueError to record an InvalidDateDefect, so the same input made message['Date'].datetime raise instead of recording a defect. Normalizing the exception at the source in parsedate_to_datetime fixes both: the direct call now raises ValueError, and the header path records InvalidDateDefect. Valid dates are unchanged.

…e dates in email.utils.parsedate_to_datetime

email.utils.parsedate_to_datetime documented that it raises ValueError for an invalid date, but it leaked OverflowError when the parsed year or timezone offset was too large for the datetime and timedelta constructors, and that OverflowError also escaped the modern header parsing path since DateHeader.parse only caught ValueError. Wrap the datetime and timezone construction so an OverflowError is re-raised as a ValueError with the original chained as the cause, which restores the documented contract and lets the existing header handler record an InvalidDateDefect instead of raising.

@bitdancer bitdancer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Thanks for the patch.

@bitdancer bitdancer merged commit 37a26b9 into python:main Jul 9, 2026
60 checks passed
@bitdancer bitdancer added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jul 9, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @tonghuaroot for the PR, and @bitdancer for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Thanks @tonghuaroot for the PR, and @bitdancer for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Thanks @tonghuaroot for the PR, and @bitdancer for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Jul 9, 2026

Copy link
Copy Markdown

GH-153433 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Jul 9, 2026
@bedevere-app

bedevere-app Bot commented Jul 9, 2026

Copy link
Copy Markdown

GH-153434 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jul 9, 2026
@bedevere-app

bedevere-app Bot commented Jul 9, 2026

Copy link
Copy Markdown

GH-153435 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jul 9, 2026
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