aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/ec/context.py
diff options
context:
space:
mode:
authorJ08nY2020-06-13 19:34:26 +0200
committerJ08nY2020-06-13 19:36:00 +0200
commit7e51c6546d369ec46a6ae8978147e79f2f0195a3 (patch)
treec34c825cbb7dfbeb167a75961e8d9c4f5ddba4d8 /pyecsca/ec/context.py
parentbec7fbe0996481bbebab80b18d781f408d96aae6 (diff)
downloadpyecsca-7e51c6546d369ec46a6ae8978147e79f2f0195a3.tar.gz
pyecsca-7e51c6546d369ec46a6ae8978147e79f2f0195a3.tar.zst
pyecsca-7e51c6546d369ec46a6ae8978147e79f2f0195a3.zip
Diffstat (limited to 'pyecsca/ec/context.py')
-rw-r--r--pyecsca/ec/context.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/pyecsca/ec/context.py b/pyecsca/ec/context.py
index d01f40e..6a267e6 100644
--- a/pyecsca/ec/context.py
+++ b/pyecsca/ec/context.py
@@ -26,6 +26,33 @@ class Action(object):
@public
+class ResultAction(Action):
+ """An action that has a result."""
+ _result: Any = None
+ _has_result: bool = False
+
+ @property
+ def result(self) -> Any:
+ if not self._has_result:
+ raise AttributeError("No result set")
+ return self._result
+
+ def exit(self, result: Any):
+ if not self.inside:
+ raise RuntimeError("Result set outside of action scope")
+ if self._has_result:
+ return
+ self._has_result = True
+ self._result = result
+ return result
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ if not self._has_result and exc_type is None and exc_val is None and exc_tb is None:
+ raise RuntimeError("Result unset on action exit")
+ super().__exit__(exc_type, exc_val, exc_tb)
+
+
+@public
class Tree(OrderedDict):
def get_by_key(self, path: List) -> Any: