Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
pganssle merged 13 commits intopython:mainfromdonBarbos:issue-126883
May 19, 2025
Merged
Show file tree
Hide file tree
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
donBarbosNov 25, 2024
5e2ddf8
Add use cases in Lib/test/datetimetester.py
donBarbosNov 25, 2024
72d4701
Add self to acknowledgements and news blurb
donBarbosNov 25, 2024
763ec67
Update for correct definition of exception
donBarbosNov 25, 2024
cc0d774
Update Lib/_pydatetime.py
donBarbosFeb 5, 2025
9adfc6e
Revert style changes
donBarbosFeb 5, 2025
eac790c
Merge branch 'main' into issue-126883
donBarbosFeb 5, 2025
d908d9d
Merge branch 'main' into issue-126883
donBarbosFeb 5, 2025
0b740a4
Merge branch 'main' into issue-126883
donBarbosFeb 12, 2025
5d8cde6
Merge branch 'main' into issue-126883
donBarbosFeb 21, 2025
8079a72
Make diff a little bit less
donBarbosFeb 24, 2025
b0a1707
Merge branch 'main' into issue-126883
donBarbosApr 10, 2025
5580d17
Apply suggestions from code review
donBarbosMay 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletionsLib/_pydatetime.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand DownExpand Up@@ -500,14 +501,22 @@ def _parse_isoformat_time(tstr):
else:
tzsign = -1 if tstr[tz_pos - 1] == '-' else 1

td = timedelta(hours=tz_comps[0], minutes=tz_comps[1],
seconds=tz_comps[2], microseconds=tz_comps[3])

tzi = timezone(tzsign * td)
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],
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
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):
Expand DownExpand Up@@ -1629,9 +1638,21 @@ def fromisoformat(cls, time_string):
time_string = time_string.removeprefix('T')

try:
return cls(*_parse_isoformat_time(time_string)[0])
except Exception:
raise ValueError(f'Invalid isoformat string: {time_string!r}')
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
Expand DownExpand Up@@ -1943,11 +1964,16 @@ def fromisoformat(cls, date_string):

if tstr:
try:
time_components, became_next_day, error_from_components = _parse_isoformat_time(tstr)
(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")

Expand Down
9 changes: 9 additions & 0 deletionsLib/test/datetimetester.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
'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:
Expand DownExpand Up@@ -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:
Expand Down
1 change: 1 addition & 0 deletionsMisc/ACKS
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1287,6 +1287,7 @@ Paul Moore
Ross Moore
Ben Morgan
Emily Morehouse
Semyon Moroz
Derek Morr
James A Morrison
Martin Morrison
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff 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.
15 changes: 15 additions & 0 deletionsModules/_datetimemodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1088,6 +1088,7 @@ parse_isoformat_time(const char *dtstr, size_t dtlen, int *hour, int *minute,
// -3: Failed to parse time component
// -4: Failed to parse time separator
// -5: Malformed timezone string
// -6: Timezone fields are not in range

const char *p = dtstr;
const char *p_end = dtstr + dtlen;
Expand DownExpand Up@@ -1134,6 +1135,11 @@ parse_isoformat_time(const char *dtstr, size_t dtlen, int *hour, int *minute,
rv = parse_hh_mm_ss_ff(tzinfo_pos, p_end, &tzhour, &tzminute, &tzsecond,
tzmicrosecond);

// Check if timezone fields are in range
if (check_time_args(tzhour, tzminute, tzsecond, *tzmicrosecond, 0) < 0) {
return -6;
}

*tzoffset = tzsign * ((tzhour * 3600) + (tzminute * 60) + tzsecond);
*tzmicrosecond *= tzsign;

Expand DownExpand Up@@ -5039,6 +5045,9 @@ time_fromisoformat(PyObject *cls, PyObject *tstr) {
&tzoffset, &tzimicrosecond);

if (rv < 0) {
if (rv == -6) {
goto error;
}
goto invalid_string_error;
}

Expand DownExpand Up@@ -5075,6 +5084,9 @@ time_fromisoformat(PyObject *cls, PyObject *tstr) {
invalid_string_error:
PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %R", tstr);
return NULL;

error:
return NULL;
}


Expand DownExpand Up@@ -5927,6 +5939,9 @@ datetime_fromisoformat(PyObject *cls, PyObject *dtstr)
len -= (p - dt_ptr);
rv = parse_isoformat_time(p, len, &hour, &minute, &second,
&microsecond, &tzoffset, &tzusec);
if (rv == -6) {
goto error;
}
}
if (rv < 0) {
goto invalid_string_error;
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp