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
__debug__ is not optimized out at compile time for anything but if:
and while:
blocks
#71356
Comments
Issue bpo-22091 points out a quirk in the compile function and use of the debug "constant" causing inconsistent behavior when the optimize level of the compile call differs from that of the main interpreter; debug in an This behavior appears to be a consequence of debug being handled by a special case for the I'm not sure I understand the peephole optimizer, but if it can operate recursively (that is, an initial replacement of A->B where B could be optimized from B->C is optimized in a subsequent pass, turning all uses of A to C eventually), it seems like the "correct" solution would be to piggyback on optimizations for True and False, by having the peephole optimizer replace LOAD_NAME/LOAD_GLOBAL for debug with an appropriate LOAD_CONST, True or False, based on the compile environment. This would fix this bug (making debug evaluate in the This would fix the problem from bpo-22091 and also make __debug__ reliably useful for "optimization", since all uses of it would "compile out" to optimized runtime code, where right now only two cases do so. Does this seem reasonable? Note: I added the nosy list from bpo-22091 here, since that bug is really just a special case of this one for |
I think that the complete solution of this problem is making __debug__ a named constant like None, True, False. This needs changing the grammar of Python. |
That would also work. The argument I'd give in favor of performing a pass that replaces it with a literal True or False is that you don't have update as many places, don't have to worry about missing a place, and you don't have to decide if __debug__ is a reference to True or False, or a new object entirely. It's just too easy to miss a case where __debug__ should be special and not notice (because optimizations aren't heavily tested for specific byte code outputs or anything), where a missed optimization for the True or False constant is much less likely to go unnoticed. |
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: