Fix parse_date and parse_time to raise ParseError instead of ValueError#1282
Open
gaoflow wants to merge 1 commit into
Open
Fix parse_date and parse_time to raise ParseError instead of ValueError#1282gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
When parse_date or parse_time receives an input string whose numbers produce an out-of-range date or time (e.g. month 13, hour 25), the final datetime.date / datetime.time constructor raises a raw ValueError. Callers that catch ParseError (the documented error type for this module) never see the exception. Wrap both constructors in try/except and re-raise as ParseError with a message that includes the input string and the expected format pattern. Fixes python-babel#1178.
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.
Problem
parse_dateandparse_timeboth catchParseErroras their documented error type, but when the parsed numbers produce an out-of-range date or time value, the internaldatetime.date/datetime.timeconstructor raises a rawValueErrorthat escapes to the caller uncaught.Reported in #1178. Example:
Fix
Wrap the final
datetime.date(year, month, day)call inparse_dateanddatetime.time(hour, minute, second)call inparse_timewithtry/except ValueError, re-raising asParseErrorwith an informative message that includes the input string and the expected format pattern.All 2245 existing tests pass (2 xfailed unchanged).
Fixes #1178.