Skip to content

gh-127260: Make more consistent to raising errors of fromisoformat methods in both implementations #130134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 18, 2025
Merged
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
4 changes: 2 additions & 2 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def _parse_hh_mm_ss_ff(tstr):

if pos < len_str:
if tstr[pos] not in '.,':
raise ValueError("Invalid microsecond component")
raise ValueError("Invalid microsecond separator")
else:
pos += 1

Expand Down Expand Up @@ -489,7 +489,7 @@ def _parse_isoformat_time(tstr):
# HH:MM:SS len: 8
# HH:MM:SS.f+ len: 10+

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

tz_comps = _parse_hh_mm_ss_ff(tzstr)
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -3533,6 +3533,8 @@ def test_fromisoformat_fails_datetime(self):
'2009-04-32T24:00:00.000000', # Day is invalid before wrapping due to 24:00
'2009-13-01T24:00:00.000000', # Month is invalid before wrapping due to 24:00
'9999-12-31T24:00:00.000000', # Year is invalid after wrapping due to 24:00
'2009-04-19T12:30Z12:00', # Extra time zone info after Z
'2009-04-19T12:30:45:334034', # Invalid microsecond separator
]

for bad_str in bad_strs:
Expand Down Expand Up @@ -4658,6 +4660,7 @@ def test_fromisoformat_time_examples(self):
('00:00:00.000', self.theclass(0, 0)),
('000000.000000', self.theclass(0, 0)),
('00:00:00.000000', self.theclass(0, 0)),
('00:00:00,100000', self.theclass(0, 0, 0, 100000)),
('1200', self.theclass(12, 0)),
('12:00', self.theclass(12, 0)),
('120000', self.theclass(12, 0)),
Expand Down Expand Up @@ -4725,6 +4728,8 @@ def test_fromisoformat_fails(self):
'12:30:45.123456+12:00:30a', # Extra at end of full time
'12.5', # Decimal mark at end of hour
'12:30,5', # Decimal mark at end of minute
'12:30:45.123456Z12:00', # Extra time zone info after Z
'12:30:45:334034', # Invalid microsecond separator
]

for bad_str in bad_strs:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Forbid the use of colon (":") as a fractional component separator and other
improvements to the consistency of error raising between the C and Python
implementaitons of :meth:`datetime.time.fromisoformat` and
:meth:`datetime.datetime.fromisoformat`. Patch by Semyon Moroz.
3 changes: 3 additions & 0 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,9 @@ parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end, int *hour,
return c != '\0';
}
else if (has_separator && (c == ':')) {
if (i == 2) {
return -4; // Malformed microsecond separator
}
continue;
}
else if (c == '.' || c == ',') {
Expand Down
Loading