From d5cbc591db9af753651ce82523bdbc636409af97 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 18 Apr 2022 02:08:40 -0400 Subject: [PATCH] batch replace 'except:' with 'except Exception:' The latter will not catch `KeyboardInterrupt` and `SystemExit`. See https://stackoverflow.com/a/18982726/9567349. --- dpdata/fhi_aims/output.py | 4 ++-- dpdata/rdkit/sanitize.py | 4 ++-- dpdata/unit.py | 2 +- tests/test_pick_atom_idx.py | 2 +- tests/test_to_ase.py | 2 +- tests/test_to_pymatgen.py | 2 +- tests/test_to_pymatgen_entry.py | 2 +- tests/test_water_ions.py | 2 +- 8 files changed, 10 insertions(+), 10 deletions(-) diff --git a/dpdata/fhi_aims/output.py b/dpdata/fhi_aims/output.py index 99cb1203a..4ee819c3b 100755 --- a/dpdata/fhi_aims/output.py +++ b/dpdata/fhi_aims/output.py @@ -124,7 +124,7 @@ def analyze_block(lines, first_blk=False, md=True) : contents="\n".join(lines) try: natom=int(re.findall("Number of atoms.*([0-9]{1,})",lines)[0]) - except: + except Exception: natom=0 if first_blk: @@ -154,7 +154,7 @@ def analyze_block(lines, first_blk=False, md=True) : try: _eng_patt=re.compile(eng_patt) energy=float(_eng_patt.search(contents).group().split()[-2]) - except: + except Exception: energy=None if not energy: diff --git a/dpdata/rdkit/sanitize.py b/dpdata/rdkit/sanitize.py index 01f002e0f..0e1637402 100644 --- a/dpdata/rdkit/sanitize.py +++ b/dpdata/rdkit/sanitize.py @@ -22,7 +22,7 @@ def get_explicit_valence(atom, verbose=False): print( f"Explicit valence given by GetExplicitValence() and sum of bond order are inconsistent on {atom.GetSymbol()}{atom.GetIdx() + 1}, using sum of bond order.") return exp_val_calculated_from_bonds - except: + except Exception: return exp_val_calculated_from_bonds @@ -37,7 +37,7 @@ def regularize_formal_charges(mol, sanitize=True, verbose=False): try: Chem.SanitizeMol(mol) return mol - except: + except Exception: return None else: return mol diff --git a/dpdata/unit.py b/dpdata/unit.py index 7b5aeffc4..299ac2bbb 100644 --- a/dpdata/unit.py +++ b/dpdata/unit.py @@ -35,7 +35,7 @@ def check_unit(unit): raise RuntimeError(f"Invaild unit: {unit}") if lunit not in lconvs.keys(): raise RuntimeError(f"Invalid unit: {unit}") - except: + except Exception: raise RuntimeError(f"Invalid unit: {unit}") class Conversion(ABC): diff --git a/tests/test_pick_atom_idx.py b/tests/test_pick_atom_idx.py index 7e13cca97..bb6af61f7 100644 --- a/tests/test_pick_atom_idx.py +++ b/tests/test_pick_atom_idx.py @@ -5,7 +5,7 @@ try: import parmed exist_module=True -except: +except Exception: exist_module=False class TestPickAtomIdx(unittest.TestCase, CompSys, IsNoPBC): diff --git a/tests/test_to_ase.py b/tests/test_to_ase.py index 1cd7200a9..4ea870c79 100644 --- a/tests/test_to_ase.py +++ b/tests/test_to_ase.py @@ -7,7 +7,7 @@ from ase import Atoms from ase.io import write exist_module=True -except: +except Exception: exist_module=False @unittest.skipIf(not exist_module,"skip test_ase") diff --git a/tests/test_to_pymatgen.py b/tests/test_to_pymatgen.py index 5922828b4..0077a0495 100644 --- a/tests/test_to_pymatgen.py +++ b/tests/test_to_pymatgen.py @@ -6,7 +6,7 @@ try: from pymatgen import Structure exist_module=True -except: +except Exception: exist_module=False @unittest.skipIf(not exist_module,"skip pymatgen") diff --git a/tests/test_to_pymatgen_entry.py b/tests/test_to_pymatgen_entry.py index ef7cb942f..e0a952be3 100644 --- a/tests/test_to_pymatgen_entry.py +++ b/tests/test_to_pymatgen_entry.py @@ -7,7 +7,7 @@ try: from pymatgen.entries.computed_entries import ComputedStructureEntry exist_module=True -except: +except Exception: exist_module=False @unittest.skipIf(not exist_module,"skip pymatgen") diff --git a/tests/test_water_ions.py b/tests/test_water_ions.py index 3a9436b6d..f2fab203b 100644 --- a/tests/test_water_ions.py +++ b/tests/test_water_ions.py @@ -6,7 +6,7 @@ import ase import ase.neighborlist exist_ase=True -except: +except Exception: exist_ase=False class TestIons(unittest.TestCase):