-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
bpo-1875: Raise SyntaxError in invalid blocks that will be optimized away #13332
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
Conversation
770a939 to
9e8f83c
Compare
…away Move the check for dead conditionals (if 0) to the peephole optimizer and make sure that the code block is still compiled to report any existing syntax errors within.
|
I wrote similar code in my old fatoptimizer project (AST optimizer): Extracts: It would be nice to ensure that our optimizer doesn't optimize these examples. |
|
@vstinner Ok, I like to add these tests but let's open a different issue for those. Note that some of you examples are now legal after https://bugs.python.org/issue32489 |
|
When I wrote an AST optimizer, I implemented correctness checks to ensure that I don't remove code that would raise a SyntaxError: My implementation checks if a tree contains yield (for example) before trying to remove it. If it contains yield, global, etc.: the node is not removed. Maybe we should implement that in the new builtin AST optimizer, rather than in peephole.c which works at the bytecode level? |
|
Hum, my previous comment is unclear. I don't understand how your PR still removes "if 0: print(name)" but keeps "if 0: yield". Or it doesn't? |
It compiles the block (raising errors if any are found) and moves the removal optimization to the peephole optimizer. The only downside (if you consider any) is that the variables inside the dead code will still be considered when building the code stack size and some jumps may not be optimized. But this is a no-problem for three reasons:
|
vstinner
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
First I misunderstood the change. Once I understood it: yeah, it's correct to move an optimization from the compiler to the (peephole) optimizer. First, I didn't understood why a compiler directly implements an optimization.
|
Thanks @pablogsal for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7. |
…away (pythonGH-13332) Move the check for dead conditionals (if 0) to the peephole optimizer and make sure that the code block is still compiled to report any existing syntax errors within. (cherry picked from commit af8646c) Co-authored-by: Pablo Galindo <[email protected]>
|
GH-13382 is a backport of this pull request to the 3.7 branch. |
…away (GH-13332) Move the check for dead conditionals (if 0) to the peephole optimizer and make sure that the code block is still compiled to report any existing syntax errors within. (cherry picked from commit af8646c) Co-authored-by: Pablo Galindo <[email protected]>
https://bugs.python.org/issue1875