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.

classification
Title: Undefined behavior for syntax "except AError or BError:" accepted by interpreter
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.9, Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: kftse20031207, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2021-08-31 11:31 by kftse20031207, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg400710 - (view) Author: kftse (kftse20031207) Date: 2021-08-31 11:31
Test case:

try:
    raise TypeError()
except TypeError or ValueError:
    print("OK")
try:
    raise ValueError()
except TypeError or ValueError:
    print("OK")

Output:
(Python 3.9.0)
OK
OK
# seem to eventually lead to segmentation fault elsewhere
(Python 3.8.0)
OK
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    raise ValueError()
ValueError


I understand that this code is incorrect syntax for exception.

The awkward behavior is that the interpreter accepted this syntax and the output being correct in some case, or even in both cases, but seem to eventually lead to segmentation fault elsewhere.
msg400718 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-08-31 12:04
I get the correct result (a ValueError traceback) in 3.9.6, I get a crash in 3.9.0. It was a bug in 3.9.0 which is now fixed.

The syntax is legal.
msg400723 - (view) Author: kftse (kftse20031207) Date: 2021-08-31 12:20
Tested 3.9.6 to have same behavior as 3.8.0.

to clarify, I suppose legal merely means syntactically correct, not

effect of "except AError or BError:" === "except (AError, BError)"

right?
msg400816 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-09-01 07:01
Yes, it is syntactically correct. It can even make sense. But the meaning is different from "except (AError, BError)". If it looks confusing to you, it is a work of linters to warn about misleading or suspicions code.
msg401026 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-09-04 01:03
"except a or b:" should be same as "except (a or b):" which should be same as "except a:", which is current behavior in 3.10.0, etc.
History
Date User Action Args
2022-04-11 14:59:49adminsetgithub: 89221
2021-09-04 01:03:54terry.reedysetstatus: open -> closed

nosy: + terry.reedy
messages: + msg401026

resolution: not a bug
stage: resolved
2021-09-01 07:01:16serhiy.storchakasetmessages: + msg400816
2021-08-31 12:20:26kftse20031207setmessages: + msg400723
2021-08-31 12:04:55serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg400718
2021-08-31 11:31:30kftse20031207create