Skip to content

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

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

xionghul wants to merge 3 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
@picnixz picnixz reopened this Nov 13, 2026
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 13, 2026

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @pablogsal as author of PEP-657 and perhaps @gaogaotiantian

Comment thread Lib/test/test_inspect/test_inspect.py Outdated
Comment on lines +598 to +600
self.assertEqual(
top.filename,
inspect.getsourcefile(top.frame) or inspect.getfile(top.frame))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit arbitrary here. Why can't we determine in advance the exact value of top.filename?

Comment thread Lib/test/test_code.py Outdated
Comment on lines +589 to +590
import inspect
import traceback

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move those imports as top-level ones. Why are they local?

Comment thread Lib/test/test_code.py Outdated
Comment on lines +612 to +617
with self.assertRaises(ValueError):
code._co_position_at(-1)
with self.assertRaises(ValueError):
code._co_position_at(len(code.co_code))
with self.assertRaises(TypeError):
code._co_position_at(None)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with self.assertRaises(ValueError):
code._co_position_at(-1)
with self.assertRaises(ValueError):
code._co_position_at(len(code.co_code))
with self.assertRaises(TypeError):
code._co_position_at(None)
self.assertRaises(ValueError, co_position_at, -1)
self.assertRaises(ValueError, co_position_at, len(code.co_code))
self.assertRaises(TypeError, co_position_at, None)

and store co_position_at = code._co_position_at at the beginning of the loop. In addition, those tests should be separate, they are not testing the equivalence with co_positions() exactly.

Comment thread Lib/test/test_code.py Outdated
self.assertEqual(
traceback._get_code_position(code, offset), expected)
self.assertEqual(
inspect._get_code_position(code, -1),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not testing traceback._get_code_position as well?

Comment thread Lib/test/test_code.py Outdated
with self.subTest(code=code):
for index, expected in enumerate(code.co_positions()):
offset = index * 2
self.assertEqual(code._co_position_at(offset), expected)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest looping over the various implementations of co_position_at:

for co_position_at in [
    code._co_position_at,
    partial(inspect._get_code_position, code),
    partial(traceback._get_code_position, code),
]:
    ...

Comment thread Lib/traceback.py Outdated
Comment on lines +474 to +477
# Byte offset. See inspect._get_code_position. Objects that only
# implement co_positions() keep the iterator path.
if isinstance(code, types.CodeType):
return code._co_position_at(instruction_index)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Byte offset. See inspect._get_code_position. Objects that only
# implement co_positions() keep the iterator path.
if isinstance(code, types.CodeType):
return code._co_position_at(instruction_index)
if isinstance(code, types.CodeType):
return code._co_position_at(instruction_index)

Comment thread Lib/traceback.py
@@ -471,6 +471,10 @@ def _walk_tb_with_full_positions(tb):
def _get_code_position(code, instruction_index):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment saying that this sbould be kept in sync with inspect._get_code_position simply.

Comment thread Objects/codeobject.c Outdated
return NULL;
}
nbytes = _PyCode_NBYTES(code);
if (addrq < 0 || addrq >= nbytes || addrq > INT_MAX) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using PyLong_AsInt to avoid the check on INT_MAX?

Comment thread Objects/codeobject.c Outdated
PyErr_SetString(PyExc_ValueError, "bytecode offset out of range");
return NULL;
}
if (!PyCode_Addr2Location(code, (int)addrq, &start_line, &start_column,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just observed that PyCode_Addr2Location never returns 0 and I think it's another issue. Could you open a separate issue for it please?

@bedevere-app

bedevere-app Bot commented Nov 13, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Use PyLong_AsInt for the bytecode offset, and test the three lookup paths the same way.

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

Copy link
Copy Markdown
Author

I have made the requested changes; please review again.

  • code._co_position_at now uses PyLong_AsInt. Values that do not fit in a C int raise OverflowError; negative and out-of-range offsets still raise ValueError.
  • The in-range comparison loops over code._co_position_at, inspect._get_code_position, and traceback._get_code_position. The error cases are a separate test.
  • inspect.stack(0) now checks the filename against this test module.
  • traceback._get_code_position notes that it stays in sync with inspect._get_code_position.

PyCode_Addr2Location always returns 1. I will open that as a separate issue rather than change it here.

@bedevere-app

bedevere-app Bot commented Nov 16, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@picnixz: please review the changes made to this pull request.

@xionghul

Copy link
Copy Markdown
Author

Opened the separate issue for PyCode_Addr2Location always returning 1: #162074

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting change review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants