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
Generator expression has wrong line/col info when inside a Call object #83416
Comments
A normal generator expression like (i for i in a) produces the following AST: Module( But when calling a function with a generator expression as an argument, something is off: Module( I'm not sure if this is intentional or not, because there is a call to copy_location in Python/ast.c:3149. If this call to copy_location is removed, the inconsistency goes away. |
I think that was introduced in https://bugs.python.org/issue31241 |
Do you think it is okay to just remove the call to copy_location? |
I think that because in that case the parenthesis is collapsed with function call parenthesis, the position of the AST node for the generator expression should point to the left parenthesis. But I would suggest asking Serhiy (I added him to the noisy list) as he was the author of the PR. |
OK, so I looked into this (sine we can't wait for Serhiy). The problem was probably introduced when Ivan added end lineno/column attributes to parse tree nodes. The offending copy_location() call copies the begin and end locations from the given node (maybegenbeg). But in the problematic case, maybegenbeg is just the left paren in the function call, not the node representing the surrounding () group. But there's a separate argument to ast_for_call(), closepar, that should be used for the end lineno/offset. Looking at the various calls to ast_for_call() in Python/ast.c, we find these:
So instead of calling copy_location() we should manually set the begin and end positions. An alternative would be to add a second node* argument to copy_location(). That would require us to modify all 5 calls to it, but it still feels the better way to go. I'll see if I can produce a PR for that. |
If somebody want to backport tests to 3.8, he must check manually all end positions. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: