Skip to content

Commit

Permalink
fix #346 scan robust to corrput file (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-chambers authored Aug 28, 2024
1 parent 2e911d1 commit 8fa6680
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 7 additions & 2 deletions dascore/io/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import inspect
import os.path
import warnings
from collections import defaultdict
from collections.abc import Generator
from functools import cache, cached_property, wraps
Expand Down Expand Up @@ -712,7 +713,7 @@ def _get_fiber_io_and_req_type(
Raises
------
UnkownFileFormatError if no format is determinable from the
UnknownFileFormatError if no format is determinable from the
patch_source
"""
Expand Down Expand Up @@ -837,7 +838,11 @@ def scan(
# contents should be returned.
source = fiber_io.scan(resource, timestamp=timestamp, _pre_cast=True)
else:
source = fiber_io.scan(resource, _pre_cast=True)
try:
source = fiber_io.scan(resource, _pre_cast=True)
except OSError: # This happens if the file is corrupt see #346.
warnings.warn(f"Failed to scan {resource}", UserWarning)
continue
for attr in source:
out.append(dc.PatchAttrs.from_dict(attr))
return out
Expand Down
17 changes: 17 additions & 0 deletions tests/test_io/test_io_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,23 @@ def test_can_raise(self):
with pytest.raises(NotImplementedError):
fio.scan(bad_input)

def test_bad_checksum(self, monkeypatch, terra15_v6_path):
"""Test for when format is identified but can't read part of file #346"""
# Monkey patch scan to raise OSError. This simulates observed behavior.
fname, ver = FiberIO.manager._get_format(path=terra15_v6_path)
fiber_io = FiberIO.manager.get_fiberio(format=fname, version=ver)

def raise_os_error(*args, **kwargs):
raise OSError("Simulated OS issue")

monkeypatch.setattr(fiber_io, "scan", raise_os_error)

# Ensure scanning doesn't raise and warns
msg = "Failed to scan"
with pytest.warns(UserWarning, match=msg):
scan = dc.scan(terra15_v6_path)
assert not len(scan)


class TestCastType:
"""Test suite to ensure types are intelligently cast to type hints."""
Expand Down

0 comments on commit 8fa6680

Please sign in to comment.