Skip to content

Commit f05bfc3

Browse files
[3.13] gh-152060: Fix _pydatetime.fromisoformat() raising AssertionError on invalid lengths (GH-152061) (#152787)
Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent c8c32e2 commit f05bfc3

3 files changed

Lines changed: 6 additions & 1 deletion

File tree

Lib/_pydatetime.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ def _find_isoformat_datetime_separator(dtstr):
335335
def _parse_isoformat_date(dtstr):
336336
# It is assumed that this is an ASCII-only string of lengths 7, 8 or 10,
337337
# see the comment on Modules/_datetimemodule.c:_find_isoformat_datetime_separator
338-
assert len(dtstr) in (7, 8, 10)
338+
if len(dtstr) not in (7, 8, 10):
339+
raise ValueError("Invalid isoformat string")
339340
year = int(dtstr[0:4])
340341
has_sep = dtstr[4] == '-'
341342

Lib/test/datetimetester.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,6 +3395,7 @@ def test_fromisoformat_fails_datetime(self):
33953395
'2009-04-19T12:30:45.400 +02:30', # Space between ms and timezone (gh-130959)
33963396
'2009-04-19T12:30:45.400 ', # Trailing space (gh-130959)
33973397
'2009-04-19T12:30:45. 400', # Space before fraction (gh-130959)
3398+
'2020-2020', # Ambiguous 9-char date portion
33983399
]
33993400

34003401
for bad_str in bad_strs:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :meth:`datetime.datetime.fromisoformat` raising :exc:`AssertionError`
2+
instead of :exc:`ValueError` for some malformed strings in the pure-Python
3+
implementation, matching the C implementation.

0 commit comments

Comments
 (0)