From 8a9ada5389a705e6d516ecac9e22cfeedec2559e Mon Sep 17 00:00:00 2001 From: Mike Taves Date: Sat, 4 Sep 2021 07:33:30 +1200 Subject: [PATCH] refactor(exceptions): reclassify IOError as OSError or other type --- autotest/t003_test.py | 6 +++--- autotest/t017_test.py | 8 ++++---- flopy/mf6/mfpackage.py | 4 ++-- flopy/mf6/utils/output_util.py | 10 +++++----- flopy/modflow/mf.py | 4 ++-- flopy/modflow/mfoc.py | 2 +- flopy/mt3d/mt.py | 2 +- flopy/utils/binaryfile.py | 6 +++--- flopy/utils/datafile.py | 2 +- flopy/utils/flopy_io.py | 4 ++-- flopy/utils/mfreadnam.py | 8 ++++---- flopy/utils/observationfile.py | 2 +- flopy/utils/optionblock.py | 5 ++--- flopy/utils/util_array.py | 7 +++---- release/make-release.py | 6 ++---- 15 files changed, 36 insertions(+), 40 deletions(-) diff --git a/autotest/t003_test.py b/autotest/t003_test.py index 60a3da2b7b..b7902f2b55 100644 --- a/autotest/t003_test.py +++ b/autotest/t003_test.py @@ -71,7 +71,7 @@ def test_loadoc(): return -@raises(IOError) +@raises(OSError) def test_loadoc_lenfail(): ws = os.path.join("temp", "t003") ml = flopy.modflow.Modflow(model_ws=ws) @@ -111,12 +111,12 @@ def test_loadoc_nstpfail(): return -@raises(IOError) +@raises(OSError) def test_load_nam_mf_nonexistant_file(): ml = flopy.modflow.Modflow.load("nonexistant.nam") -@raises(IOError) +@raises(OSError) def test_load_nam_mt_nonexistant_file(): ml = flopy.mt3d.Mt3dms.load("nonexistant.nam") diff --git a/autotest/t017_test.py b/autotest/t017_test.py index 342e72bac0..de7a9d50ab 100644 --- a/autotest/t017_test.py +++ b/autotest/t017_test.py @@ -46,7 +46,7 @@ def test_formattedfile_read(): fname = os.path.join(cpth, "empty.githds") with open(fname, "w"): pass - with assert_raises(IOError): + with assert_raises(ValueError): flopy.utils.FormattedHeadFile(fname) return @@ -86,9 +86,9 @@ def test_binaryfile_read(): fname = os.path.join(cpth, "empty.githds") with open(fname, "w"): pass - with assert_raises(IOError): + with assert_raises(ValueError): flopy.utils.HeadFile(fname) - with assert_raises(IOError): + with assert_raises(ValueError): flopy.utils.HeadFile(fname, "head", "single") return @@ -210,7 +210,7 @@ def test_cellbudgetfile_position(): fname = os.path.join(cpth, "empty.gitcbc") with open(fname, "w"): pass - with assert_raises(IOError): + with assert_raises(ValueError): flopy.utils.CellBudgetFile(fname) return diff --git a/flopy/mf6/mfpackage.py b/flopy/mf6/mfpackage.py index be54b007f8..786b65d7c9 100644 --- a/flopy/mf6/mfpackage.py +++ b/flopy/mf6/mfpackage.py @@ -1844,7 +1844,7 @@ def _update_size_defs(self): for key in dataset.get_active_key_list(): try: data = dataset.get_data(key=key[0]) - except (IOError, OSError, MFDataException): + except (OSError, MFDataException): # TODO: Handle case where external file # path has been moved data = None @@ -1857,7 +1857,7 @@ def _update_size_defs(self): new_size = -1 try: data = dataset.get_data() - except (IOError, OSError, MFDataException): + except (OSError, MFDataException): # TODO: Handle case where external file # path has been moved data = None diff --git a/flopy/mf6/utils/output_util.py b/flopy/mf6/utils/output_util.py index 0a1638be58..3c95f66b12 100644 --- a/flopy/mf6/utils/output_util.py +++ b/flopy/mf6/utils/output_util.py @@ -183,7 +183,7 @@ def get_layerfile_data(self, f=data, text=rectype): try: f = os.path.join(self._sim_ws, f) return HeadFile(f, text=text) - except (IOError, FileNotFoundError): + except OSError: return setattr(self.__class__, rectype, get_layerfile_data) @@ -270,7 +270,7 @@ def __budget(self, precision="double"): try: budget_file = os.path.join(self._sim_ws, self._budget[0]) return CellBudgetFile(budget_file, precision=precision) - except (IOError, FileNotFoundError): + except OSError: return None def __obs(self, f=None): @@ -290,7 +290,7 @@ def __obs(self, f=None): try: obs_file = os.path.join(self._sim_ws, obs_file) return Mf6Obs(obs_file) - except (IOError, FileNotFoundError): + except OSError: return None def __csv(self, f=None): @@ -310,7 +310,7 @@ def __csv(self, f=None): try: csv_file = os.path.join(self._sim_ws, csv_file) return CsvFile(csv_file) - except (IOError, FileNotFoundError): + except OSError: return None def __list(self): @@ -325,7 +325,7 @@ def __list(self): try: list_file = os.path.join(self._sim_ws, self._lst) return Mf6ListBudget(list_file) - except (AssertionError, IOError, FileNotFoundError): + except (AssertionError, OSError): return None def __mulitfile_handler(self, f, flist): diff --git a/flopy/modflow/mf.py b/flopy/modflow/mf.py index 16a7eba3f9..110b4eb1e7 100644 --- a/flopy/modflow/mf.py +++ b/flopy/modflow/mf.py @@ -716,7 +716,7 @@ def load( ): namefile_path += ".nam" if not os.path.isfile(namefile_path): - raise IOError(f"cannot find name file: {namefile_path}") + raise OSError(f"cannot find name file: {namefile_path}") # Determine model name from 'f', without any extension or path modelname = os.path.splitext(os.path.basename(f))[0] @@ -950,7 +950,7 @@ def load( print(f" {os.path.basename(fname)}") if len(files_not_loaded) > 0: print( - " The following {len(files_not_loaded)} packages " + f" The following {len(files_not_loaded)} packages " "were not loaded." ) for fname in files_not_loaded: diff --git a/flopy/modflow/mfoc.py b/flopy/modflow/mfoc.py index d45f99f867..3777504a93 100644 --- a/flopy/modflow/mfoc.py +++ b/flopy/modflow/mfoc.py @@ -795,7 +795,7 @@ def load( # validate the size of nstp if len(nstp) != nper: - raise IOError( + raise OSError( f"nstp must be a list with {nper} entries, " f"provided nstp list has {len(nstp)} entries." ) diff --git a/flopy/mt3d/mt.py b/flopy/mt3d/mt.py index 91b53054ac..42358007aa 100644 --- a/flopy/mt3d/mt.py +++ b/flopy/mt3d/mt.py @@ -516,7 +516,7 @@ def load( # read name file namefile_path = os.path.join(mt.model_ws, f) if not os.path.isfile(namefile_path): - raise IOError(f"cannot find name file: {namefile_path}") + raise FileNotFoundError(f"cannot find name file: {namefile_path}") try: ext_unit_dict = mfreadnam.parsenamefile( namefile_path, mt.mfnam_packages, verbose=verbose diff --git a/flopy/utils/binaryfile.py b/flopy/utils/binaryfile.py index a46419d9b2..f04b2ac296 100755 --- a/flopy/utils/binaryfile.py +++ b/flopy/utils/binaryfile.py @@ -218,7 +218,7 @@ def get_headfile_precision(filename): f.seek(0, 0) # reset to beginning assert f.tell() == 0 if totalbytes == 0: - raise IOError(f"datafile error: file is empty: {filename}") + raise ValueError(f"datafile error: file is empty: {filename}") # first try single vartype = [ @@ -260,7 +260,7 @@ def get_headfile_precision(filename): result = "double" except: f.close() - raise IOError( + raise ValueError( f"Could not determine the precision of the headfile {filename}" ) @@ -610,7 +610,7 @@ def __init__(self, filename, precision="auto", verbose=False, **kwargs): self.file.seek(0, 0) # reset to beginning assert self.file.tell() == 0 if totalbytes == 0: - raise IOError(f"datafile error: file is empty: {filename}") + raise ValueError(f"datafile error: file is empty: {filename}") self.nrow = 0 self.ncol = 0 self.nlay = 0 diff --git a/flopy/utils/datafile.py b/flopy/utils/datafile.py index b919b0ef3e..04749dca0d 100755 --- a/flopy/utils/datafile.py +++ b/flopy/utils/datafile.py @@ -164,7 +164,7 @@ def __init__(self, filename, precision, verbose, kwargs): self.file.seek(0, 0) # reset to beginning assert self.file.tell() == 0 if totalbytes == 0: - raise IOError(f"datafile error: file is empty: {filename}") + raise ValueError(f"datafile error: file is empty: {filename}") self.nrow = 0 self.ncol = 0 self.nlay = 0 diff --git a/flopy/utils/flopy_io.py b/flopy/utils/flopy_io.py index 325058931a..eb96d09b46 100755 --- a/flopy/utils/flopy_io.py +++ b/flopy/utils/flopy_io.py @@ -433,9 +433,9 @@ def ulstrd(f, nlist, ra, model, sfac_columns, ext_unit_dict): namdata = ext_unit_dict[inunit] file_handle = namdata.filehandle else: - raise IOError(errmsg) + raise OSError(errmsg) else: - raise IOError(errmsg) + raise OSError(errmsg) if namdata.filetype == "DATA(BINARY)": binary = True if not binary: diff --git a/flopy/utils/mfreadnam.py b/flopy/utils/mfreadnam.py index 906c4340cb..27b29f465c 100644 --- a/flopy/utils/mfreadnam.py +++ b/flopy/utils/mfreadnam.py @@ -112,9 +112,9 @@ def parsenamefile(namfilename, packages, verbose=True): Raises ------ - IOError: + FileNotFoundError If namfilename does not exist in the directory. - ValueError: + ValueError For lines that cannot be parsed. """ # initiate the ext_unit_dict dictionary @@ -125,7 +125,7 @@ def parsenamefile(namfilename, packages, verbose=True): if not os.path.isfile(namfilename): # help diagnose the namfile and directory - raise IOError( + raise FileNotFoundError( f"Could not find {namfilename} " f"in directory {os.path.dirname(namfilename)}" ) @@ -179,7 +179,7 @@ def parsenamefile(namfilename, packages, verbose=True): kwargs["errors"] = "replace" try: filehandle = open(fname, openmode, **kwargs) - except IOError: + except OSError: if verbose: print(f"could not set filehandle to {fpath}") filehandle = None diff --git a/flopy/utils/observationfile.py b/flopy/utils/observationfile.py index 7984f65da1..85a9172e48 100644 --- a/flopy/utils/observationfile.py +++ b/flopy/utils/observationfile.py @@ -294,7 +294,7 @@ def __init__(self, filename, verbose=False, isBinary="auto"): isBinary = True else: err = "Could not determine if file is binary or ascii" - raise IOError(err) + raise ValueError(err) if isBinary: # --open binary head file self.file = open(filename, "rb") diff --git a/flopy/utils/optionblock.py b/flopy/utils/optionblock.py index db377436d9..427616fbe9 100644 --- a/flopy/utils/optionblock.py +++ b/flopy/utils/optionblock.py @@ -350,11 +350,10 @@ def load_options(cls, options, package): if openfile: try: options = open(options, "r") - except IOError: - err_msg = ( + except OSError: + raise TypeError( f"Unrecognized type for options variable: {type(options)}" ) - raise TypeError(err_msg) option_line = "" while True: diff --git a/flopy/utils/util_array.py b/flopy/utils/util_array.py index d9c22a9c57..71127471f8 100644 --- a/flopy/utils/util_array.py +++ b/flopy/utils/util_array.py @@ -2911,10 +2911,9 @@ def load( elif cr_dict["type"] == "external": ext_unit = ext_unit_dict[cr_dict["nunit"]] if ext_unit.filehandle is None: - raise IOError( - "cannot read unit {0}, filename: {1}".format( - cr_dict["nunit"], ext_unit.filename - ) + raise OSError( + f"cannot read unit {cr_dict['nunit']}, " + f"filename: {ext_unit.filename}" ) elif "binary" not in str(cr_dict["fmtin"].lower()): assert cr_dict["nunit"] in list(ext_unit_dict.keys()) diff --git a/release/make-release.py b/release/make-release.py index da781c0045..e0453e8cc5 100644 --- a/release/make-release.py +++ b/release/make-release.py @@ -176,8 +176,7 @@ def update_version(): name_pos = idx + 1 except: - msg = "There was a problem updating the version file" - raise IOError(msg) + raise OSError("There was a problem updating the version file") try: # write new version file @@ -204,8 +203,7 @@ def update_version(): f.close() print("Successfully updated version.py") except: - msg = "There was a problem updating the version file" - raise IOError(msg) + raise OSError("There was a problem updating the version file") # update README.md with new version information update_readme_markdown(vmajor, vminor, vmicro)