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)]
>>>
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:
Linked PRs
co_linesmore efficient #100447