Skip to content

Commit 14e3c70

Browse files
gh-115225: Raise error on unsupported ISO 8601 time strings (#119339)
Some time strings that contain fractional hours or minutes are permitted by ISO 8601, but such strings are very unlikely to be intentional. The current parser does not parse such strings correctly or raise an error. This change raises a ValueError when hours or minutes contain a decimal mark. Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 10eac02 commit 14e3c70

File tree

4 files changed

+7
-0
lines changed

4 files changed

+7
-0
lines changed

Lib/test/datetimetester.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4412,6 +4412,8 @@ def test_fromisoformat_fails(self):
44124412
'12:30:45.123456-', # Extra at end of microsecond time
44134413
'12:30:45.123456+', # Extra at end of microsecond time
44144414
'12:30:45.123456+12:00:30a', # Extra at end of full time
4415+
'12.5', # Decimal mark at end of hour
4416+
'12:30,5', # Decimal mark at end of minute
44154417
]
44164418

44174419
for bad_str in bad_strs:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ Greg Chapman
315315
Mitch Chapman
316316
Matt Chaput
317317
William Chargin
318+
Ben Chatterton
318319
Yogesh Chaudhari
319320
Gautam Chaudhuri
320321
David Chaum
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Raise error on certain technically valid but pathological ISO 8601 strings passed to :meth:`datetime.time.fromisoformat` that were previously parsed incorrectly.

Modules/_datetimemodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,9 @@ parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end, int *hour,
10201020
continue;
10211021
}
10221022
else if (c == '.' || c == ',') {
1023+
if (i < 2) {
1024+
return -3; // Decimal mark on hour or minute
1025+
}
10231026
break;
10241027
} else if (!has_separator) {
10251028
--p;

0 commit comments

Comments
 (0)