Skip to content

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

Open
tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:email-date-overflow
Open

gh-153406: Raise ValueError, not OverflowError, for out-of-range dates in email.utils.parsedate_to_datetime#153407
tonghuaroot wants to merge 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant