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

gh-119786: improve internal docs on co_linetable #123198

Merged

Conversation

picnixz
Copy link
Member

@picnixz picnixz commented Aug 21, 2024

In #123168, I needed to play with artificial instructions where some positions are not available. It's trivial to have full positions information or no positions information, but it's hard to create co_linetable values where only some instructions have positions.

This PR aims to improve the internal docs so that we can easily remember what to do (and how to do it with an example).

@picnixz picnixz added docs Documentation in the Doc dir skip issue skip news interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Aug 21, 2024
@picnixz picnixz requested a review from iritkatriel August 21, 2024 10:19
InternalDocs/locations.md Outdated Show resolved Hide resolved
InternalDocs/locations.md Outdated Show resolved Hide resolved
InternalDocs/locations.md Outdated Show resolved Hide resolved
Copy link
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up until the section "Artificial constructions" this seems fine.

I don't think we want to be encouraging anyone to create position tables.

The reason for explaining the layout in detail is for anyone maintaining the code.
Even for out of process tools, like pyspy, we provide C functions for them to use, so they don't need to parse the tables.

To create tables for testing dis and such, I'd recommend parsing Python code, modifying the locations on the AST, then generating the code object with compile.
Like this:

import ast
import dis

code = """
def foo():
    a = 1
    b = 2
    c = 3
"""

tree = ast.parse(code)
func = tree.body[0]
b = func.body[1].targets[0]
b.lineno = 5
b.col_offset = 17
b.end_lineno = 6
b.end_col_offset = 18
foo = compile(tree,"test", "exec")
dis.dis(foo.co_consts[0], show_positions=True)
2:0-2:0              RESUME                   0

3:8-3:9              LOAD_CONST               1 (1)
3:4-3:5              STORE_FAST               0 (a)

4:8-4:9              LOAD_CONST               2 (2)

5:17-6:18            STORE_FAST               1 (b)
5:8-5:9              LOAD_CONST               3 (3)
5:4-5:5              STORE_FAST               2 (c)
5:4-5:5              RETURN_CONST             0 (None)

@picnixz
Copy link
Member Author

picnixz commented Aug 21, 2024

To create tables for testing dis and such, I'd recommend parsing Python code, modifying the locations on the AST, then generating the code object with compile.

Oh, that's what I was searching for in #123168. Should I amend this PR and use your approach instead?

@iritkatriel
Copy link
Member

Yes, make a new PR though as that one was merged.

@picnixz
Copy link
Member Author

picnixz commented Aug 21, 2024

Actually, modifying the AST does not help because I can't remove some attributes that are expected to exist on the nodes. In particular, I cannot cancel positions (but I can easily change the values of the positions). So I think we need to stick with the co_linetable approach (unless there is something I'm missing?)

@markshannon
Copy link
Member

I see. You can't set an expression to have no location, because the compiler will just give it one.
FTR, you could test the no columns case by setting node.end_lineno = node.end_col_offset = -1, and leaving the line number.

I think the test you wrote is fine. There is need to rewrite it.

@picnixz
Copy link
Member Author

picnixz commented Aug 22, 2024

no columns case by setting node.end_lineno = node.end_col_offset = -1,

Yes, that's what I saw. But I'm not sure whether this is actually a bug or a feature of the compiler (namely, whether the -1 is really meant to indicate "no location").

@picnixz
Copy link
Member Author

picnixz commented Aug 22, 2024

So I managed to use AST tricks only though I'm not really sure that setting the columns to -1 means that we are deleting the information. I'll remove the artificial constructions in these docs anyway.

@picnixz picnixz changed the title Improve internal docs on co_linetable gh-119786: improve internal docs on co_linetable Aug 24, 2024
@picnixz picnixz requested a review from iritkatriel September 28, 2024 14:23
@picnixz
Copy link
Member Author

picnixz commented Nov 27, 2024

Friendly ping @iritkatriel (I'll fix the merge conflicts once I'm back, sorry missed that one)

@iritkatriel
Copy link
Member

There's a merge conflict now.

@picnixz
Copy link
Member Author

picnixz commented Nov 27, 2024

Since I'm on mobile I cannot resolve it but I'll take care of it on Friday (or maybe you can if you have time). Sorry for bothering you!

@picnixz
Copy link
Member Author

picnixz commented Nov 29, 2024

@iritkatriel Ok, actually the locations.md file was gone! So I fixed the conflicts. By the way, I put everything under the "Source code locations" section (semantically speaking I think the way we encode integers, the meaning of locations and the short forms are all part of this topic).

@picnixz picnixz requested a review from iritkatriel November 29, 2024 13:31
@iritkatriel iritkatriel merged commit 49f15d8 into python:main Nov 30, 2024
23 checks passed
@iritkatriel
Copy link
Member

Thank you!

@picnixz picnixz deleted the improve-internal-docs-on-co-linetable branch November 30, 2024 00:40
picnixz added a commit to picnixz/cpython that referenced this pull request Dec 2, 2024
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Jan 8, 2025
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) skip news
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants