1
0
mirror of https://github.com/ARM-software/devlib.git synced 2024-10-06 02:40:50 +01:00

serial_port: Handle exception in open_serial_connection

Use try/finally clause in the contextmanager to always close the
connection if an exception is raised.

Also remove "del conn" since it only decrements the reference count,
which is done anyway when the generator function returns.
This commit is contained in:
Douglas RAILLARD 2018-11-27 17:20:47 +00:00 committed by Marc Bonnici
parent b7ab340d33
commit fbf0875357

View File

@ -114,10 +114,13 @@ def open_serial_connection(timeout, get_conn=False, init_dtr=None,
"""
target, conn = get_connection(timeout, init_dtr=init_dtr,
logcls=logcls, *args, **kwargs)
if get_conn:
yield target, conn
else:
yield target
target.close() # Closes the file descriptor used by the conn.
del conn
if get_conn:
target_and_conn = (target, conn)
else:
target_and_conn = target
try:
yield target_and_conn
finally:
target.close() # Closes the file descriptor used by the conn.