Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code.co_positions behaviour does not match documentation #100117

Closed
iritkatriel opened this issue Dec 8, 2022 · 5 comments · Fixed by #119364
Closed

code.co_positions behaviour does not match documentation #100117

iritkatriel opened this issue Dec 8, 2022 · 5 comments · Fixed by #119364
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@iritkatriel
Copy link
Member

iritkatriel commented Dec 8, 2022

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

@iritkatriel iritkatriel added type-bug An unexpected behavior, bug, or error docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.11 only security fixes 3.12 bugs and security fixes labels Dec 8, 2022
@iritkatriel
Copy link
Member Author

CC @markshannon @brandtbucher .

@iritkatriel iritkatriel changed the title error documentation of code.co_positions error in documentation of code.co_positions Dec 8, 2022
@iritkatriel iritkatriel changed the title error in documentation of code.co_positions code.co_positions behaviour does not match documentation Dec 12, 2022
@brandtbucher
Copy link
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.

@iritkatriel
Copy link
Member Author

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

@markshannon
Copy link
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.

@iritkatriel
Copy link
Member Author

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

@iritkatriel iritkatriel added the 3.13 bugs and security fixes label Dec 11, 2023
iritkatriel added a commit to iritkatriel/cpython that referenced this issue May 21, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 31, 2024
…co_positions field. (pythonGH-119364)

(cherry picked from commit 015b1fd)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 31, 2024
…co_positions field. (pythonGH-119364)

(cherry picked from commit 015b1fd)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 31, 2024
…co_positions field. (pythonGH-119364)

(cherry picked from commit 015b1fd)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
iritkatriel added a commit that referenced this issue May 31, 2024
… co_positions field. (GH-119364) (#119870)

gh-100117: Fix inaccuracy in documentation of the CodeObject's co_positions field. (GH-119364)
(cherry picked from commit 015b1fd)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
iritkatriel added a commit that referenced this issue May 31, 2024
… co_positions field. (GH-119364) (#119869)

gh-100117: Fix inaccuracy in documentation of the CodeObject's co_positions field. (GH-119364)
(cherry picked from commit 015b1fd)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
ambv pushed a commit that referenced this issue Sep 4, 2024
… co_positions field. (GH-119364) (#119871)

(cherry picked from commit 015b1fd)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
3 participants