try:
expression block
expect Exception if condition else ():
expression block
That's an interesting idea I haven't seen before, but it doesn't work if the condition you want to test depends on the exception object, which in my experience it often does.
Some of OP's examples are of that form: for instance, in setuptools/sandbox.py:
try:
...
except SystemExit as v:
if v.args and v.args[0]:
raise
# Normal exit, just return
In pip/_internal/utils/misc.py:
try:
os.makedirs(path)
except OSError as e:
# Windows can raise spurious ENOTEMPTY errors. See #6426.
if e.errno != errno.EEXIST and e.errno != errno.ENOTEMPTY:
raise
I suppose the right way to implement it would be to support PEP 622 patterns in except clauses.
except case SystemExit(args=args) if not args or not args[0]:
pass # Normal exit, just return
except case OSError(errno = errno.EEXIST | errno.ENOTEMPTY):
# Windows can raise spurious ENOTEMPTY errors. See #6426.
pass