diff --git a/corsikaio/longitudinal.py b/corsikaio/longitudinal.py index c610254..f49551d 100644 --- a/corsikaio/longitudinal.py +++ b/corsikaio/longitudinal.py @@ -59,14 +59,18 @@ def read_longitudinal_distributions(path): """ first = True with open(path, "r") as f: - while line := f.readline().strip(): + try: + line = f.readline().strip() + except UnicodeDecodeError: + raise IOError(f"Inputfile {path} does not seem to be a longitudinal file") + while line: match = PARTICLE_HEADER_RE.match(line) if not match: if first: - raise ValueError(f"Inputfile {path} does not seem to be a longitudinal file") + raise IOError(f"Inputfile {path} does not seem to be a longitudinal file") else: - raise ValueError(f"Error reading file, expected header line, got: {line}") + raise IOError(f"Error reading file, expected header line, got: {line}") first = False n_steps = int(match.group(1)) @@ -86,7 +90,7 @@ def read_longitudinal_distributions(path): line = f.readline().strip() match = ENERGY_HEADER_RE.match(line) if not match: - raise ValueError(f"Error reading file, expected energy deposition header line, got: {line}") + raise IOError(f"Error reading file, expected energy deposition header line, got: {line}") n_steps = int(match.group(1)) # skip @@ -106,5 +110,5 @@ def read_longitudinal_distributions(path): longi["average_deviation"] = float(deviation) f.readline() - yield longi + line = f.readline().strip() diff --git a/tests/test_longitudinal.py b/tests/test_longitudinal.py index 944ae74..d43ec2d 100644 --- a/tests/test_longitudinal.py +++ b/tests/test_longitudinal.py @@ -43,3 +43,10 @@ def test_longitudinal(slant): assert n_showers == 5 else: assert n_showers == 1 + + +def test_invalid(tmp_path): + from corsikaio import read_longitudinal_distributions + + with pytest.raises(IOError, match="does not seem to be"): + next(read_longitudinal_distributions("tests/resources/corsika_77500_particle"))