From b74cea23c2e30d3112f4c0b1e93bdfce6aa3c976 Mon Sep 17 00:00:00 2001 From: Ibrahim Shaqqou <101660458+IbrahimShaqqou@users.noreply.github.com> Date: Fri, 17 Jul 2026 13:08:42 -0400 Subject: [PATCH 1/3] gh-153854: Fix imaplib.Time2Internaldate crash on an empty string --- Lib/imaplib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/imaplib.py b/Lib/imaplib.py index 139da1d3bb6fb86..d193c1d8d884be4 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") From c5bd6b0322c6ac94af843dd5333469e6b09765d3 Mon Sep 17 00:00:00 2001 From: Ibrahim Shaqqou <101660458+IbrahimShaqqou@users.noreply.github.com> Date: Fri, 17 Jul 2026 13:09:45 -0400 Subject: [PATCH 2/3] gh-153854: Add regression test for Time2Internaldate('') --- Lib/test/test_imaplib.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py index 100791a9c3eb2c3..c7f28bb917c640d 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, From 4725ba55c99eaf7ae926bde4f1eeff1eff732e08 Mon Sep 17 00:00:00 2001 From: Ibrahim Shaqqou <101660458+IbrahimShaqqou@users.noreply.github.com> Date: Sat, 18 Jul 2026 07:49:47 -0400 Subject: [PATCH 3/3] gh-153854: Add NEWS entry Corrected the exception raised by imaplib.Time2Internaldate when an empty string is passed. --- .../next/Library/2026-07-17-14-30-00.gh-issue-153854.Xw3Fyz.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-07-17-14-30-00.gh-issue-153854.Xw3Fyz.rst 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 000000000000000..51b9f5418b583b1 --- /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.