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
8 changes: 8 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -3758,6 +3758,10 @@ def test_fromisoformat_fails_datetime(self):
'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
'2020-2020', # Ambiguous 9-char date portion
'2009-04-19T12:30:45.+05:00', # Empty fraction before offset
'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
]

for bad_str in bad_strs:
Expand Down Expand Up @@ -5003,6 +5007,10 @@ def test_fromisoformat_fails(self):
'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
'12:30:45.+05:00', # Empty fraction before offset
'12:30:45.-05:00', # Empty fraction before offset
'12:30:45.Z', # Empty fraction before Z
'12:30:45,+05:00', # Empty fraction (comma) before offset
]

for bad_str in bad_strs:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:meth:`~datetime.datetime.fromisoformat` and :meth:`~datetime.time.fromisoformat`
now reject a decimal separator (``.`` or ``,``) that is not followed by any
fractional digit before a timezone designator, such as ``'12:34:56.+05:00'``.
The C implementation previously accepted such strings while the pure-Python
implementation correctly raised :exc:`ValueError`.
17 changes: 10 additions & 7 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1034,20 +1034,23 @@ parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end, int *hour,
has_separator = (c == ':');
}

if (p >= p_end) {
if (c == '.' || c == ',') {
if (i < 2) {
return -3; // Decimal mark on hour or minute
}
if (p >= p_end) {
return -3; // Decimal mark not followed by any digit
}
break;
}
else if (p >= p_end) {
return c != '\0';
}
else if (has_separator && (c == ':')) {
if (i == 2) {
return -4; // Malformed microsecond separator
}
continue;
}
else if (c == '.' || c == ',') {
if (i < 2) {
return -3; // Decimal mark on hour or minute
}
break;
} else if (!has_separator) {
--p;
} else {
Expand Down
Loading