gh-152204: Validate date fields in pure-Python date.fromisoformat#152205
Open
tonghuaroot wants to merge 2 commits into
Open
gh-152204: Validate date fields in pure-Python date.fromisoformat#152205tonghuaroot wants to merge 2 commits into
tonghuaroot wants to merge 2 commits into
Conversation
The pure-Python _parse_isoformat_date read each fixed-width field with int() on a slice, which silently accepts a leading sign or whitespace, or a short slice that runs off the end of the string. Malformed basic-format inputs such as '2020+12' or '2020061' were therefore parsed into a wrong-but-plausible date instead of raising, while the C accelerator rejects them via parse_digits(). Validate that each field slice is exactly N ASCII digits before converting.
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.
_pydatetime._parse_isoformat_datereads each fixed-width field withint()on a slice, without checking that the slice is exactly N ASCII digits.int()accepts a leading+/-/whitespace and a short string, so several malformed ISO 8601 basic-format dates are silently parsed into a wrong-but-plausibledateinstead of raisingValueError:The C accelerator rejects all of these via
parse_digits()(which requires the exact field width and digit-only content), so this is a C-vs-pure-Python divergence. The pure-Python path is used when the_datetimeC extension is unavailable, and directly via_pydatetime.This validates each field slice (
year/month/day/weekno/weekday) to be exactly N ASCII digits before converting, mirroring the Cparse_digits(), and extendsdatetimetester'stest_fromisoformat_failswith the affected inputs (the new cases now reject on both implementations).Fixes #152204.
Prepared with AI assistance (Claude Code) and verified by hand against a debug build, against both the C and pure-Python implementations.
date.fromisoformatsilently mis-parses malformed basic-format dates #152204