From 476cfbb2722bed4f0bbfccc28071c929565779dc Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Thu, 12 Oct 2023 15:46:18 +0100 Subject: [PATCH] simplify AbstractContextManager --- spinn_utilities/abstract_context_manager.py | 18 -------------- unittests/test_context_manager.py | 26 +++------------------ 2 files changed, 3 insertions(+), 41 deletions(-) diff --git a/spinn_utilities/abstract_context_manager.py b/spinn_utilities/abstract_context_manager.py index 3c4ac989..1c55f0e2 100644 --- a/spinn_utilities/abstract_context_manager.py +++ b/spinn_utilities/abstract_context_manager.py @@ -28,27 +28,9 @@ def close(self): How to actually close the underlying resources. """ - def _context_entered(self): - """ - Called when the context is entered. The result is ignored. - """ - - def _context_exception_occurred(self, exc_type, exc_val, exc_tb): - """ - Called when an exception occurs during the `with` context, *after* - the context has been closed. - - :param type exc_type: - :param object exc_val: - :param traceback exc_tb: - """ - def __enter__(self): - self._context_entered() return self def __exit__(self, exc_type, exc_val, exc_tb): self.close() - if exc_type: - self._context_exception_occurred(exc_type, exc_val, exc_tb) return False diff --git a/unittests/test_context_manager.py b/unittests/test_context_manager.py index db1a2a86..6712d517 100644 --- a/unittests/test_context_manager.py +++ b/unittests/test_context_manager.py @@ -17,16 +17,10 @@ class CM(AbstractContextManager): def __init__(self): - self.state = None - - def _context_entered(self): - self.state = 0 + self.state = "open" def close(self): - self.state += 1 - - def _context_exception_occurred(self, exc_type, exc_val, exc_tb): - self.state = str(exc_val) + self.state = "closed" class CMTestExn(Exception): @@ -40,18 +34,4 @@ def test_acm_with_success(): with cm: states.append(cm.state) states.append(cm.state) - assert states == [None, 0, 1] - - -def test_acm_with_exception(): - states = [] - cm = CM() - try: - states.append(cm.state) - with cm: - states.append(cm.state) - raise CMTestExn("boo") - except CMTestExn: - pass - states.append(cm.state) - assert states == [None, 0, "boo"] + assert states == ["open", "open", "closed"]