Skip to content

code.co_positions behaviour does not match documentation #100117

Description

@iritkatriel

The documentation of co_positions() says:

The iterator returns tuples containing the (start_line, end_line, start_column, end_column). The i-th tuple corresponds to the position of the source code that compiled to the i-th instruction.

I think this is incorrect, because the iterator returns tuples for cache entries as well:

>>> def f():
...   a.b = 1
... 
>>> import dis
>>> dis.dis(f)
  1           0 RESUME                   0

  2           2 LOAD_CONST               1 (1)
              4 LOAD_GLOBAL              0 (a)
             16 STORE_ATTR               1 (b)
             26 LOAD_CONST               0 (None)
             28 RETURN_VALUE
>>> len(list(f.__code__.co_positions()))
15
>>> from pprint import pprint as pp
>>> pp(list(f.__code__.co_positions()))
[(1, 1, 0, 0),
 (2, 2, 8, 9),
 (2, 2, 2, 3),
 (2, 2, 2, 3),
 (2, 2, 2, 3),
 (2, 2, 2, 3),
 (2, 2, 2, 3),
 (2, 2, 2, 3),
 (2, 2, 2, 5),
 (2, 2, 2, 5),
 (2, 2, 2, 5),
 (2, 2, 2, 5),
 (2, 2, 2, 5),
 (2, 2, 2, 5),
 (2, 2, 2, 5)]
>>> 

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    docsDocumentation in the Doc dir
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    3.11only security fixes
    3.12only security fixes
    on Dec 8, 2022
  2. iritkatriel commented on Dec 8, 2022

    @iritkatriel
    MemberAuthor
  3. changed the title [-]error documentation of code.co_positions[/-] [+]error in documentation of code.co_positions[/+] on Dec 8, 2022
  4. changed the title [-]error in documentation of code.co_positions[/-] [+]code.co_positions behaviour does not match documentation[/+] on Dec 12, 2022
  5. brandtbucher commented on Dec 21, 2022

    @brandtbucher
    Member

    I think this is probably just a place where the docs could be improved.

    Are you suggesting replacing "instruction" with "code unit"? Or adding a note that inline CACHE "instructions" are included? I'd be fine with either.

  6. iritkatriel commented on Dec 22, 2022

    @iritkatriel
    MemberAuthor

    We discussed this at the meeting and I think the view was that the implementation needs to change. @markshannon ?

  7. markshannon commented on Dec 22, 2022

    @markshannon
    Member

    I don't think we can change the current behavior of emitting one tuple per code unit . The docs should reflect that behaviour.

    I think it is co_lines() that we should modify. We can merge successive tuples with the same line number, now that #99556 is merged.

  8. added a commit that references this issue on Jan 10, 2023
  9. iritkatriel commented on Dec 11, 2023

    @iritkatriel
    MemberAuthor

    This shouldn't have been closed, the doc is still incorrect.

  10. added a commit that references this issue on May 21, 2024
  11. added a commit that references this issue on May 31, 2024
  12. added 3 commits that reference this issue on May 31, 2024
  13. added 2 commits that reference this issue on May 31, 2024
  14. added a commit that references this issue on Jul 11, 2024
  15. added a commit that references this issue on Jul 17, 2024
  16. added a commit that references this issue on Sep 4, 2024
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

    3.11only security fixes3.12only security fixes3.13only security fixesdocsDocumentation in the Doc dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions