Skip to content

Commit

Permalink
Close the file handler if the file is corrupt. (#42)
Browse files Browse the repository at this point in the history
* Trying to make a correctly failing test

* Implemented assert_file_not_in_use

* Updated the linux test script

* Another try for linux

* Fixed the line for linux

* Make sure the test is failing

* Make sure the file is closed if ValueError
  • Loading branch information
fujiisoup authored Nov 11, 2024
1 parent a714fc0 commit 537e7df
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sif_parser/sif_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ def np_open(sif_file, ignore_corrupt=False, lazy=None):
tile, size, no_images, info = _open(f)
except AttributeError:
f = open(sif_file,'rb')
tile, size, no_images, info = _open(f)
will_close = True
try:
tile, size, no_images, info = _open(f)
except SyntaxError as e:
f.close()
raise e

# allocate np.array
if lazy == 'dask':
Expand Down Expand Up @@ -72,6 +76,8 @@ def np_open(sif_file, ignore_corrupt=False, lazy=None):
except ValueError:
data = data[:i]
if not ignore_corrupt:
if will_close:
f.close()
raise ValueError(
'The file might be corrupt. Number of files should be {} '
'according to the header, but only {} is found in the file.'
Expand Down
Binary file added testings/corrupt_data/c0rrupt.sif
Binary file not shown.
29 changes: 29 additions & 0 deletions testings/test_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import sys
import glob
import codecs
import psutil


THIS_DIR = os.path.dirname(__file__)
sys.path.append(THIS_DIR + "/../sif_parser/")
Expand Down Expand Up @@ -49,6 +51,30 @@
)


def is_file_not_in_use(filename):
platform = sys.platform
if platform == "linux":
for proc in psutil.process_iter():
try:
for item in proc.open_files():
if filename == item.path:
return False
except Exception:
pass
return True
elif platform == "darwin":
raise NotImplementedError('Testing in Mac is not supported.')
elif platform == "win32":
# for windows:
try:
os.rename(filename, filename)
except Exception:
return False
return True
else:
raise Exception("OS not supported")


def test_step_and_glue():
filename = THIS_DIR + "/step_and_glue/step_and_glue.sif"
with open(filename, "rb") as f:
Expand Down Expand Up @@ -191,6 +217,9 @@ def test_corrupt_file(filename):
with pytest.raises(ValueError) as e_info:
data, info = sif_parser.np_open(filename)

# try open with a write mode to make sure the file is closed
assert is_file_not_in_use(filename)

with pytest.warns(UserWarning, match="corrupt."):
data, info = sif_parser.np_open(filename, ignore_corrupt=True)

Expand Down

0 comments on commit 537e7df

Please sign in to comment.