Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-126883: Add check that timezone fields are in range fordatetime.fromisoformat
#127242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
13 commits Select commitHold shift + click to select a range
a052a93
Add check that timezone fields are in range
donBarbos5e2ddf8
Add use cases in Lib/test/datetimetester.py
donBarbos72d4701
Add self to acknowledgements and news blurb
donBarbos763ec67
Update for correct definition of exception
donBarboscc0d774
Update Lib/_pydatetime.py
donBarbos9adfc6e
Revert style changes
donBarboseac790c
Merge branch 'main' into issue-126883
donBarbosd908d9d
Merge branch 'main' into issue-126883
donBarbos0b740a4
Merge branch 'main' into issue-126883
donBarbos5d8cde6
Merge branch 'main' into issue-126883
donBarbos8079a72
Make diff a little bit less
donBarbosb0a1707
Merge branch 'main' into issue-126883
donBarbos5580d17
Apply suggestions from code review
donBarbosFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -467,6 +467,7 @@ def _parse_isoformat_time(tstr): | ||
hour, minute, second, microsecond = time_comps | ||
became_next_day = False | ||
error_from_components = False | ||
error_from_tz = None | ||
if (hour == 24): | ||
if all(time_comp == 0 for time_comp in time_comps[1:]): | ||
hour = 0 | ||
@@ -500,14 +501,22 @@ def _parse_isoformat_time(tstr): | ||
else: | ||
tzsign = -1 if tstr[tz_pos - 1] == '-' else 1 | ||
try: | ||
# This function is intended to validate datetimes, but because | ||
# we restrict time zones to ±24h, it serves here as well. | ||
_check_time_fields(hour=tz_comps[0], minute=tz_comps[1], | ||
donBarbos marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
second=tz_comps[2], microsecond=tz_comps[3], | ||
fold=0) | ||
except ValueError as e: | ||
error_from_tz = e | ||
else: | ||
td = timedelta(hours=tz_comps[0], minutes=tz_comps[1], | ||
seconds=tz_comps[2], microseconds=tz_comps[3]) | ||
tzi = timezone(tzsign * td) | ||
time_comps.append(tzi) | ||
return time_comps, became_next_day, error_from_components, error_from_tz | ||
# tuple[int, int, int] -> tuple[int, int, int] version of date.fromisocalendar | ||
def _isoweek_to_gregorian(year, week, day): | ||
@@ -1629,9 +1638,21 @@ def fromisoformat(cls, time_string): | ||
time_string = time_string.removeprefix('T') | ||
try: | ||
time_components, _, error_from_components, error_from_tz = ( | ||
_parse_isoformat_time(time_string) | ||
) | ||
except ValueError: | ||
raise ValueError( | ||
f'Invalid isoformat string: {time_string!r}') from None | ||
else: | ||
if error_from_tz: | ||
raise error_from_tz | ||
if error_from_components: | ||
raise ValueError( | ||
"Minute, second, and microsecond must be 0 when hour is 24" | ||
) | ||
return cls(*time_components) | ||
def strftime(self, format): | ||
"""Format using strftime(). The date part of the timestamp passed | ||
@@ -1943,11 +1964,16 @@ def fromisoformat(cls, date_string): | ||
if tstr: | ||
try: | ||
(time_components, | ||
became_next_day, | ||
error_from_components, | ||
error_from_tz) = _parse_isoformat_time(tstr) | ||
except ValueError: | ||
raise ValueError( | ||
f'Invalid isoformat string: {date_string!r}') from None | ||
else: | ||
if error_from_tz: | ||
raise error_from_tz | ||
if error_from_components: | ||
raise ValueError("minute, second, and microsecond must be 0 when hour is 24") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3567,6 +3567,10 @@ def test_fromisoformat_fails_datetime(self): | ||
'2009-04-19T12:30:45.400 +02:30', # Space between ms and timezone (gh-130959) | ||
'2009-04-19T12:30:45.400 ', # Trailing space (gh-130959) | ||
'2009-04-19T12:30:45. 400', # Space before fraction (gh-130959) | ||
'2009-04-19T12:30:45+00:90:00', # Time zone field out from range | ||
'2009-04-19T12:30:45+00:00:90', # Time zone field out from range | ||
donBarbos marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
'2009-04-19T12:30:45-00:90:00', # Time zone field out from range | ||
'2009-04-19T12:30:45-00:00:90', # Time zone field out from range | ||
] | ||
for bad_str in bad_strs: | ||
@@ -4791,6 +4795,11 @@ def test_fromisoformat_fails(self): | ||
'12:30:45.400 +02:30', # Space between ms and timezone (gh-130959) | ||
'12:30:45.400 ', # Trailing space (gh-130959) | ||
'12:30:45. 400', # Space before fraction (gh-130959) | ||
'24:00:00.000001', # Has non-zero microseconds on 24:00 | ||
'24:00:01.000000', # Has non-zero seconds on 24:00 | ||
'24:01:00.000000', # Has non-zero minutes on 24:00 | ||
'12:30:45+00:90:00', # Time zone field out from range | ||
'12:30:45+00:00:90', # Time zone field out from range | ||
] | ||
for bad_str in bad_strs: | ||
1 change: 1 addition & 0 deletionsMisc/ACKS
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1287,6 +1287,7 @@ Paul Moore | ||
Ross Moore | ||
Ben Morgan | ||
Emily Morehouse | ||
Semyon Moroz | ||
Derek Morr | ||
James A Morrison | ||
Martin Morrison | ||
3 changes: 3 additions & 0 deletionsMisc/NEWS.d/next/Library/2024-11-25-10-22-08.gh-issue-126883.MAEF7g.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Add check that timezone fields are in range for | ||
:meth:`datetime.datetime.fromisoformat` and | ||
:meth:`datetime.time.fromisoformat`. Patch by Semyon Moroz. |
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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.