DAGDA SEARCH | Back to results | Support

Message 386123 - Python tracker

Source: https://bugs.python.org/msg386123

homepage

Message386123 ➜

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Mark.Shannon
Recipients Mark.Shannon, Mohamed_Atef, ethan.furman, gregory.p.smith, gvanrossum, josh.r, pablogsal, serhiy.storchaka, stestagg
Date 2021-02-02.00:03:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1612224203.12.0.529573757396.issue42899@roundup.psfhosted.org>
In-reply-to
Content
The differences between allowing the optimiser to remove truly redundant (in terms of control flow) boolean tests, or not, is slight.
It only matters for __bool__() calls that have side effects. Like __hash__(), __bool__() should not produce side effects.


There are three options, each with their own issues.

1. Freely remove redundant boolean checks, which means that

b = B()
try:
    if b: pass
except:
    print("This won't happen, but it used to")

will not print anything.

2. Don't remove redundant boolean checks, which prevents optimizations to remove repeated checks in short-circuiting operators, and would be a regression in terms of efficiency.

3. Attempt some sort of complex compromise. This is hard to get right, especially once the optimizer has started to transform the code.
It is also likely to produce inconsistencies, in that `True or b` might behave differently from `b or True`. Both are true, but one might raise an exception, whereas the other might not.


I favour option 1. It is already implemented. It is consistent both with itself, and with optimizations up to 3.9.
PEP 626 means that it will generally only be applied to short-circuiting operators and will not eliminate entire lines.


Note:

b = B()
try:
    if b: 
         pass
except:
    print("This will still happen")

Will print, as PEP 626 requires the execution of the pass statement to be observable, as it is on a line by itself. Consequently the test must be performed.

Given that this is quite obscure, I don't think it is a problem to release an alpha with this unresolved.
I would like to resolve it before beta.
History
Date User Action Args
2021-02-02 00:03:23Mark.Shannonsetrecipients: + Mark.Shannon, gvanrossum, gregory.p.smith, ethan.furman, serhiy.storchaka, josh.r, Mohamed_Atef, pablogsal, stestagg
2021-02-02 00:03:23Mark.Shannonsetmessageid: <1612224203.12.0.529573757396.issue42899@roundup.psfhosted.org>
2021-02-02 00:03:23Mark.Shannonlinkissue42899 messages
2021-02-02 00:03:22Mark.Shannoncreate
Supported by The Python Software Foundation,
Powered by Roundup Copyright © 1990-2022, Python Software Foundation
Legal Statements