From 96c10f1e23281b7cfb99e71709c77bd7a2826113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tonghuaroot=20=28=E7=AB=A5=E8=AF=9D=29?= Date: Wed, 1 Jul 2026 18:21:12 +0800 Subject: [PATCH] gh-152157: Reject empty fractions in `_datetime.{date}time.fromisoformat` (GH-152161) (cherry picked from commit 112c69a0514902af2d2f7d0503de6690ca59a10f) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: tonghuaroot (童话) Co-authored-by: Stan Ulbrych --- Lib/test/datetimetester.py | 8 ++++++++ ...2026-06-25-07-08-17.gh-issue-152157.dt5Ef0.rst | 3 +++ Modules/_datetimemodule.c | 15 ++++++++++----- 3 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-06-25-07-08-17.gh-issue-152157.dt5Ef0.rst diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 3e7aebe8ecf9bf..c4b81771f88caf 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -3395,6 +3395,10 @@ def test_fromisoformat_fails_datetime(self): '2009-04-19T12:30:45.400 +02:30', # Space between ms and timezone (gh-130959) '2009-04-19T12:30:45.400 ', # Trailing space (gh-130959) '2009-04-19T12:30:45. 400', # Space before fraction (gh-130959) + '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: @@ -4508,6 +4512,10 @@ def test_fromisoformat_fails(self): '12:30:45.400 +02:30', # Space between ms and timezone (gh-130959) '12:30:45.400 ', # Trailing space (gh-130959) '12:30:45. 400', # Space before fraction (gh-130959) + '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: diff --git a/Misc/NEWS.d/next/Library/2026-06-25-07-08-17.gh-issue-152157.dt5Ef0.rst b/Misc/NEWS.d/next/Library/2026-06-25-07-08-17.gh-issue-152157.dt5Ef0.rst new file mode 100644 index 00000000000000..00e83b4af6be7a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-25-07-08-17.gh-issue-152157.dt5Ef0.rst @@ -0,0 +1,3 @@ +The C implementations of :meth:`~datetime.datetime.fromisoformat` and :meth:`~datetime.time.fromisoformat` +now reject a decimal separator that is not followed by any +fractional digit before a timezone designator. diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index e0d1f57c259c42..f54ec606888f88 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -1013,17 +1013,22 @@ 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 (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 == ':')) { continue; } - else if (c == '.' || c == ',') { - break; - } else if (!has_separator) { + else if (!has_separator) { --p; - } else { + } + else { return -4; // Malformed time separator } }