Skip to content

Commit 3f8d96b

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 3f8d96b

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

‎Doc/c-api/code.rst‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,28 @@ 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+
* :c:func:`_PyCode_InitAddressRange` initializes a
109+
:c:type:`PyCodeAddressRange` for a code object.
110+
* :c:func:`_PyLineTable_NextAddressRange` advances the range to the next
111+
line-number entry.
112+
* :c:func:`_PyLineTable_PreviousAddressRange` retreats the range to the
113+
previous entry.
114+
115+
.. warning::
116+
These functions are **internal and not a public C-API**. They are
117+
declared in a private header, may change or be removed without notice,
118+
and are not exported as stable symbols.
102119
103120
.. c:function:: int PyCode_Addr2Location(PyObject *co, int byte_offset, int *start_line, int *start_column, int *end_line, int *end_column)
104121

0 commit comments

Comments
 (0)