From 8af6c9ad22c2facf17a8c2a59e042545305cb06f Mon Sep 17 00:00:00 2001 From: xionghuluo Date: Wed, 11 Nov 2026 16:17:01 +0800 Subject: [PATCH 1/3] Look up one bytecode position in inspect and traceback 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 --- Lib/inspect.py | 6 +++ Lib/test/test_code.py | 33 +++++++++++++++++ Lib/test/test_inspect/test_inspect.py | 13 +++++++ Lib/traceback.py | 4 ++ ...26-11-11-16-10-00.gh-issue-0.position1.rst | 2 + Objects/codeobject.c | 37 +++++++++++++++++++ 6 files changed, 95 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-11-16-10-00.gh-issue-0.position1.rst diff --git a/Lib/inspect.py b/Lib/inspect.py index 81ea5372eec235..1796ef66291979 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1612,6 +1612,12 @@ def _get_code_position_from_tb(tb): def _get_code_position(code, instruction_index): if instruction_index < 0: return (None, None, None, None) + # Byte offset, the same value traceback.c passes to PyCode_Addr2Location. + # co_positions() builds a tuple per earlier instruction. Real code objects + # can look the one offset up directly. Duck-typed code objects only + # implement co_positions(). + if isinstance(code, types.CodeType): + return code._co_position_at(instruction_index) positions_gen = code.co_positions() # The nth entry in code.co_positions() corresponds to instruction (2*n)th since Python 3.10+ return next(itertools.islice(positions_gen, instruction_index // 2, None)) diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py index f54703cf37b2de..32415826f26d8a 100644 --- a/Lib/test/test_code.py +++ b/Lib/test/test_code.py @@ -583,6 +583,39 @@ def func(): self.assertIsNone(line) self.assertEqual(end_line, new_code.co_firstlineno + 1) + def test_co_position_at_matches_co_positions(self): + # _co_position_at is the single-offset form of co_positions(). + # The argument is a byte offset, and each instruction is 2 bytes. + import inspect + import traceback + + def func(x): + return x + 1 + + samples = [ + func.__code__, + (lambda y: y).__code__, + func.__code__.replace(co_linetable=b''), + ] + for code in samples: + with self.subTest(code=code): + for index, expected in enumerate(code.co_positions()): + offset = index * 2 + self.assertEqual(code._co_position_at(offset), expected) + self.assertEqual( + inspect._get_code_position(code, offset), expected) + self.assertEqual( + traceback._get_code_position(code, offset), expected) + self.assertEqual( + inspect._get_code_position(code, -1), + (None, None, None, None)) + 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) + def test_code_equality(self): def f(): try: diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index ef9d2061611f33..d76a6a9a1a8c20 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -586,6 +586,19 @@ def test_trace(self): (modfile, 18, 'eggs', [' q = y / 0\n'], 0)) self.assertEqual(frame3.positions, dis.Positions(18, 18, 8, 13)) + def test_stack_context_zero_keeps_positions(self): + # context=0 drops source lines. Column positions stay filled, and + # the filename still comes from getsourcefile(). + stack = inspect.stack(0) + top = stack[0] + self.assertIsNone(top.code_context) + self.assertIsNone(top.index) + self.assertIn(tuple(top.positions), + list(top.frame.f_code.co_positions())) + self.assertEqual( + top.filename, + inspect.getsourcefile(top.frame) or inspect.getfile(top.frame)) + def test_frame(self): args, varargs, varkw, locals = inspect.getargvalues(mod.fr) self.assertEqual(args, ['x', 'y']) diff --git a/Lib/traceback.py b/Lib/traceback.py index 3ecb1dd318fbcb..e6c4baf59f22a2 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -471,6 +471,10 @@ def _walk_tb_with_full_positions(tb): def _get_code_position(code, instruction_index): if instruction_index < 0: return (None, None, None, None) + # 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) positions_gen = code.co_positions() return next(itertools.islice(positions_gen, instruction_index // 2, None)) diff --git a/Misc/NEWS.d/next/Library/2026-11-11-16-10-00.gh-issue-0.position1.rst b/Misc/NEWS.d/next/Library/2026-11-11-16-10-00.gh-issue-0.position1.rst new file mode 100644 index 00000000000000..d98feb433dd3d6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-11-16-10-00.gh-issue-0.position1.rst @@ -0,0 +1,2 @@ +:mod:`inspect` and :mod:`traceback` now look up one bytecode position directly. +Previously they built a tuple for every instruction before the requested offset. diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 2255220bec3d30..d7c6da2c64bd6d 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1501,6 +1501,42 @@ code_positionsiterator(PyObject *self, PyObject* Py_UNUSED(args)) return (PyObject*)pi; } +/* One position for inspect and traceback. + + co_positions() yields one tuple per instruction, so islice() builds a + tuple for every instruction before the requested offset. + PyCode_Addr2Location walks the same table and only fills four ints. + The offset is a byte offset (f_lasti / tb_lasti), not an instruction index. +*/ +static PyObject * +code_co_position_at(PyObject *self, PyObject *indexobj) +{ + PyCodeObject *code = (PyCodeObject *)self; + int start_line, end_line, start_column, end_column; + long addrq; + Py_ssize_t nbytes; + + addrq = PyLong_AsLong(indexobj); + if (addrq == -1 && PyErr_Occurred()) { + return NULL; + } + nbytes = _PyCode_NBYTES(code); + if (addrq < 0 || addrq >= nbytes || addrq > INT_MAX) { + PyErr_SetString(PyExc_ValueError, "bytecode offset out of range"); + return NULL; + } + if (!PyCode_Addr2Location(code, (int)addrq, &start_line, &start_column, + &end_line, &end_column)) { + PyErr_SetString(PyExc_SystemError, "PyCode_Addr2Location failed"); + return NULL; + } + return Py_BuildValue("(O&O&O&O&)", + _source_offset_converter, &start_line, + _source_offset_converter, &end_line, + _source_offset_converter, &start_column, + _source_offset_converter, &end_column); +} + /****************** * "extra" frame eval info (see PEP 523) @@ -2873,6 +2909,7 @@ static struct PyMethodDef code_methods[] = { {"co_lines", code_linesiterator, METH_NOARGS}, {"co_branches", code_branchesiterator, METH_NOARGS}, {"co_positions", code_positionsiterator, METH_NOARGS}, + {"_co_position_at", code_co_position_at, METH_O}, CODE_REPLACE_METHODDEF CODE__VARNAME_FROM_OPARG_METHODDEF {"__replace__", _PyCFunction_CAST(code_replace), METH_FASTCALL|METH_KEYWORDS, From f45a24dd24589179082dc6763c066c04461b2de8 Mon Sep 17 00:00:00 2001 From: xionghuluo Date: Wed, 11 Nov 2026 17:19:38 +0800 Subject: [PATCH 2/3] Use the issue number in the inspect position NEWS entry blurb rejects gh-issue-0, which failed the documentation build. Co-authored-by: Cursor --- .../next/Library/2026-11-11-16-10-00.gh-issue-0.position1.rst | 2 -- .../Library/2026-11-11-16-10-00.gh-issue-161799.position1.rst | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2026-11-11-16-10-00.gh-issue-0.position1.rst create mode 100644 Misc/NEWS.d/next/Library/2026-11-11-16-10-00.gh-issue-161799.position1.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-11-16-10-00.gh-issue-0.position1.rst b/Misc/NEWS.d/next/Library/2026-11-11-16-10-00.gh-issue-0.position1.rst deleted file mode 100644 index d98feb433dd3d6..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-11-11-16-10-00.gh-issue-0.position1.rst +++ /dev/null @@ -1,2 +0,0 @@ -:mod:`inspect` and :mod:`traceback` now look up one bytecode position directly. -Previously they built a tuple for every instruction before the requested offset. diff --git a/Misc/NEWS.d/next/Library/2026-11-11-16-10-00.gh-issue-161799.position1.rst b/Misc/NEWS.d/next/Library/2026-11-11-16-10-00.gh-issue-161799.position1.rst new file mode 100644 index 00000000000000..ea892f7ac58b9b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-11-16-10-00.gh-issue-161799.position1.rst @@ -0,0 +1,2 @@ +Speed up :func:`inspect.getframeinfo` and :mod:`traceback` position lookup +on large code objects. From 4c696aaedccc5688536a6b27be8c2aef3e85dc17 Mon Sep 17 00:00:00 2001 From: xionghuluo Date: Mon, 16 Nov 2026 10:25:19 +0800 Subject: [PATCH 3/3] gh-161799: Address review comments on the position lookup Use PyLong_AsInt for the bytecode offset, and test the three lookup paths the same way. Co-authored-by: Cursor --- Lib/inspect.py | 7 ++--- Lib/test/test_code.py | 45 +++++++++++++++++---------- Lib/test/test_inspect/test_inspect.py | 8 ++--- Lib/traceback.py | 3 +- Objects/codeobject.c | 8 ++--- 5 files changed, 39 insertions(+), 32 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 1796ef66291979..26bcd834d0683d 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1610,12 +1610,11 @@ def _get_code_position_from_tb(tb): return _get_code_position(code, instruction_index) def _get_code_position(code, instruction_index): + # Keep this in sync with traceback._get_code_position. + # The offset is a byte offset. Real code objects look it up directly. + # Duck-typed code objects only implement co_positions(). if instruction_index < 0: return (None, None, None, None) - # Byte offset, the same value traceback.c passes to PyCode_Addr2Location. - # co_positions() builds a tuple per earlier instruction. Real code objects - # can look the one offset up directly. Duck-typed code objects only - # implement co_positions(). if isinstance(code, types.CodeType): return code._co_position_at(instruction_index) positions_gen = code.co_positions() diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py index 32415826f26d8a..ca9d24fc1e8fe2 100644 --- a/Lib/test/test_code.py +++ b/Lib/test/test_code.py @@ -195,10 +195,12 @@ """ import copy +import functools import inspect import sys import threading import doctest +import traceback import unittest import textwrap import weakref @@ -586,9 +588,6 @@ def func(): def test_co_position_at_matches_co_positions(self): # _co_position_at is the single-offset form of co_positions(). # The argument is a byte offset, and each instruction is 2 bytes. - import inspect - import traceback - def func(x): return x + 1 @@ -598,23 +597,35 @@ def func(x): func.__code__.replace(co_linetable=b''), ] for code in samples: + lookups = ( + ("code._co_position_at", code._co_position_at), + ("inspect._get_code_position", + functools.partial(inspect._get_code_position, code)), + ("traceback._get_code_position", + functools.partial(traceback._get_code_position, code)), + ) with self.subTest(code=code): for index, expected in enumerate(code.co_positions()): offset = index * 2 - self.assertEqual(code._co_position_at(offset), expected) - self.assertEqual( - inspect._get_code_position(code, offset), expected) - self.assertEqual( - traceback._get_code_position(code, offset), expected) - self.assertEqual( - inspect._get_code_position(code, -1), - (None, None, None, None)) - 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) + for name, lookup in lookups: + with self.subTest(lookup=name, offset=offset): + self.assertEqual(lookup(offset), expected) + + def test_co_position_at_rejects_bad_offsets(self): + def func(x): + return x + 1 + + code = func.__code__ + co_position_at = code._co_position_at + # Negative indexes stay in the Python wrappers. The C method rejects them. + self.assertEqual(inspect._get_code_position(code, -1), + (None, None, None, None)) + self.assertEqual(traceback._get_code_position(code, -1), + (None, None, None, 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) + self.assertRaises(OverflowError, co_position_at, 2**100) def test_code_equality(self): def f(): diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index d76a6a9a1a8c20..415a031f4190ac 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -587,17 +587,15 @@ def test_trace(self): self.assertEqual(frame3.positions, dis.Positions(18, 18, 8, 13)) def test_stack_context_zero_keeps_positions(self): - # context=0 drops source lines. Column positions stay filled, and - # the filename still comes from getsourcefile(). + # context=0 drops source lines. Column positions stay filled. + # The frame is this test function, so the filename is this module. stack = inspect.stack(0) top = stack[0] self.assertIsNone(top.code_context) self.assertIsNone(top.index) + self.assertEqual(top.filename, __file__) self.assertIn(tuple(top.positions), list(top.frame.f_code.co_positions())) - self.assertEqual( - top.filename, - inspect.getsourcefile(top.frame) or inspect.getfile(top.frame)) def test_frame(self): args, varargs, varkw, locals = inspect.getargvalues(mod.fr) diff --git a/Lib/traceback.py b/Lib/traceback.py index e6c4baf59f22a2..299ddd296e4311 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -469,10 +469,9 @@ def _walk_tb_with_full_positions(tb): def _get_code_position(code, instruction_index): + # Keep this in sync with inspect._get_code_position. if instruction_index < 0: return (None, None, None, None) - # 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) positions_gen = code.co_positions() diff --git a/Objects/codeobject.c b/Objects/codeobject.c index d7c6da2c64bd6d..b80338836a4f63 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1513,19 +1513,19 @@ code_co_position_at(PyObject *self, PyObject *indexobj) { PyCodeObject *code = (PyCodeObject *)self; int start_line, end_line, start_column, end_column; - long addrq; + int addrq; Py_ssize_t nbytes; - addrq = PyLong_AsLong(indexobj); + addrq = PyLong_AsInt(indexobj); if (addrq == -1 && PyErr_Occurred()) { return NULL; } nbytes = _PyCode_NBYTES(code); - if (addrq < 0 || addrq >= nbytes || addrq > INT_MAX) { + if (addrq < 0 || addrq >= nbytes) { PyErr_SetString(PyExc_ValueError, "bytecode offset out of range"); return NULL; } - if (!PyCode_Addr2Location(code, (int)addrq, &start_line, &start_column, + if (!PyCode_Addr2Location(code, addrq, &start_line, &start_column, &end_line, &end_column)) { PyErr_SetString(PyExc_SystemError, "PyCode_Addr2Location failed"); return NULL;