From 8991758af2f213f9589f735513ba06e964e4857c Mon Sep 17 00:00:00 2001 From: Joey Vagedes Date: Mon, 10 Jul 2023 16:08:58 -0700 Subject: [PATCH] SetupDataPkg: Remove bare excepts Catching a bare exception is considered bad practice. Catching bare exceptions can make it impossible to interrupt the program (e.g., with Ctrl-C) and can disguise other problems. This removes all bare excepts and instead catches `Exception` which is the base class for all code-generated exceptions. --- SetupDataPkg/Tools/ConfigEditor.py | 14 ++++++------- .../Tools/SettingSupport/CertSupportLib.py | 2 +- .../Tools/SettingSupport/DFCI_SupportLib.py | 20 +++++++++---------- .../SettingSupport/UefiVariablesSupportLib.py | 2 +- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/SetupDataPkg/Tools/ConfigEditor.py b/SetupDataPkg/Tools/ConfigEditor.py index f459bf8a..cdb7b470 100644 --- a/SetupDataPkg/Tools/ConfigEditor.py +++ b/SetupDataPkg/Tools/ConfigEditor.py @@ -142,7 +142,7 @@ def validate(self, value): if len(value) > 0: try: int(value, 16) - except: + except Exception: return None # Normalize the cell format @@ -634,7 +634,7 @@ def edit_num_finished(self, event): if value < min_val or value > max_val: raise Exception("Invalid input!") self.set_config_item_value(item, text, file_id) - except: + except Exception: pass text = item["value"].strip("{").strip("}").strip() @@ -1030,7 +1030,7 @@ def set_config_item_value(self, item, value_str, file_id): new_value = self.cfg_data_list[file_id].cfg_data_obj.reformat_value_str( value_str, self.cfg_data_list[file_id].cfg_data_obj.get_cfg_item_length(item), item=item ) - except: + except Exception: print("WARNING: Failed to format knob value string '%s' for '%s' !" % (value_str, item['path'])) new_value = item['value'] else: @@ -1040,7 +1040,7 @@ def set_config_item_value(self, item, value_str, file_id): self.cfg_data_list[file_id].cfg_data_obj.get_cfg_item_length(item), item["value"], ) - except: + except Exception: print( "WARNING: Failed to format value string '%s' for '%s' !" % (value_str, item["path"]) @@ -1104,7 +1104,7 @@ def update_config_data_from_widget(self, widget, args): def evaluate_condition(self, item, file_id): try: result = self.cfg_data_list[file_id].cfg_data_obj.evaluate_condition(item) - except: + except Exception: print( "WARNING: Condition '%s' is invalid for '%s' !" % (item["condition"], item["path"]) @@ -1134,7 +1134,7 @@ def add_config_item(self, item, row, file_id): option_value = self.cfg_data_list[file_id].cfg_data_obj.get_value( option_str, len(option_str), False ) - except: + except Exception: option_value = 0 print( 'WARNING: Option "%s" has invalid format for "%s" !' @@ -1208,7 +1208,7 @@ def add_config_item(self, item, row, file_id): option_value = self.cfg_data_list[file_id].cfg_data_obj.get_value( item, len(option_str), False, option_str ) - except: + except Exception: option_value = 0 print('WARNING: Option "%s" has invalid format for "%s" !' % (option_str, item['path'])) if option_value == current_value: diff --git a/SetupDataPkg/Tools/SettingSupport/CertSupportLib.py b/SetupDataPkg/Tools/SettingSupport/CertSupportLib.py index ab7d7c4e..81a1001c 100644 --- a/SetupDataPkg/Tools/SettingSupport/CertSupportLib.py +++ b/SetupDataPkg/Tools/SettingSupport/CertSupportLib.py @@ -69,7 +69,7 @@ def get_thumbprint_from_pfx(self, pfxfilename=None): if (len(thumbprint) != 40) or (found is False): return "No thumbprint" - except: + except Exception: traceback.print_exc() return "Unable to read certificate" diff --git a/SetupDataPkg/Tools/SettingSupport/DFCI_SupportLib.py b/SetupDataPkg/Tools/SettingSupport/DFCI_SupportLib.py index a9972ad1..c4715881 100644 --- a/SetupDataPkg/Tools/SettingSupport/DFCI_SupportLib.py +++ b/SetupDataPkg/Tools/SettingSupport/DFCI_SupportLib.py @@ -255,12 +255,12 @@ def get_current_permission_defaults(self, resultfile): # Collect the root attributes try: r1 = root.attrib["Default"] - except: + except Exception: r1 = None try: r2 = root.attrib["Delegated"] - except: + except Exception: r2 = None print("Result Default Values for : Default=%s, Delegated=%s" % (r1, r2)) @@ -412,7 +412,7 @@ def extract_payload_from_current(self, resultfile, payloadfile): try: tree = ET.parse(resultfile) elem = tree.find("./SyncBody/Results/Item/Data") - except: + except Exception: elem = None if elem is None: @@ -429,7 +429,7 @@ def extract_results_packet(self, resultfile, resultpktfile): try: tree = ET.parse(resultfile) elem = tree.find("./SyncBody/Results/Item/Data") - except: + except Exception: elem = None if elem is None: @@ -495,7 +495,7 @@ def get_status_and_sessionid_from_settings_results(self, resultfile, checktype): "Setting %s - Code %s" % (elem.find("Id").text, elem.find("Result").text) ) - except: + except Exception: traceback.print_exc() return RsltRc, rslt.SessionId @@ -541,7 +541,7 @@ def get_status_from_dmtools_results(self, resultsfile): result = 0x8000000000000007 # EFI_DEVICE_ERROR break result = 0 - except: + except Exception: traceback.print_exc() return result @@ -566,7 +566,7 @@ def print_xml_payload(self, XmlFileName): try: tree = xml.dom.minidom.parse(XmlFileName) print("%s" % tree.toprettyxml()) - except: + except Exception: traceback.print_exc() print("Unable to print settings XML") @@ -614,7 +614,7 @@ def get_device_ids(self, XmlFileName): for key in d: print(" Key {0} has the value of {1}".format(key, d[key])) - except: + except Exception: traceback.print_exc() print("Unable to extract DeviceIdElements.") d = {} @@ -653,7 +653,7 @@ def get_dfci_version(self, XmlFileName): d = elem.text print("Dfci Version detected as %s" % d) - except: + except Exception: traceback.print_exc() print("Unable to extract DfciVersion from %s" % XmlFileName) d = None @@ -686,7 +686,7 @@ def get_thumbprints(self, XmlFileName): for key in d: print(" Key {0} has the value of {1}".format(key, d[key])) - except: + except Exception: traceback.print_exc() print("Unable to extract DeviceIdElements.") d = {} diff --git a/SetupDataPkg/Tools/SettingSupport/UefiVariablesSupportLib.py b/SetupDataPkg/Tools/SettingSupport/UefiVariablesSupportLib.py index 86400b1e..3db0dc82 100644 --- a/SetupDataPkg/Tools/SettingSupport/UefiVariablesSupportLib.py +++ b/SetupDataPkg/Tools/SettingSupport/UefiVariablesSupportLib.py @@ -84,7 +84,7 @@ def __init__(self): c_int, c_int, ] - except: + except Exception: logging.warn( "G[S]etFirmwareEnvironmentVariableW function doesn't seem to exist" )