Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Commit

Permalink
Suppress exceptions correctly in with blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
abonie committed Jul 31, 2017
1 parent 89b9329 commit d4cef80
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion batavia/VirtualMachine.js
Expand Up @@ -1638,6 +1638,10 @@ VirtualMachine.prototype.byte_END_FINALLY = function() {
var exc_type = this.pop()
if (exc_type === builtins.None) {
why = null
} else if (exc_type === 'silenced') {
var block = this.pop_block() // should be except-handler
this.unwind_block(block)
return null
} else {
value = this.pop()
if (value instanceof builtins.BaseException.$pyclass) {
Expand Down Expand Up @@ -1748,7 +1752,7 @@ VirtualMachine.prototype.byte_WITH_CLEANUP = function() {
}
var ret = callables.call_function(exit_func, [exc, val, tb]) // XXX this arg list is incorrect for first 2 cases
//TODO: silence error if necessary
if (!exc instanceof types.NoneType && ret) { // XXX
if (!(exc instanceof types.NoneType) && ret) { // XXX
this.push('silenced')
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/structures/test_with.py
Expand Up @@ -42,3 +42,18 @@ def __exit__(self, exc, val, tb):
except Exception as e:
print(type(e), e)
""")

def test_suppress_exception(self):
self.assertCodeExecution("""
class mgr:
def __enter__(self):
pass
def __exit__(self, exc, val, tb):
print('supress')
return True
with mgr():
raise KeyError(42)
print('raised')
print('done')
""")

0 comments on commit d4cef80

Please sign in to comment.