-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
compile() with PyCF_ONLY_AST flag ignores the optimize arg #108113
Comments
I think we can use the optimize parameter for this. When the We can decide that when |
Currently:
With the suggested change:
|
Currently the design is that the So rather than deciding to call Presumably for |
Another thing to decide is whether we want this to work when the input is already an AST. Currently if |
Hmm, my leaning would be rather to stick with the current behavior (return input unchanged) if the input is already an AST and you request an AST. IMO it's too subtle for |
My concern is that in normal compilation we would end up calling |
Note that _PyAST_Compile takes its own version of the flags. |
Isn't this a problem regardless, for I think the main back-compat issue would be if there are C extensions currently calling Or another (IMO less clean, but perhaps also less invasive) option would be to call |
This is what I was thinking we'd do. |
I think that's reasonable, but in that case I see no reason to also limit it to |
…..., flags=ast.PyCF_ONLY_AST)
As discussed offline, the AST optimization performs const folding regardless of the value of |
Maybe this is another topic with this issue, Do you think that it's worth adding it too? WDYT? There might be another way to deal with this issue too. |
There is another way. I refactored the compiler to separate the codegen/opt/assemble stages, so we can pick and choose which ones to apply (not through compile but a new function). We already have unit tests that exercise the different stages. See #104240. |
Ah for debugging purposes, it will be enough. |
Ironically the solution we chose does not actually fix the issue as described in the issue title :) But I think it's OK, we now clearly present an alternative option that does respect the |
I think we should make ast.parse() optimize also when the input is an AST. At the moment there is no way to apply AST optimization to an AST that you created, so there is a gap. I'd expect these to give the same result:
|
I agree. When we were discussing respecting |
compile()
(orast.parse()
, which calls it) can return the ast for some source code, converted to a Python object version of the AST. But this AST is not optimized (with_PyAST_Optimize
).Static Python re-implemented
_PyAST_Optimize
for this reason. If we expose a way for them to get an optimized AST, they won't have to.For performance, it would be better not to call another API function on a Python AST, but to add an API option that runs
_PyAST_Optimize
before converting the AST to Python.Linked PRs
The text was updated successfully, but these errors were encountered: