GeezerVison is showing github.com — code.co_positions behaviour does not match documentation · Issue #100117 · python/cpython
code.co_positions behaviour does not match documentation #100117Labels 3.11only security fixesonly security fixes3.12only security fixesonly security fixes3.13only security fixesonly security fixesdocsDocumentation in the Doc dirDocumentation in the Doc dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error DescriptionThe 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
Activityiritkatriel commented on Dec 8, 2022 changed the title [-]error documentation of code.co_positions[/-] [+]error in documentation of code.co_positions[/+] on Dec 8, 2022changed the title [-]error in documentation of code.co_positions[/-] [+]code.co_positions behaviour does not match documentation[/+] on Dec 12, 2022brandtbucher commented on Dec 21, 2022 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. iritkatriel commented on Dec 22, 2022 We discussed this at the meeting and I think the view was that the implementation needs to change. @markshannon ? markshannon commented on Dec 22, 2022 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. iritkatriel commented on Dec 11, 2023 This shouldn't have been closed, the doc is still incorrect. added a commit that references this issue on May 31, 2024gh-100117: Fix inaccuracy in documentation of the CodeObject's co_pos…
MetadataMetadata3.11only security fixesonly security fixes3.12only security fixesonly security fixes3.13only security fixesonly security fixesdocsDocumentation in the Doc dirDocumentation in the Doc dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error DevelopmentNo branches or pull requestsIssue actions
|