Skip to content

gh-161799: Look up one bytecode position in inspect and traceback - #161800

Closed
xionghul wants to merge 2 commits into
python:mainfrom
xionghul:gh-inspect-one-position
Closed

xionghul wants to merge 2 commits into
python:mainfrom
xionghul:gh-inspect-one-position

Conversation

@xionghul

@xionghul xionghul commented Nov 11, 2026 •

Copy link
Copy Markdown

inspect.getframeinfo() and traceback read the current instruction with itertools.islice(code.co_positions(), lasti // 2). co_positions() yields one tuple per instruction, so islice builds and discards a tuple for every preceding instruction. Callers such as inspect.stack(0) do this for every frame, and the cost grows with the size of the code object.

The byte offset is the same value traceback.c already passes to PyCode_Addr2Location. Expose that lookup as the private method code._co_position_at() and use it for real code objects. The returned tuple matches co_positions(), including None where a column is missing. Objects that only implement co_positions() keep the iterator path. Filename selection is unchanged.

inspect.getframeinfo() and traceback read the current instruction with
itertools.islice(code.co_positions(), lasti // 2). co_positions() yields
one tuple per instruction, so islice builds and discards a tuple for
every preceding instruction. Callers such as inspect.stack(0) do this
for every frame, and the cost grows with the size of the code object.

The byte offset is the same value traceback.c already passes to
PyCode_Addr2Location. Expose that lookup as the private method
code._co_position_at() and use it for real code objects. The returned
tuple matches co_positions(), including None where a column is missing.
Objects that only implement co_positions() keep the iterator path.
Filename selection is unchanged.

Co-authored-by: Cursor <cursoragent@cursor.com>
@python-cla-bot

python-cla-bot Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

blurb rejects gh-issue-0, which failed the documentation build.

Co-authored-by: Cursor <cursoragent@cursor.com>
@picnixz

picnixz commented Nov 11, 2026

Copy link
Copy Markdown
Member

I am not convinced by this change. Until it has been more discussed on the issue, I'm closing the PR.

@picnixz picnixz closed this Nov 11, 2026
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.

2 participants