Skip to content

gh-153854: Raise ValueError from imaplib.Time2Internaldate on empty string#153874

Open
fedonman wants to merge 1 commit into
python:mainfrom
fedonman:fix-gh-153854-imaplib-time2internaldate
Open

gh-153854: Raise ValueError from imaplib.Time2Internaldate on empty string#153874
fedonman wants to merge 1 commit into
python:mainfrom
fedonman:fix-gh-153854-imaplib-time2internaldate

Conversation

@fedonman

@fedonman fedonman commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

imaplib.Time2Internaldate is documented to raise ValueError for input it doesn't recognize, and its final branch does exactly that. The problem is that the branch handling strings looks at date_time[0] and date_time[-1] before reaching that point, so passing an empty string raised IndexError instead:

>>> import imaplib
>>> imaplib.Time2Internaldate('')
IndexError: string index out of range

This switches that check to str.startswith('"') and str.endswith('"'), which handle the empty string fine. A properly quoted string still passes through unchanged, and anything else still raises ValueError.

I added regression tests for the empty and malformed string cases and for the quoted-string passthrough.

@fedonman
fedonman requested a review from a team as a code owner July 17, 2026 22:59
@python-cla-bot

python-cla-bot Bot commented Jul 17, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

…mpty string

Time2Internaldate() is documented to raise ValueError for input that is
not of a known type, and its final branch does so. However, the string
branch subscripted the value with date_time[0]/date_time[-1] before that
check, so an empty string escaped as a bare IndexError. Use str.startswith()
and str.endswith() instead, which handle the empty string gracefully while
preserving behaviour for every other input.
@fedonman
fedonman force-pushed the fix-gh-153854-imaplib-time2internaldate branch from 0b78032 to e5bd248 Compare July 18, 2026 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant