From 7c8bed172baac20997d1092a5f853980fca725f3 Mon Sep 17 00:00:00 2001 From: Vincent Chiang <47546216+vincentchiang-ec@users.noreply.github.com> Date: Sat, 11 Jul 2020 09:23:27 +0800 Subject: [PATCH] [eeprom] Fix UnboundLocalError (#93) Fix the error, "UnboundLocalError: local variable 'F' referenced before assignment" --- sonic_platform_base/sonic_eeprom/eeprom_base.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/sonic_platform_base/sonic_eeprom/eeprom_base.py b/sonic_platform_base/sonic_eeprom/eeprom_base.py index a82837b7a271..31bc0bb5175f 100644 --- a/sonic_platform_base/sonic_eeprom/eeprom_base.py +++ b/sonic_platform_base/sonic_eeprom/eeprom_base.py @@ -41,13 +41,15 @@ def __init__(self, path, format, start, status, readonly): def check_status(self): if self.u != '': + F = None try: F = open(self.u, "r") d = F.readline().rstrip() except IOError as e: raise IOError("Failed to check status : %s" % (str(e))) finally: - F.close() + if F is not None: + F.close() return d else: return 'ok' @@ -236,6 +238,7 @@ def read_eeprom(self): return o def read_eeprom_bytes(self, byteCount, offset=0): + F = None try: F = self.open_eeprom() F.seek(self.s + offset) @@ -259,7 +262,8 @@ def read_eeprom_bytes(self, byteCount, offset=0): except IOError as e: raise IOError("Failed to read eeprom : %s" % (str(e))) finally: - F.close() + if F is not None: + F.close() return o @@ -267,6 +271,7 @@ def read_eeprom_db(self): return 0 def write_eeprom(self, e): + F = None try: F = open(self.p, "wb") F.seek(self.s) @@ -274,12 +279,14 @@ def write_eeprom(self, e): except IOError as e: raise IOError("Failed to write eeprom : %s" % (str(e))) finally: - F.close() + if F is not None: + F.close() self.write_cache(e) def write_cache(self, e): if self.cache_name: + F = None try: F = open(self.cache_name, "wb") F.seek(self.s) @@ -287,7 +294,8 @@ def write_cache(self, e): except IOError as e: raise IOError("Failed to write cache : %s" % (str(e))) finally: - F.close() + if F is not None: + F.close() def update_cache(self, e): if self.cache_update_needed: