Files
pplus-web/mojo/stress/context-manager.mojo
T
2026-10-05 03:03:38 +00:00

33 lines
940 B
Mojo

# probe: with over a struct with __enter__ and __exit__, normal and failing
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: begin load
# expect-stdout: loading
# expect-stdout: end load
# expect-stdout: begin parse
# expect-stdout: inside parse
# expect-stdout: end parse after error: bad token
# expect-stdout: done
@fieldwise_init
struct Section(Copyable, Movable):
var name: String
def __enter__(self) -> Self:
print("begin", self.name)
return self.copy()
def __exit__(self):
print("end", self.name)
def __exit__(self, err: Error) -> Bool:
print("end", self.name, "after error:", err)
return True
def main() raises:
with Section("load"):
print("loading")
with Section("parse") as s:
print("inside", s.name)
raise Error("bad token")
print("done")