Exception safe context manager
Without the try/finally the cleanup could be skipped by an exception being raised.
This commit is contained in:
parent
babf0255b9
commit
33597e3c7d
|
@ -34,7 +34,9 @@ def hostname_context(hostname: str):
|
||||||
back in the end of the caller scope. """
|
back in the end of the caller scope. """
|
||||||
hostname_current = gethostname()
|
hostname_current = gethostname()
|
||||||
hostname_process = run_command(['hostname', hostname])
|
hostname_process = run_command(['hostname', hostname])
|
||||||
|
try:
|
||||||
yield hostname_process
|
yield hostname_process
|
||||||
|
finally:
|
||||||
# Restoring the live session hostname.
|
# Restoring the live session hostname.
|
||||||
hostname_process = run_command(['hostname', hostname_current])
|
hostname_process = run_command(['hostname', hostname_current])
|
||||||
if hostname_process.returncode:
|
if hostname_process.returncode:
|
||||||
|
|
Loading…
Reference in New Issue