From b10dac0390041bb921927f87a7f757eb205fa697 Mon Sep 17 00:00:00 2001 From: Ying Xie Date: Tue, 4 Dec 2018 00:17:21 +0000 Subject: [PATCH 1/2] [sfp_util] open eeprom files with buffering disabled Buffering will cause read to read more than requested. For slow files like SFP EEPROM, we want to read exactly what we asked for. Signed-off-by: Ying Xie --- sonic_sfp/sfputilbase.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sonic_sfp/sfputilbase.py b/sonic_sfp/sfputilbase.py index 498706ab9e25..ce774a317a53 100644 --- a/sonic_sfp/sfputilbase.py +++ b/sonic_sfp/sfputilbase.py @@ -198,7 +198,7 @@ def _sfp_eeprom_present(self, sysfs_sfp_i2c_client_eeprompath, offset): return False else: try: - sysfsfile = open(sysfs_sfp_i2c_client_eeprompath, "rb") + sysfsfile = open(sysfs_sfp_i2c_client_eeprompath, mode="rb", buffering=0) sysfsfile.seek(offset) sysfsfile.read(1) except IOError: @@ -275,7 +275,7 @@ def _read_eeprom_devid(self, port_num, devid, offset, num_bytes = 256): return None try: - sysfsfile_eeprom = open(sysfs_sfp_i2c_client_eeprom_path, "rb") + sysfsfile_eeprom = open(sysfs_sfp_i2c_client_eeprom_path, mode="rb", buffering=0) except IOError: print("Error: reading sysfs file %s" % sysfs_sfp_i2c_client_eeprom_path) return None @@ -646,7 +646,7 @@ def get_transceiver_info_dict(self, port_num): return None try: - sysfsfile_eeprom = open(file_path, "rb") + sysfsfile_eeprom = open(file_path, mode="rb", buffering=0) except IOError: print("Error: reading sysfs file %s" % file_path) return None @@ -711,7 +711,7 @@ def get_transceiver_dom_info_dict(self, port_num): return None try: - sysfsfile_eeprom = open(file_path, "rb") + sysfsfile_eeprom = open(file_path, mode="rb", buffering=0) except IOError: print("Error: reading sysfs file %s" % file_path) return None @@ -807,7 +807,7 @@ def get_transceiver_dom_info_dict(self, port_num): return None try: - sysfsfile_eeprom = open(file_path, "rb") + sysfsfile_eeprom = open(file_path, mode="rb", buffering=0) except IOError: print("Error: reading sysfs file %s" % file_path) return None From c799a5eefcf1411cdd7b08889e9985841c2af10d Mon Sep 17 00:00:00 2001 From: Ying Xie Date: Tue, 4 Dec 2018 00:46:41 +0000 Subject: [PATCH 2/2] syntax sugar --- sonic_sfp/sfputilbase.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sonic_sfp/sfputilbase.py b/sonic_sfp/sfputilbase.py index ce774a317a53..78ca51942dfb 100644 --- a/sonic_sfp/sfputilbase.py +++ b/sonic_sfp/sfputilbase.py @@ -198,9 +198,9 @@ def _sfp_eeprom_present(self, sysfs_sfp_i2c_client_eeprompath, offset): return False else: try: - sysfsfile = open(sysfs_sfp_i2c_client_eeprompath, mode="rb", buffering=0) - sysfsfile.seek(offset) - sysfsfile.read(1) + with open(sysfs_sfp_i2c_client_eeprompath, mode="rb", buffering=0) as sysfsfile: + sysfsfile.seek(offset) + sysfsfile.read(1) except IOError: return False except: