Skip to content

Commit 9566430

Browse files
committed
🥅 Disallow user to call close on Run within context manager
1 parent e3cf626 commit 9566430

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

‎simvue/run.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ def __init__(
151151
self._timer: float = 0
152152
self._retention: float | None = None
153153

154+
# Keep track of if the Run class has been intialised
155+
# through a context manager
156+
self._context_manager_called: bool = False
157+
154158
self._testing: bool = False
155159
self._abort_on_alert: typing.Literal["run", "terminate", "ignore"] = "terminate"
156160
self._abort_callback: typing.Callable[[Self], None] | None = abort_callback
@@ -209,6 +213,7 @@ def __init__(
209213
self._emissions_monitor: CO2Monitor | None = None
210214

211215
def __enter__(self) -> Self:
216+
self._context_manager_called = True
212217
return self
213218

214219
def _handle_exception_throw(
@@ -1659,6 +1664,10 @@ def close(self) -> bool:
16591664
bool
16601665
whether close was successful
16611666
"""
1667+
if self._context_manager_called:
1668+
self._error("Cannot call close method in context manager.")
1669+
return
1670+
16621671
self._executor.wait_for_completion()
16631672

16641673
if not self._sv_obj:

0 commit comments

Comments
 (0)