bpo-37830: Optimize return statement bytecode with fixing segfaults#15247
bpo-37830: Optimize return statement bytecode with fixing segfaults#15247isidentical wants to merge 5 commits intopython:masterfrom
Conversation
pablogsal
left a comment
There was a problem hiding this comment.
Thanks, @isidentical for your PR.
Apart from the failures that you already see in the CI, I think this approach is not the correct one
as aside from other implementation issues is backwards-incompatible as the left side of the return statement is not executed anymore. For example:
def f():
print("Side effect")
def g():
for _ in range(3):
try:
return f()
finally:
break
g()In Python3 this prints "Side effect" while with this approach it does not print anything.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
Hey @pablogsal , |
Backwards compatibility is essential in CPython, so yes, we want backwards compatibility as there may be code out there that is using this (there are users even relying on compiler optimizations). Additionally, this approach fails with nested blocks: |
What about reverting #15230 and then adding allow continue with a new future flag called
Thanks, i'll look into them By the way, currently i am on a vacation (muslim holiday) so maybe i can't be very productive but i'm going to return in 2 days. |
I (personally) don't think to allow continue in finally is that important to start adding that complexity (especially future flags). Also, as Damien George indicates in the other PR, other Python implementations are having troubles with this for little gain. I think we need to think of other solutions that do not involve backward-incompatible changes or notable changes in the compiler state. Especially we should focus on solving the problem for 'break' cleanly at least for 3.8. |
|
This does not work because def simple(x):
for number in range(2):
try:
return number
finally:
if x:
continue
print(simple(0))
print(simple(1))It should print |
|
Hmm, i dont see a way to make this solution work with conditions so i believe it is best to close this too :/ |
I think this can help continue/break to stay inside finally with optimizing return bytecode, also merging #15230 .
https://bugs.python.org/issue37830