Skip to content

Commit a2c1361

Browse files
author
Alexis Simon
committed
python/tskit/util.py: Add a file name to FileFormatError
Closes #2467 Uses the .add_note method of exception, introduced in Python 3.11. Also hardcodes the file name in the raised from exceptions for HDF5 or zip. Until 3.10 is EOF, needs the sys module.
1 parent b7fd993 commit a2c1361

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ python/benchmark/*.html
77
.env
88
.vscode
99
env
10+
# pixi environments
11+
.pixi/*
12+
!.pixi/config.toml

python/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ build
55
.*.swp
66
.*.swo
77
*/.ipynb_checkpoints
8+
# pixi environments
9+
.pixi/*
10+
!.pixi/config.toml

python/tests/test_file_format.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,12 @@ def test_format_too_old_raised_for_hdf5(self):
261261

262262
with pytest.raises(
263263
exceptions.FileFormatError,
264-
match="appears to be in HDF5 format",
264+
match=f"{filename}.*appears to be in HDF5 format",
265265
):
266266
tskit.load(path)
267267
with pytest.raises(
268268
exceptions.FileFormatError,
269-
match="appears to be in HDF5 format",
269+
match=f"{filename}.*appears to be in HDF5 format",
270270
):
271271
tskit.TableCollection.load(path)
272272

@@ -284,9 +284,15 @@ class TestErrors(TestFileFormat):
284284
def test_tszip_file(self):
285285
ts = msprime.simulate(5)
286286
tszip.compress(ts, self.temp_file)
287-
with pytest.raises(tskit.FileFormatError, match="appears to be in zip format"):
287+
with pytest.raises(
288+
tskit.FileFormatError,
289+
match=f"{self.temp_file}.*appears to be in zip format",
290+
):
288291
tskit.load(self.temp_file)
289-
with pytest.raises(tskit.FileFormatError, match="appears to be in zip format"):
292+
with pytest.raises(
293+
tskit.FileFormatError,
294+
match=f"{self.temp_file}.*appears to be in zip format",
295+
):
290296
tskit.TableCollection.load(self.temp_file)
291297

292298

@@ -897,7 +903,10 @@ def test_format_name_error(self):
897903
data = dict(store)
898904
data["format/name"] = np.array(bytearray(bad_name.encode()), dtype=np.int8)
899905
kastore.dump(data, self.temp_file)
900-
with pytest.raises(exceptions.FileFormatError):
906+
with pytest.raises(
907+
exceptions.FileFormatError,
908+
match=f"While trying to load {self.temp_file}",
909+
):
901910
tskit.load(self.temp_file)
902911

903912
def test_load_bad_formats(self):
@@ -908,12 +917,16 @@ def test_load_bad_formats(self):
908917
# Now some ascii text
909918
with open(self.temp_file, "wb") as f:
910919
f.write(b"Some ASCII text")
911-
with pytest.raises(exceptions.FileFormatError):
920+
with pytest.raises(
921+
exceptions.FileFormatError, match=f"While trying to load {self.temp_file}"
922+
):
912923
tskit.load(self.temp_file)
913924
# Now write 8k of random bytes
914925
with open(self.temp_file, "wb") as f:
915926
f.write(os.urandom(8192))
916-
with pytest.raises(exceptions.FileFormatError):
927+
with pytest.raises(
928+
exceptions.FileFormatError, match=f"While trying to load {self.temp_file}"
929+
):
917930
tskit.load(self.temp_file)
918931

919932
def test_load_bad_formats_fileobj(self):
@@ -925,7 +938,9 @@ def load():
925938
load()
926939
with open(self.temp_file, "wb") as f:
927940
f.write(b"Some ASCII text")
928-
with pytest.raises(exceptions.FileFormatError):
941+
with pytest.raises(
942+
exceptions.FileFormatError, match=f"While trying to load {self.temp_file}"
943+
):
929944
load()
930945

931946

python/tskit/util.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,7 @@ def raise_known_file_format_errors(open_file, existing_exception):
878878
Sniffs the file for pk-zip or hdf header bytes, then raises an exception
879879
if these are detected, if not raises the existing exception.
880880
"""
881+
existing_exception.add_note(f"While trying to load {open_file.name}")
881882
# Check for HDF5 header bytes
882883
try:
883884
open_file.seek(0)
@@ -887,16 +888,16 @@ def raise_known_file_format_errors(open_file, existing_exception):
887888
raise existing_exception
888889
if header == b"\x89HDF":
889890
raise tskit.FileFormatError(
890-
"The specified file appears to be in HDF5 format. This file "
891+
f"The file {open_file.name} appears to be in HDF5 format. This file "
891892
"may have been generated by msprime < 0.6.0 (June 2018) which "
892893
"can no longer be read directly. Please convert to the new "
893894
"kastore format using the ``tskit upgrade`` command from tskit version<0.6.2"
894895
) from existing_exception
895896
if header[:2] == b"\x50\x4b":
896897
raise tskit.FileFormatError(
897-
"The specified file appears to be in zip format, so may be a compressed "
898-
"tree sequence. Try using the tszip module to decompress this file before "
899-
"loading. `pip install tszip; tsunzip <filename>` or use "
898+
f"The file {open_file.name} appears to be in zip format, so may be a "
899+
"compressed tree sequence. Try using the tszip module to decompress this "
900+
"file before loading. `pip install tszip; tsunzip <filename>` or use "
900901
"`tszip.decompress` in Python code."
901902
) from existing_exception
902903
raise existing_exception

0 commit comments

Comments
 (0)