Re:Python 3.11: “Zero cost” exception handling
by albertzeyer on Wednesday October 06, 2021 @02:55PM
(#28776833)
But that is what I meant, this StopIteration would not be affected by the zero cost optimization. The zero cost optimization is about compiled Python bytecode and the VM interpreter. The StopIteration is handled internally by CPython (not via Python bytecode).
For example, consider this function:
def foo():
for i in range(3):
print(i)
There is no try-except in this code, and neither in its bytecode. So this is not affected by the zero cost optimization from the linked issue.
The iteration logic is inside the `FOR_ITER` op. And that is purely handled inside CPython.
Also, this is again what I meant: In user code, you rarely would use custom exceptions for control flow. Please show me any example where you have seen otherwise.
So, again, what I wrote: I'm very confident that for most user code, the code path under `except ...:` is only rarely executed. Please show me any big Python project where this would not be the case. I doubt that there is any.
For example, consider this function:
There is no try-except in this code, and neither in its bytecode. So this is not affected by the zero cost optimization from the linked issue.The bytecode is this:
The iteration logic is inside the `FOR_ITER` op. And that is purely handled inside CPython.Also, this is again what I meant: In user code, you rarely would use custom exceptions for control flow. Please show me any example where you have seen otherwise.
So, again, what I wrote: I'm very confident that for most user code, the code path under `except ...:` is only rarely executed. Please show me any big Python project where this would not be the case. I doubt that there is any.
Also, btw, on EAFP, Guido van Rossum disagrees: https://mail.python.org/pipermail/python-dev/2014-March/1331...