Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion vm/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,10 @@ func do_WITH_CLEANUP(vm *Vm, arg int32) error {

wasErr := false
if exc != py.None {
wasErr = res == py.True
wasErr, err = py.ObjectIsTrue(res)
if err != nil {
return err
}
}
if wasErr {
/* There was an exception and a True return */
Expand Down
44 changes: 44 additions & 0 deletions vm/tests/with.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,50 @@ def __exit__(self, type, value, traceback):
assert c.value is None
assert c.traceback is None

class ExitResult():
def __init__(self, result):
self.result = result
def __enter__(self):
pass
def __exit__(self, type, value, traceback):
return self.result

class Falsy():
def __bool__(self):
return False

class Empty():
def __len__(self):
return 0

class BadBool():
def __bool__(self):
raise ZeroDivisionError("bad bool")

doc="with silenced error: true __exit__ result"
for result in (1, "potato", [0], 0.5, object()):
with ExitResult(result):
raise ValueError("potato")

doc="with error: false __exit__ result"
for result in (None, False, 0, "", [], 0.0, Falsy(), Empty()):
ok = False
try:
with ExitResult(result):
raise ValueError("potato")
except ValueError:
ok = True
assert ok, "ValueError not raised"

doc="with error: __bool__ of __exit__ result raises"
ok = False
try:
with ExitResult(BadBool()):
raise ValueError("potato")
except ZeroDivisionError:
ok = True
assert ok, "ZeroDivisionError not raised"

doc="with in loop: break"
c = Context()
assert c.state == "__init__"
Expand Down
Loading