Skip to content

Commit

Permalink
Merge pull request #250 from SpiNNakerManchester/AbstractContextManager
Browse files Browse the repository at this point in the history
simplify AbstractContextManager
  • Loading branch information
Christian-B authored Oct 13, 2023
2 parents 065d4f1 + 476cfbb commit 60b1809
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 41 deletions.
18 changes: 0 additions & 18 deletions spinn_utilities/abstract_context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 3 additions & 23 deletions unittests/test_context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"]

0 comments on commit 60b1809

Please sign in to comment.