[python] take care to this detail of @contextmanager: if an unhandled exception occurs in the block, it is reraised inside the generator at the point where the yield occurred stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 01 Apr 2010 08:24:53 +0200
branchstable
changeset 5128 e5d300d75519
parent 5127 3c2dda44e2f6
child 5129 839aadaf42d3
[python] take care to this detail of @contextmanager: if an unhandled exception occurs in the block, it is reraised inside the generator at the point where the yield occurred
devtools/testlib.py
server/sources/native.py
--- a/devtools/testlib.py	Thu Apr 01 08:24:11 2010 +0200
+++ b/devtools/testlib.py	Thu Apr 01 08:24:53 2010 +0200
@@ -360,9 +360,11 @@
         self.vreg._loadedmods.setdefault(self.__module__, {})
         for obj in appobjects:
             self.vreg.register(obj)
-        yield
-        for obj in appobjects:
-            self.vreg.unregister(obj)
+        try:
+            yield
+        finally:
+            for obj in appobjects:
+                self.vreg.unregister(obj)
 
     # vregistry inspection utilities ###########################################
 
--- a/server/sources/native.py	Thu Apr 01 08:24:11 2010 +0200
+++ b/server/sources/native.py	Thu Apr 01 08:24:53 2010 +0200
@@ -482,10 +482,12 @@
             except AttributeError:
                 assert event == 'deleted'
                 getattr(storage, 'entity_deleted')(entity, attr)
-        yield # 2/ execute the source's instructions
-        # 3/ restore original values
-        for attr, value in orig_values.items():
-            entity[attr] = value
+        try:
+            yield # 2/ execute the source's instructions
+        finally:
+            # 3/ restore original values
+            for attr, value in orig_values.items():
+                entity[attr] = value
 
     def add_entity(self, session, entity):
         """add a new entity to the source"""