https://github.com/python/cpython/commit/f05bfc3685a803e41dafcdfe1cdf0f33136c6db2 commit: f05bfc3685a803e41dafcdfe1cdf0f33136c6db2 branch: 3.13 author: tonghuaroot (童话) <[email protected]> committer: StanFromIreland <[email protected]> date: 2026-07-01T20:47:57+02:00 summary:
[3.13] gh-152060: Fix `_pydatetime.fromisoformat()` raising `AssertionError` on invalid lengths (GH-152061) (#152787) Co-authored-by: Stan Ulbrych <[email protected]> files: A Misc/NEWS.d/next/Library/2026-06-24-10-46-35.gh-issue-152060.f2asrt.rst M Lib/_pydatetime.py M Lib/test/datetimetester.py diff --git a/Lib/_pydatetime.py b/Lib/_pydatetime.py index 38e1f764f008a9..1135c1d1a1bc52 100644 --- a/Lib/_pydatetime.py +++ b/Lib/_pydatetime.py @@ -335,7 +335,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] == '-' diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 3e7aebe8ecf9bf..4c5d03700e15b0 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -3395,6 +3395,7 @@ 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) + '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 index 00000000000000..6088a6b80829bb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-24-10-46-35.gh-issue-152060.f2asrt.rst @@ -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. _______________________________________________ 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]
