Skip to content

Commit 428857e

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 428857e

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
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/tskit/util.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import json
3131
import numbers
3232
import os
33+
import sys
3334
import textwrap
3435
from typing import Union
3536

@@ -878,6 +879,10 @@ def raise_known_file_format_errors(open_file, existing_exception):
878879
Sniffs the file for pk-zip or hdf header bytes, then raises an exception
879880
if these are detected, if not raises the existing exception.
880881
"""
882+
if (sys.version_info[0] >= 3) & (sys.version_info[1] > 10):
883+
# add_note has been added in python 3.11
884+
# This condition can be removed once 3.10 is end-of-life
885+
existing_exception.add_note(f"While trying to load {open_file.name}")
881886
# Check for HDF5 header bytes
882887
try:
883888
open_file.seek(0)
@@ -887,14 +892,14 @@ def raise_known_file_format_errors(open_file, existing_exception):
887892
raise existing_exception
888893
if header == b"\x89HDF":
889894
raise tskit.FileFormatError(
890-
"The specified file appears to be in HDF5 format. This file "
895+
f"The file {open_file.name} appears to be in HDF5 format. This file "
891896
"may have been generated by msprime < 0.6.0 (June 2018) which "
892897
"can no longer be read directly. Please convert to the new "
893898
"kastore format using the ``tskit upgrade`` command from tskit version<0.6.2"
894899
) from existing_exception
895900
if header[:2] == b"\x50\x4b":
896901
raise tskit.FileFormatError(
897-
"The specified file appears to be in zip format, so may be a compressed "
902+
f"The file {open_file.name} appears to be in zip format, so may be a compressed "
898903
"tree sequence. Try using the tszip module to decompress this file before "
899904
"loading. `pip install tszip; tsunzip <filename>` or use "
900905
"`tszip.decompress` in Python code."

0 commit comments

Comments
 (0)