gh-151770: Fix datetime.fromisoformat() assertion on an out-of-range month with a 24:00 time#151771
Open
tonghuaroot wants to merge 3 commits into
Open
gh-151770: Fix datetime.fromisoformat() assertion on an out-of-range month with a 24:00 time#151771tonghuaroot wants to merge 3 commits into
tonghuaroot wants to merge 3 commits into
Conversation
…range month with a 24:00 time The 24:00 midnight-rollover path validated only the upper month bound before calling days_in_month(), so a month below 1 reached assert(month >= 1) on a debug build (and AssertionError in the pure Python implementation). Add the missing lower bound to both so an out-of-range month consistently raises ValueError.
StanFromIreland
left a comment
Member
There was a problem hiding this comment.
Just a few little nits, the core change is correct.
While we're dealing with this assert, can you please update it (on L58) like so:
- assert 1 <= month <= 12, month
+ assert 1 <= month <= 12, f"month must be in 1..12, not {month}"
Comment on lines
+1
to
+3
| Fix :meth:`datetime.datetime.fromisoformat` raising an unexpected error on an | ||
| out-of-range month combined with a ``24:00`` time, such as | ||
| ``"2009-00-01T24:00:00"``. It now consistently raises :exc:`ValueError`. |
Member
There was a problem hiding this comment.
Suggested change
| Fix :meth:`datetime.datetime.fromisoformat` raising an unexpected error on an | |
| out-of-range month combined with a ``24:00`` time, such as | |
| ``"2009-00-01T24:00:00"``. It now consistently raises :exc:`ValueError`. | |
| Fix :meth:`datetime.datetime.fromisoformat` raising :exc:`AssertionError` | |
| instead of :exc:`ValueError` for an out-of-range month combined with a | |
| ``24:00`` time. |
| "2009-04-01T12:30:90", # Second out of range | ||
| "2009-04-01T12:90:45", # Minute out of range | ||
| "2009-04-01T25:30:45", # Hour out of range | ||
| "2009-00-01T24:00:00", # Month out of range (below) |
Member
There was a problem hiding this comment.
Suggested change
| "2009-00-01T24:00:00", # Month out of range (below) | |
| "2009-00-01T24:00:00", # Month below range |
Contributor
Author
|
Thanks @StanFromIreland — applied all three. |
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.
datetime.fromisoformat()reacheddays_in_month()withmonth == 0when the input combined an out-of-range (below 1) month with a24:00time, for example"2009-00-01T24:00:00". On a--with-pydebugbuild this trippedassert(month >= 1)indays_in_month(Modules/_datetimemodule.c); the pure-Python implementation inLib/_pydatetime.pyraisedAssertionErroron the same input. On a release build both already raisedValueError.The
24:00midnight-rollover guard validated only the upper month bound (month <= 12) before thedays_in_month()call, and the parser does not range-check the month before that point, so a month below 1 slipped through. The matching upper-bound input"2009-13-01T24:00:00"was already rejected by themonth <= 12check.This adds the missing
month >= 1lower bound to the guard in both implementations (mirroring the existingcheck_date_argsvalidation), so an out-of-range month consistently raisesValueError. A regression test is added next to the existing out-of-range-month case indatetimetester.py.