Skip to content

Commit 7707506

Browse files
gh-156725: document O(N) behavior of PyCode_Addr2Line and the private line-table API
The C-API docs for PyCode_Addr2Line pointed to the PEP 626 section that describes PyLineTable_InitAddressRange / PyLineTable_NextAddressRange / PyLineTable_PreviousAddressRange, but those functions have since been renamed with a leading underscore and moved to the private header Include/internal/pycore_code.h. Point to the current private names, note that they are not a stable public API, and document that PyCode_Addr2Line is O(N).
1 parent 486b000 commit 7707506

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

‎Doc/c-api/code.rst‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,27 @@ bound into a function.
9494
9595
.. c:function:: int PyCode_Addr2Line(PyCodeObject *co, int byte_offset)
9696
97-
Return the line number of the instruction that occurs on or before ``byte_offset`` and ends after it.
97+
Return the line number of the instruction that occurs on or before
98+
``byte_offset`` and ends after it. This is ``O(N)`` in the number of
99+
instructions in the code object, so it is not suitable for iterating over
100+
all the line numbers in a code object.
101+
98102
If you just need the line number of a frame, use :c:func:`PyFrame_GetLineNumber` instead.
99103
100-
For efficiently iterating over the line numbers in a code object, use :pep:`the API described in PEP 626
101-
<0626#out-of-process-debuggers-and-profilers>`.
104+
To iterate efficiently over the line numbers in a code object, use the
105+
private, unstable line-table iteration APIs declared in the private header
106+
``Include/internal/pycore_code.h``:
107+
108+
* ``_PyCode_InitAddressRange`` initializes a ``PyCodeAddressRange`` for a code object.
109+
* ``_PyLineTable_NextAddressRange`` advances the range to the next
110+
line-number entry.
111+
* ``_PyLineTable_PreviousAddressRange`` retreats the range to the
112+
previous entry.
113+
114+
.. warning::
115+
These functions are **internal and not a public C-API**. They are
116+
declared in a private header, may change or be removed without notice,
117+
and are not exported as stable symbols.
102118
103119
.. c:function:: int PyCode_Addr2Location(PyObject *co, int byte_offset, int *start_line, int *start_column, int *end_line, int *end_column)
104120

0 commit comments

Comments
 (0)