]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-152157: Reject empty fractions in `_datetime.{date}time.fromisoformat`...
authortonghuaroot (童话) <tonghuaroot@gmail.com>
Wed, 8 Jul 2026 10:37:11 +0000 (18:37 +0800)
committerGitHub <noreply@github.com>
Wed, 8 Jul 2026 10:37:11 +0000 (12:37 +0200)
(cherry picked from commit 112c69a0514902af2d2f7d0503de6690ca59a10f)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
Lib/test/datetimetester.py
Misc/NEWS.d/next/Library/2026-06-25-07-08-17.gh-issue-152157.dt5Ef0.rst [new file with mode: 0644]
Modules/_datetimemodule.c

index 0792b14471b937716bff7998d809c6a1983535d5..1b71b71c544c0cb99cf11cb5283f7f1378837094 100644 (file)
@@ -3582,6 +3582,10 @@ class TestDateTime(TestDate):
             '2009-04-19T12:30:45.400 ',        # Trailing space (gh-130959)
             '2009-04-19T12:30:45. 400',        # Space before fraction (gh-130959)
             '2020-2020',                       # Ambiguous 9-char date portion
+            '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:
@@ -4838,6 +4842,10 @@ class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
             '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 (file)
index 0000000..00e83b4
--- /dev/null
@@ -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.
index 3122d0196519d5520ddcd610a31a6635c2b8b6d3..f81234f39307c6747bed050b4d610899061a8a9e 100644 (file)
@@ -1031,7 +1031,16 @@ 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 (i < 2) {
+                return -3;  // Decimal mark on hour or minute
+            }
+            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 == ':')) {
@@ -1040,14 +1049,10 @@ parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end, int *hour,
             }
             continue;
         }
-        else if (c == '.' || c == ',') {
-            if (i < 2) {
-                return -3; // Decimal mark on hour or minute
-            }
-            break;
-        } else if (!has_separator) {
+        else if (!has_separator) {
             --p;
-        } else {
+        }
+        else {
             return -4;  // Malformed time separator
         }
     }