Skip to content

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

Open
fedonman wants to merge 2 commits 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 2 commits into
python:mainfrom
fedonman:fix-gh-153854-imaplib-time2internaldate

Conversation

@fedonman

Copy link
Copy Markdown

imaplib.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 before that check:

elif isinstance(date_time, str) and (date_time[0],date_time[-1]) == ('"','"'):

so an empty string escaped as a bare IndexError:

>>> import imaplib
>>> imaplib.Time2Internaldate('')
Traceback (most recent call last):
  ...
IndexError: string index out of range
>>> imaplib.Time2Internaldate('x')  # non-empty unquoted string is fine
Traceback (most recent call last):
  ...
ValueError: date_time not of a known type

This replaces the subscript comparison with str.startswith('"') and
str.endswith('"'), which handle the empty string gracefully while
preserving behaviour for every other input (a properly double-quoted
string is still passed through unchanged; anything else raises
ValueError).

Regression tests are added for the empty/malformed string cases and for
the quoted-string passthrough.

🤖 Generated with Claude Code

…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.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@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

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