Skip to content
Closed
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
6 changes: 6 additions & 0 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ 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 @@ -399,6 +402,9 @@ 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
5 changes: 5 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2114,9 +2114,14 @@ 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
'2020010112', # Trailing characters after a basic-format date
'2020W011xx', # Trailing characters after a basic-format week date
'٢025-03-09', # Unicode characters
'2009\ud80002\ud80028', # Separators are surrogate codepoints
]
if not issubclass(self.theclass, datetime):
# 9 characters, 10 UTF-8 bytes; exercise the C parser's 10-byte path.
bad_strs.append('20200101\u00e9')

for bad_str in bad_strs:
with self.assertRaises(ValueError):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :meth:`datetime.date.fromisoformat` accepting basic-format date strings
with trailing characters.
7 changes: 7 additions & 0 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,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 @@ -1011,6 +1015,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