Skip to content

PyCode_Addr2Location always returns 1 #162074

Open

Description

@xionghul

PyCode_Addr2Location returns int, but both paths in Objects/codeobject.c return 1:

  • a negative addrq stores co_firstlineno and column 0, then returns 1
  • any other addrq walks the line table and returns 1

No path returns 0. A caller that treats 0 as failure never takes that branch. code._co_position_at in gh-161800 has this check:

if (!PyCode_Addr2Location(code, addrq, &start_line, &start_column,
                          &end_line, &end_column)) {
    PyErr_SetString(PyExc_SystemError, "PyCode_Addr2Location failed");
    return NULL;
}

That SystemError cannot be raised. This is separate from gh-161799, so it was left unchanged in that pull request.

The header in Include/cpython/code.h does not say what the return value means.

Activity

  1. added
    type-refactorCode refactoring (with no changes in behavior)
    type-featureA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    and removed
    type-refactorCode refactoring (with no changes in behavior)
    on Nov 16, 2026
  2. aisk commented on Nov 16, 2026

    @aisk
    Member

    This is a public API, and it is also documented behavior. Changing it would introduce some unnecessary breaks for users. And in the future we may introduce an error path, so I think changing its return type to void is not a good choice.

  3. picnixz commented on Nov 16, 2026

    @picnixz
    Member

    I think the error was that it returned 1 if addr was negative instead of 0

    Returns 1 if the function succeeds and 0 otherwise.

    But I am no longer really sure about it. So either it is meant for reimplementors or docs should be updated.

  4. added
    type-bugAn unexpected behavior, bug, or error
    and removed
    type-featureA feature request or enhancement
    on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions