mirror of
				https://github.com/ARM-software/devlib.git
				synced 2025-11-03 23:41:21 +00: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:
		
				
					committed by
					
						
						Marc Bonnici
					
				
			
			
				
	
			
			
			
						parent
						
							b7ab340d33
						
					
				
				
					commit
					fbf0875357
				
			@@ -114,10 +114,13 @@ def open_serial_connection(timeout, get_conn=False, init_dtr=None,
 | 
				
			|||||||
    """
 | 
					    """
 | 
				
			||||||
    target, conn = get_connection(timeout, init_dtr=init_dtr,
 | 
					    target, conn = get_connection(timeout, init_dtr=init_dtr,
 | 
				
			||||||
                                  logcls=logcls, *args, **kwargs)
 | 
					                                  logcls=logcls, *args, **kwargs)
 | 
				
			||||||
    if get_conn:
 | 
					 | 
				
			||||||
        yield target, conn
 | 
					 | 
				
			||||||
    else:
 | 
					 | 
				
			||||||
        yield target
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    target.close()  # Closes the file descriptor used by the conn.
 | 
					    if get_conn:
 | 
				
			||||||
    del 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.
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user