diff --git a/Lib/imaplib.py b/Lib/imaplib.py index 139da1d3bb6fb8..d193c1d8d884be 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -2272,7 +2272,7 @@ def Time2Internaldate(date_time): if date_time.tzinfo is None: raise ValueError("date_time must be aware") dt = date_time - elif isinstance(date_time, str) and (date_time[0],date_time[-1]) == ('"','"'): + elif isinstance(date_time, str) and (date_time[:1], date_time[-1:]) == ('"', '"'): return date_time # Assume in correct format else: raise ValueError("date_time not of a known type") diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py index 100791a9c3eb2c..c7f28bb917c640 100644 --- a/Lib/test/test_imaplib.py +++ b/Lib/test/test_imaplib.py @@ -152,6 +152,14 @@ def test_Time2Internaldate_datetime_timetuple(self): '"18-May-2033 05:33:20 +0200"', ) + def test_Time2Internaldate_invalid_str(self): + # gh-153854: an empty string raised IndexError instead of the + # documented ValueError. + for invalid in '', '18-May-2033 05:33:20 +0200': + with self.subTest(invalid=invalid): + with self.assertRaises(ValueError): + imaplib.Time2Internaldate(invalid) + def test_that_Time2Internaldate_returns_a_result(self): # Without tzset, we can check only that it successfully # produces a result, not the correctness of the result itself, diff --git a/Misc/NEWS.d/next/Library/2026-07-17-14-30-00.gh-issue-153854.Xw3Fyz.rst b/Misc/NEWS.d/next/Library/2026-07-17-14-30-00.gh-issue-153854.Xw3Fyz.rst new file mode 100644 index 00000000000000..51b9f5418b583b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-17-14-30-00.gh-issue-153854.Xw3Fyz.rst @@ -0,0 +1,2 @@ +Fix :func:`imaplib.Time2Internaldate` raising :exc:`IndexError` instead of +:exc:`ValueError` when passed an empty string.