Skip to content

gh-156725: document O(N) behavior of PyCode_Addr2Line and the private line-table API - #156736

Merged
ZeroIntensity merged 4 commits into
python:mainfrom
Himesh-rupchandani:gh-156725-code-addrtoline-docs
Oct 23, 2026
Merged

ZeroIntensity merged 4 commits into
python:mainfrom
Himesh-rupchandani:gh-156725-code-addrtoline-docs

Conversation

@Himesh-rupchandani

@Himesh-rupchandani Himesh-rupchandani commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

[Closes gh-156725.

Summary

The C-API docs for PyCode_Addr2Line pointed to the PEP 626 section describing
PyLineTable_InitAddressRange, PyLineTable_NextAddressRange, and
PyLineTable_PreviousAddressRange. Those functions were renamed with a leading
underscore and moved to the private header Include/internal/pycore_code.h; they
are no longer a public C-API.

This docs PR:

  • notes that PyCode_Addr2Line is O(N) in the number of instructions;
  • points to the current private line-table iteration API (_PyCode_InitAddressRange,
    _PyLineTable_NextAddressRange, _PyLineTable_PreviousAddressRange) and flags
    them as internal/unstable.

I left the request to expose these as PyUnstable public APIs as a separate feature
proposal, per the issue.](gh-156725: Document O(N) behavior of PyCode_Addr2Line)

@bedevere-app bedevere-app Bot added docs Documentation in the Doc dir skip news labels Aug 31, 2026
@python-cla-bot

python-cla-bot Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@read-the-docs-community

read-the-docs-community Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

…rivate 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).
@Himesh-rupchandani
Himesh-rupchandani force-pushed the gh-156725-code-addrtoline-docs branch from 3f8d96b to 7707506 Compare August 31, 2026 19:07
Comment thread Doc/c-api/code.rst Outdated
Comment on lines +114 to +117
.. warning::
These functions are **internal and not a public C-API**. They are
declared in a private header, may change or be removed without notice,
and are not exported as stable symbols.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure if we should document internal. Users should not be calling them, and pointing it from the docs might send the wrong message.

https://docs.python.org/3/c-api/stable.html#:~:text=Names%20prefixed%20by%20an%20underscore

Comment thread Doc/c-api/code.rst Outdated
For efficiently iterating over the line numbers in a code object, use :pep:`the API described in PEP 626
<0626#out-of-process-debuggers-and-profilers>`.
To iterate efficiently over the line numbers in a code object, use the
private, unstable line-table iteration APIs declared in the private header

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment thread Doc/c-api/code.rst Outdated
Comment on lines +106 to +114
``Include/internal/pycore_code.h``:

* ``_PyCode_InitAddressRange`` initializes a ``PyCodeAddressRange`` for a code object.
* ``_PyLineTable_NextAddressRange`` advances the range to the next
line-number entry.
* ``_PyLineTable_PreviousAddressRange`` retreats the range to the
previous entry.

.. warning::

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 agree that we shouldn't document internal APIs. In a separate PR, let's instead promote these to the unstable API.

Comment thread Doc/c-api/code.rst Outdated
Comment on lines +99 to +100
instructions in the code object, so it is not suitable for iterating over
all the line numbers in a code object.

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 wouldn't say that it's not suitable, it's just not performant:

Suggested change
instructions in the code object, so it is not suitable for iterating over
all the line numbers in a code object.
instructions in the code object, so it is not performant for iterating over
all the line numbers in a code object.

@Himesh-rupchandani

Copy link
Copy Markdown
Contributor Author

Thanks for the review @ZeroIntensity and @maurycy! I have updated the wording to performant and removed the internal line-table API section as suggested. Also happy to open a separate PR promoting the address-range functions to the unstable C API if you'd like!

@Himesh-rupchandani

Copy link
Copy Markdown
Contributor Author

Thanks for the review @ZeroIntensity and @maurycy! I have updated the wording to performant and removed the internal line-table API section as suggested.

Comment thread Doc/c-api/code.rst Outdated
Comment on lines 104 to 105

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.

Per the issue, this API is outdated, and we shouldn't mention it in the docs.

Comment thread Doc/c-api/code.rst Outdated

Return the line number of the instruction that occurs on or before ``byte_offset`` and ends after it.
Return the line number of the instruction that occurs on or before
``byte_offset`` and ends after it. This is ``O(N)`` in the number of

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.

It may be O(N). It's not a guarantee.

@Himesh-rupchandani

Copy link
Copy Markdown
Contributor Author

Thanks @ZeroIntensity! Updated This is -> This may be and removed the outdated PEP 626 reference.

@ZeroIntensity ZeroIntensity 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.

LGTM

@ZeroIntensity ZeroIntensity added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Oct 23, 2026
@ZeroIntensity
ZeroIntensity merged commit 8d11920 into python:main Oct 23, 2026
38 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @Himesh-rupchandani for the PR, and @ZeroIntensity for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@github-project-automation github-project-automation Bot moved this from Todo to Done in Docs PRs Oct 23, 2026
@bedevere-app

bedevere-app Bot commented Oct 23, 2026

Copy link
Copy Markdown

GH-160453 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Oct 23, 2026
@bedevere-app

bedevere-app Bot commented Oct 23, 2026

Copy link
Copy Markdown

GH-160454 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Oct 23, 2026
ZeroIntensity pushed a commit that referenced this pull request Oct 23, 2026
…6736) (GH-160454)

(cherry picked from commit 8d11920)

Co-authored-by: Himesh Rupchandani <rupchandanihimesh69@gmail.com>
ZeroIntensity pushed a commit that referenced this pull request Oct 23, 2026
…6736) (GH-160453)

(cherry picked from commit 8d11920)

Co-authored-by: Himesh Rupchandani <rupchandanihimesh69@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants