Skip to content

Commit 427dd10

Browse files
authored
gh-127260: Improve error consistency in both fromisoformat implementations (#130134)
In the Python implementation, "Z" was allowed where only "+" or "-" should be allowed in time zone specifiers. In the C implementation, ":" was allowed as a separator between the whole and fractional portion of times (seconds). These have both been forbidden and the error messages harmonized.
1 parent 46ac85e commit 427dd10

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

Lib/_pydatetime.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ def _parse_hh_mm_ss_ff(tstr):
431431

432432
if pos < len_str:
433433
if tstr[pos] not in '.,':
434-
raise ValueError("Invalid microsecond component")
434+
raise ValueError("Invalid microsecond separator")
435435
else:
436436
pos += 1
437437

@@ -489,7 +489,7 @@ def _parse_isoformat_time(tstr):
489489
# HH:MM:SS len: 8
490490
# HH:MM:SS.f+ len: 10+
491491

492-
if len(tzstr) in (0, 1, 3):
492+
if len(tzstr) in (0, 1, 3) or tstr[tz_pos-1] == 'Z':
493493
raise ValueError("Malformed time zone string")
494494

495495
tz_comps = _parse_hh_mm_ss_ff(tzstr)

Lib/test/datetimetester.py

+5
Original file line numberDiff line numberDiff line change
@@ -3533,6 +3533,8 @@ def test_fromisoformat_fails_datetime(self):
35333533
'2009-04-32T24:00:00.000000', # Day is invalid before wrapping due to 24:00
35343534
'2009-13-01T24:00:00.000000', # Month is invalid before wrapping due to 24:00
35353535
'9999-12-31T24:00:00.000000', # Year is invalid after wrapping due to 24:00
3536+
'2009-04-19T12:30Z12:00', # Extra time zone info after Z
3537+
'2009-04-19T12:30:45:334034', # Invalid microsecond separator
35363538
]
35373539

35383540
for bad_str in bad_strs:
@@ -4658,6 +4660,7 @@ def test_fromisoformat_time_examples(self):
46584660
('00:00:00.000', self.theclass(0, 0)),
46594661
('000000.000000', self.theclass(0, 0)),
46604662
('00:00:00.000000', self.theclass(0, 0)),
4663+
('00:00:00,100000', self.theclass(0, 0, 0, 100000)),
46614664
('1200', self.theclass(12, 0)),
46624665
('12:00', self.theclass(12, 0)),
46634666
('120000', self.theclass(12, 0)),
@@ -4725,6 +4728,8 @@ def test_fromisoformat_fails(self):
47254728
'12:30:45.123456+12:00:30a', # Extra at end of full time
47264729
'12.5', # Decimal mark at end of hour
47274730
'12:30,5', # Decimal mark at end of minute
4731+
'12:30:45.123456Z12:00', # Extra time zone info after Z
4732+
'12:30:45:334034', # Invalid microsecond separator
47284733
]
47294734

47304735
for bad_str in bad_strs:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Forbid the use of colon (":") as a fractional component separator and other
2+
improvements to the consistency of error raising between the C and Python
3+
implementaitons of :meth:`datetime.time.fromisoformat` and
4+
:meth:`datetime.datetime.fromisoformat`. Patch by Semyon Moroz.

Modules/_datetimemodule.c

+3
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,9 @@ parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end, int *hour,
10141014
return c != '\0';
10151015
}
10161016
else if (has_separator && (c == ':')) {
1017+
if (i == 2) {
1018+
return -4; // Malformed microsecond separator
1019+
}
10171020
continue;
10181021
}
10191022
else if (c == '.' || c == ',') {

0 commit comments

Comments
 (0)