Skip to content

Instantly share code, notes, and snippets.

@eliasdorneles
Created February 4, 2017 11:43
Show Gist options
  • Save eliasdorneles/6a4420c4fa2746569378b15820573297 to your computer and use it in GitHub Desktop.
Save eliasdorneles/6a4420c4fa2746569378b15820573297 to your computer and use it in GitHub Desktop.
Apply the diff over the code of PR 351 pybee/VOC
class CtxMgr:
def __enter__(self):
print('entering CtxMgr')
return 42
def __exit__(self, *args):
print('exiting CtxMgr')
with CtxMgr() as val:
print('val', val)
val = 16
with CtxMgr():
print('in another ctx, val now is', val)
diff --git a/tests/structures/test_with.py b/tests/structures/test_with.py
index 88ed09d..f007e9d 100644
--- a/tests/structures/test_with.py
+++ b/tests/structures/test_with.py
@@ -19,18 +19,17 @@ class WithLoopTests(TranspileTestCase):
print('in another ctx, val now is', val)
""")
- # TODO: make this test pass
- # def test_with_body_fails(self):
- # self.assertCodeExecution("""
- # class CtxMgr:
- # def __enter__(self):
- # print('entering CtxMgr')
- # def __exit__(self, *args):
- # print('exiting CtxMgr')
- #
- # with CtxMgr():
- # raise ValueError
- # """)
+ def test_with_body_fails(self):
+ self.assertCodeExecution("""
+ class CtxMgr:
+ def __enter__(self):
+ print('entering CtxMgr')
+ def __exit__(self, *args):
+ print('exiting CtxMgr')
+
+ with CtxMgr():
+ raise ValueError
+ """)
def test_with_noexit(self):
self.assertCodeExecution("""
diff --git a/voc/python/ast.py b/voc/python/ast.py
index 2fe3580..ea51b5a 100644
--- a/voc/python/ast.py
+++ b/voc/python/ast.py
@@ -627,9 +627,15 @@ class Visitor(ast.NodeVisitor):
self.context.add_opcodes(JavaOpcodes.POP())
# TODO: use TRY helper and put exception information in the array passed to __exit__
+ self.context.add_opcodes(
+ TRY(),
+ )
for child in node.body:
self.visit(child)
+ self.context.add_opcodes(
+ FINALLY(),
+ )
for _ in node.items:
# pop __exit__ from stack and invoke it
self.context.add_opcodes(
@@ -637,6 +643,9 @@ class Visitor(ast.NodeVisitor):
JavaOpcodes.ACONST_NULL(),
python.Callable.invoke(),
)
+ self.context.add_opcodes(
+ END_TRY(),
+ )
@node_visitor
def visit_Raise(self, node):
@eliasdorneles
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment