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
7 changes: 6 additions & 1 deletion Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1862,13 +1862,18 @@ def fromisoformat(cls, date_string):
try:
separator_location = _find_isoformat_datetime_separator(date_string)
dstr = date_string[0:separator_location]
tstr = date_string[(separator_location+1):]
sep = date_string[separator_location:separator_location+1]
tstr = date_string[separator_location+1:]

date_components = _parse_isoformat_date(dstr)
except ValueError:
raise ValueError(
f'Invalid isoformat string: {date_string!r}') from None

if sep and (_is_ascii_digit(sep) or not tstr):
# Date and time most likely split at the wrong place, indicates wrong format.
raise ValueError(f'Invalid isoformat string: {date_string!r}')

if tstr:
try:
time_components = _parse_isoformat_time(tstr)
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,7 @@ def test_fromisoformat_fails(self):
'2019-W53-1', # No week 53 in 2019
'2020-W54-1', # No week 54
'2009\ud80002\ud80028', # Separators are surrogate codepoints
'2009-01-01 ', # Trailing separator without time component
]

for bad_str in bad_strs:
Expand Down Expand Up @@ -3295,6 +3296,8 @@ def test_fromisoformat_fails_datetime(self):
'2009-04-19T12:30:45.123456-05:00a', # Extra text
'2009-04-19T12:30:45.123-05:00a', # Extra text
'2009-04-19T12:30:45-05:00a', # Extra text
'2009-04-1912:30:45', # Missing date-time separator 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a valid ISO 8601 format...

By mutual agreement of the partners in information interchange, the character [T] may be omitted in
applications where there is no risk of confusing a date and time of day representation with others defined in this International Standard.

'20090419123054', # Missing date-time separator 2
]

for bad_str in bad_strs:
Expand Down
6 changes: 6 additions & 0 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5451,6 +5451,12 @@ datetime_fromisoformat(PyObject *cls, PyObject *dtstr)
if (!rv && len > separator_location) {
// In UTF-8, the length of multi-byte characters is encoded in the MSB
p += separator_location;

if (is_digit(p[0])) {
// Date and time most likely split at the wrong place, indicates wrong format.
goto invalid_string_error;
}

if ((p[0] & 0x80) == 0) {
p += 1;
}
Expand Down