gh-153406: Raise ValueError, not OverflowError, for out-of-range dates in email.utils.parsedate_to_datetime#153407
Merged
Conversation
…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
approved these changes
Jul 9, 2026
bitdancer
left a comment
Member
There was a problem hiding this comment.
Good catch. Thanks for the patch.
|
Thanks @tonghuaroot for the PR, and @bitdancer for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13. |
|
Thanks @tonghuaroot for the PR, and @bitdancer for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14. |
|
Thanks @tonghuaroot for the PR, and @bitdancer for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15. |
|
GH-153433 is a backport of this pull request to the 3.13 branch. |
|
GH-153434 is a backport of this pull request to the 3.14 branch. |
|
GH-153435 is a backport of this pull request to the 3.15 branch. |
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.
email.utils.parsedate_to_datetimebuilds adatetime(and atimezoneoffset) from the parsed date tuple without catchingOverflowError, so an out-of-range year or timezone offset escapes asOverflowErrorinstead of the documentedValueError.email.headerregistry.DateHeader.parseonly catchesValueErrorto record anInvalidDateDefect, so the same input mademessage['Date'].datetimeraise instead of recording a defect. Normalizing the exception at the source inparsedate_to_datetimefixes both: the direct call now raisesValueError, and the header path recordsInvalidDateDefect. Valid dates are unchanged.