gh-152157: Reject empty fraction before timezone in C fromisoformat#152161
Open
tonghuaroot wants to merge 1 commit into
Open
gh-152157: Reject empty fraction before timezone in C fromisoformat#152161tonghuaroot wants to merge 1 commit into
tonghuaroot wants to merge 1 commit into
Conversation
…rmat The C accelerator for datetime.fromisoformat() and time.fromisoformat() accepted a decimal separator (. or ,) with no following digit when a timezone designator came next, e.g. '12:34:56.+05:00', while the pure-Python implementation correctly raised ValueError. Handle the decimal-separator case before the generic end-of-substring check so an empty fraction is rejected.
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.
Fixes gh-152157
datetime.fromisoformat()andtime.fromisoformat()accepted, in the C accelerator, adecimal separator (
.or,) followed by zero fractional digits when a timezonedesignator (
+/-/Z) came next, e.g.'2020-01-01T12:34:56.+05:00'or'12:34:56.Z'.The pure-Python implementation already raised
ValueErrorfor these. ISO 8601 §4.2.2.4 andRFC 3339 §5.6 require at least one digit after the decimal sign, so the C side was the
lenient/incorrect one.
Divergence (before this PR)
'2020-01-01T12:34:56.+05:00'ValueError'2020-01-01T12:34:56.Z'ValueError'2020-01-01T12:34:56,+05:00'ValueError'12:34:56.+05:00'ValueError'12:34:56,+05:00'ValueError'12:34:56.Z'ValueErrorAfter this PR both implementations raise
ValueErrorfor all of the above, and allpreviously-accepted valid strings (
.5,.123456, comma fractions, no-fraction +offset,Z) parse identically on both.Fix
In
parse_hh_mm_ss_ff()the decimal-separator case is now handled before the genericend-of-substring check, so a separator with no following digit is rejected instead of being
treated as trailing content. The standalone trailing separator (
'12:34:56.') remainsrejected. No change to the valid-fraction or no-fraction paths.
Note for reviewers
The spec requires ≥1 digit after the decimal sign, and the C implementation already rejects
a standalone trailing separator, so this PR fixes C to reject. The alternative direction
(making pure-Python lenient) is possible but contradicts both the spec and C's own
end-of-string behavior; happy to flip the approach if you'd rather standardize on lenient.
Tests
Added the four empty-fraction-before-tz inputs (offset, negative offset,
Z, comma) to thedatetimeandtimefromisoformatfailure tests, which run against both the C andpure-Python implementations. Positive parity (valid fractions, comma fractions, and
no-fraction-with-offset) is already covered by the existing
*_examplestests, which alsorun on both implementations.
./python.exe -m test test_datetimepasses (both impls), with no refleaks for the touchedtests.