https://github.com/python/cpython/commit/427dd10250f94d79bad7927d6ea231950c395bd6
commit: 427dd10250f94d79bad7927d6ea231950c395bd6
branch: main
author: donBarbos <[email protected]>
committer: pganssle <[email protected]>
date: 2025-02-18T15:49:28Z
summary:
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.
files:
A Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst
M Lib/_pydatetime.py
M Lib/test/datetimetester.py
M Modules/_datetimemodule.c
diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py
index 600a7e08345a82..ed8a5cdfaf1cde 100644
--- a/Lib/_pydatetime.py
+++ b/Lib/_pydatetime.py
@@ -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
@@ -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)
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index d1ef6339789d20..b670973a71c748 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -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:
@@ -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)),
@@ -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:
diff --git
a/Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst
b/Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst
new file mode 100644
index 00000000000000..453aa4a8678fe4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-02-14-23-38-03.gh-issue-127260.dXQ8P0.rst
@@ -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.
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 8b202cc51788a9..b5ccf2f7b65d63 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -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 == ',') {
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]