Skip to content

gh-158556: Fix inspect.getsource() for a lambda after a multiline string - #158558

Open
drakeo338 wants to merge 2 commits into
python:mainfrom
drakeo338:claude/158556-fix
Open

drakeo338 wants to merge 2 commits into
python:mainfrom
drakeo338:claude/158556-fix

Conversation

@drakeo338

Copy link
Copy Markdown

Fixes #158556.

Pull Request title

gh-158556: Fix inspect.getsource() for a lambda after a multiline string

Backport Pull Request title

Not a backport.

Description

inspect.getsource() and getsourcelines() returned the wrong lines for a lambda or generator expression that begins on the line where a multiline string ends, because the block finder started at that line instead of at the start of the string. This change makes the source begin where the string starts. Tests and a NEWS entry are included.

I used AI assistance for this change and ran ./python -m test test_inspect on the committed HEAD (388 tests, passing).

…ne string

getblock() tokenized lines starting in the middle of a multiline string, so the closing delimiter was read as an opening one and tokenize raised TokenError.
… multiline string

A lambda or generator expression that begins on the line where a multiline
string ends was tokenized starting in the middle of that string, so the
closing delimiter was read as an opening one. getblock() either raised
tokenize.TokenError or returned a block that was too long. Start the block
on the line where the string begins instead.
@python-cla-bot

python-cla-bot Bot commented Oct 1, 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.

inspect.getsourcelines raises TokenError for lambda following a multiline string closing delimiter

1 participant