Skip to content

Commit 85c427b

Browse files
authored
Reset exception in WithCleanupFinish (RustPython#5203)
Context managers have an `__exit__` function that returns a boolean-like object. If the object is truthy, then exceptions are suppressed. If an exception was thrown while resolving that boolean, it would leak and live on in the error stack, getting tacked on to all future exceptions. This caused several mysterious test failures which would only trigger after this very specific event was tested in `test_with`. The solution is to move a call to `vm.set_exception()` before attempting the `try_to_bool()` which threw the error. Minimal example to reproduce the bug: ```py import sys import traceback class cm(object): def __init__(self): pass def __enter__(self): return 3 def __exit__(self, a, b, c): class Bool: def __bool__(self): 1 // 0 return Bool() try: with cm(): raise Exception("Should NOT see this") except ZeroDivisionError: print("exception caught, as expected") print("There should now be no exception") traceback.print_exc() print(sys.exc_info()) ```
1 parent 5ee5531 commit 85c427b

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

‎vm/src/frame.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,10 +939,10 @@ impl ExecutingFrame<'_> {
939939
_ => self.fatal("WithCleanupFinish expects a FinallyHandler block on stack"),
940940
};
941941

942-
let suppress_exception = self.pop_value().try_to_bool(vm)?;
943-
944942
vm.set_exception(prev_exc);
945943

944+
let suppress_exception = self.pop_value().try_to_bool(vm)?;
945+
946946
if suppress_exception {
947947
Ok(None)
948948
} else if let Some(reason) = reason {

0 commit comments

Comments
 (0)