Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 9 additions & 1 deletion Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ def _parse_isoformat_date(dtstr):
pos += has_sep

dayno = _read_isoformat_component(dtstr[pos:pos + 1], 1)
pos += 1

if pos != len(dtstr):
raise ValueError("Invalid isoformat string")

return list(_isoweek_to_gregorian(year, weekno, dayno))
else:
Expand All @@ -406,6 +410,10 @@ def _parse_isoformat_date(dtstr):

pos += has_sep
day = _read_isoformat_component(dtstr[pos:pos + 2], 2)
pos += 2

if pos != len(dtstr):
raise ValueError("Invalid isoformat string")

return [year, month, day]

Expand Down Expand Up @@ -1984,7 +1992,7 @@ def fromisoformat(cls, date_string):
raise ValueError(
f'Invalid isoformat string: {date_string!r}') from None

if tstr:
if separator_location < len(date_string):
try:
(time_components,
became_next_day,
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2236,6 +2236,10 @@ def test_fromisoformat_fails(self):
'2020-W 5', # space in the week number
'2020061', # 7 chars: day slice reads a 1-character tail
'2020-W2', # 1-digit week number
# gh-158500: trailing characters must not be ignored
'2020010112', # YYYYMMDD followed by two characters
'2020W011xx', # YYYYWwwD followed by two characters
'20200101\u00e9', # 9 characters, but 10 bytes in UTF-8
'٢025-03-09', # Unicode characters
'2009\ud80002\ud80028', # Separators are surrogate codepoints
]
Expand Down Expand Up @@ -3977,6 +3981,11 @@ def test_fromisoformat_fails_datetime(self):
'2009-04-19T12:30:45.-05:00', # Empty fraction before offset
'2009-04-19T12:30:45.Z', # Empty fraction before Z
'2009-04-19T12:30:45,+05:00', # Empty fraction (comma) before offset
# gh-158500: a separator must be followed by a time
'2009-04-19T', # Separator without a time
'20090419T', # Separator without a time
'2009-W16-7T', # Separator without a time
'2009W167T', # Separator without a time
]

for bad_str in bad_strs:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:meth:`datetime.date.fromisoformat` now raises :exc:`ValueError` for a
basic-format date followed by extra characters, such as ``'2020010112'``,
instead of silently ignoring them. The pure-Python implementation of
:meth:`datetime.datetime.fromisoformat` now also rejects a date followed by a
single character, such as ``'2020-01-01T'``, matching the C implementation.
7 changes: 7 additions & 0 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,10 @@ parse_isoformat_date(const char *dtstr, const size_t len, int *year, int *month,
iso_day = 1;
}

if ((size_t)(p - dtstr) != len) {
return -1;
}

int rv = iso_to_ymd(*year, iso_week, iso_day, year, month, day);
if (rv) {
return -3 + rv;
Expand All @@ -997,6 +1001,9 @@ parse_isoformat_date(const char *dtstr, const size_t len, int *year, int *month,
if (p == NULL) {
return -1;
}
if ((size_t)(p - dtstr) != len) {
return -1;
}
return 0;
}

Expand Down
Loading