Skip to content

Commit

Permalink
py/vm: Don't emit warning when using "raise ... from None".
Browse files Browse the repository at this point in the history
"Raise SomeException() from None" is a common Python idiom to suppress
chained exceptions and thus shouldn't trigger a warning on a version of
Python that doesn't support them in the first place.
  • Loading branch information
smurfix authored and dpgeorge committed Oct 8, 2023
1 parent 5232847 commit 3fb1bb1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 4 additions & 2 deletions py/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1171,8 +1171,10 @@ unwind_jump:;

ENTRY(MP_BC_RAISE_FROM): {
MARK_EXC_IP_SELECTIVE();
mp_warning(NULL, "exception chaining not supported");
sp--; // ignore (pop) "from" argument
mp_obj_t from_value = POP();
if (from_value != mp_const_none) {
mp_warning(NULL, "exception chaining not supported");
}
mp_obj_t obj = mp_make_raise_obj(TOP());
RAISE(obj);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/basics/exception_chain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Exception chaining is not supported, but check that basic
# exception works as expected.

try:
raise Exception from None
except Exception:
print("Caught Exception")

try:
try:
raise ValueError("Value")
except Exception as exc:
raise RuntimeError("Runtime") from exc
except Exception as ex2:
print("Caught Exception:", ex2)
3 changes: 2 additions & 1 deletion tests/basics/exception_chain.py.exp
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Warning: exception chaining not supported
Caught Exception
Warning: exception chaining not supported
Caught Exception: Runtime

0 comments on commit 3fb1bb1

Please sign in to comment.