]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-152060: Fix `_pydatetime.fromisoformat()` raising `AssertionError` on inval...
authortonghuaroot (童话) <tonghuaroot@gmail.com>
Wed, 1 Jul 2026 18:48:20 +0000 (02:48 +0800)
committerGitHub <noreply@github.com>
Wed, 1 Jul 2026 18:48:20 +0000 (18:48 +0000)
Co-authored-by: Stan Ulbrych <stan@python.org>
Lib/_pydatetime.py
Lib/test/datetimetester.py
Misc/NEWS.d/next/Library/2026-06-24-10-46-35.gh-issue-152060.f2asrt.rst [new file with mode: 0644]

index b22aa7bd76e2d52797d99939b6395a4de9602e64..0f95a57508482370da8e131cf9349e6e3f0af2fb 100644 (file)
@@ -358,7 +358,8 @@ def _find_isoformat_datetime_separator(dtstr):
 def _parse_isoformat_date(dtstr):
     # It is assumed that this is an ASCII-only string of lengths 7, 8 or 10,
     # see the comment on Modules/_datetimemodule.c:_find_isoformat_datetime_separator
-    assert len(dtstr) in (7, 8, 10)
+    if len(dtstr) not in (7, 8, 10):
+        raise ValueError("Invalid isoformat string")
     year = int(dtstr[0:4])
     has_sep = dtstr[4] == '-'
 
index 9653a39da26cefd8c0151a611b2efba1eac9f9f5..0792b14471b937716bff7998d809c6a1983535d5 100644 (file)
@@ -3581,6 +3581,7 @@ class TestDateTime(TestDate):
             '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)
+            '2020-2020',                       # Ambiguous 9-char date portion
         ]
 
         for bad_str in bad_strs:
diff --git a/Misc/NEWS.d/next/Library/2026-06-24-10-46-35.gh-issue-152060.f2asrt.rst b/Misc/NEWS.d/next/Library/2026-06-24-10-46-35.gh-issue-152060.f2asrt.rst
new file mode 100644 (file)
index 0000000..6088a6b
--- /dev/null
@@ -0,0 +1,3 @@
+Fix :meth:`datetime.datetime.fromisoformat` raising :exc:`AssertionError`
+instead of :exc:`ValueError` for some malformed strings in the pure-Python
+implementation, matching the C implementation.