Bug description
inspect.getsourcelines() raises tokenize.TokenError for a lambda defined on the same line as the closing delimiter of a multiline string, even when the complete, valid source file is available.
Save the following as repro.py and run it from a file:
import inspect
pair = (
"""text
""", lambda: None
)
print(inspect.getsourcelines(pair[1]))
On CPython 3.14.7 and 3.15.0rc2, the final exception is:
tokenize.TokenError: ('EOF in multi-line string', (1, 1))
Expected behavior: return the source lines containing the lambda without a tokenization error. The file is valid Python, and the lambda itself works normally.
The lambda's co_firstlineno correctly points to the line containing lambda, which also contains the multiline string's closing delimiter. getsourcelines() passes the source slice starting on this line to getblock(). Tokenizing that slice loses the opening delimiter, so the closing delimiter is interpreted as the beginning of a new multiline string.
Moving the lambda onto a separate line avoids the failure. In a larger reproduction containing another triple-quoted string later in the file, inspection can instead return an oversized, incorrect source block.
An equivalent example was also reproduced on CPython 3.8.20, 3.9.25, 3.10.19, 3.11.14, 3.12.12, and 3.13.13. All raised TokenError, with minor differences in the reported error location.
Related reports include #88640 (truncated multiline lambda bodies) and #143299 (adjacent code on shared source lines). I did not find a report for this closing-delimiter case.
CPython versions tested
3.8.20, 3.9.25, 3.10.19, 3.11.14, 3.12.12, 3.13.13, 3.14.7, and 3.15.0rc2.
Operating systems tested
macOS, ARM64.
Linked PRs
Credit to @leycec for finding this issue while developing https://github.com/beartype/beartype.
Bug description
inspect.getsourcelines()raisestokenize.TokenErrorfor a lambda defined on the same line as the closing delimiter of a multiline string, even when the complete, valid source file is available.Save the following as
repro.pyand run it from a file:On CPython 3.14.7 and 3.15.0rc2, the final exception is:
Expected behavior: return the source lines containing the lambda without a tokenization error. The file is valid Python, and the lambda itself works normally.
The lambda's
co_firstlinenocorrectly points to the line containinglambda, which also contains the multiline string's closing delimiter.getsourcelines()passes the source slice starting on this line togetblock(). Tokenizing that slice loses the opening delimiter, so the closing delimiter is interpreted as the beginning of a new multiline string.Moving the lambda onto a separate line avoids the failure. In a larger reproduction containing another triple-quoted string later in the file, inspection can instead return an oversized, incorrect source block.
An equivalent example was also reproduced on CPython 3.8.20, 3.9.25, 3.10.19, 3.11.14, 3.12.12, and 3.13.13. All raised
TokenError, with minor differences in the reported error location.Related reports include #88640 (truncated multiline lambda bodies) and #143299 (adjacent code on shared source lines). I did not find a report for this closing-delimiter case.
CPython versions tested
3.8.20, 3.9.25, 3.10.19, 3.11.14, 3.12.12, 3.13.13, 3.14.7, and 3.15.0rc2.
Operating systems tested
macOS, ARM64.
Linked PRs
Credit to @leycec for finding this issue while developing https://github.com/beartype/beartype.