Skip to content

gh-159621: Handle empty lists in TokenList.startswith_fws - #159622

Open
KohakuBlueleaf wants to merge 2 commits into
python:mainfrom
KohakuBlueleaf:main
Open

KohakuBlueleaf wants to merge 2 commits into
python:mainfrom
KohakuBlueleaf:main

Conversation

@KohakuBlueleaf

@KohakuBlueleaf KohakuBlueleaf commented Oct 17, 2026 •

Copy link
Copy Markdown

This PR fixed #159621

from email.parser import Parser
from email import policy

value = "a" * 50 + "@example.com,()b@example.com"
m = Parser(policy=policy.default).parsestr(f"To: {value}\r\n\r\nx")

assert m["To"].defects == ()
m.as_string()  # IndexError

The m.as_string() call startswith_fws in empty tokenlist cause IndexError

def startswith_fws(self):
   # return self[0].startswith_fws() <- if self is empty, this will error
   return self and self[0].startswith_fws()

As in some cases the startswith_fws may call within an empty tokenlist, I think check whether "self" is non empty is needed here.
In the same file the "WhiteSpaceTerminal" also do same "non empty check" before testing the self[0] in WPS

@KohakuBlueleaf
KohakuBlueleaf requested a review from a team as a code owner October 17, 2026 12:24
@python-cla-bot

python-cla-bot Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Oct 17, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

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.

email: IndexError when folding address headers containing empty comments

1 participant