Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent errors from identifier functions for loaders #404

Merged
merged 1 commit into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions specutils/io/registers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ def data_loader(label, identifier=None, dtype=Spectrum1D, extensions=None,
Set the priority of the loader. Currently influences the sorting of the
returned loaders for a dtype.
"""
def identifier_wrapper(ident):
def wrapper(*args, **kwargs):
'''In case the identifier function raises an exception, log that and continue'''
try:
return ident(*args, **kwargs)
except Exception as e:
logging.debug("Tried to read this as {} file, but could not.".format(label))
logging.debug(e, exc_info=True)
return False
return wrapper

identifier = identifier_wrapper(identifier)

def decorator(func):
io_registry.register_reader(label, dtype, func)

Expand Down
22 changes: 21 additions & 1 deletion specutils/tests/test_loaders.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import shutil
import tempfile
import urllib
Expand All @@ -11,8 +12,9 @@
from astropy.table import Table
from astropy.units import UnitsWarning
from astropy.wcs import FITSFixedWarning
from astropy.io.registry import IORegistryError

from .conftest import remote_access
from .conftest import remote_data_path, remote_access
from .. import Spectrum1D, SpectrumList
from ..io import get_loaders_by_extension

Expand Down Expand Up @@ -119,3 +121,21 @@ def test_sdss_spspec():

assert isinstance(spec, Spectrum1D)
assert spec.flux.size > 0


@pytest.mark.parametrize("name", ['file.fit', 'file.fits', 'file.dat'])
def test_no_reader_matches(name):
'''If no reader matches a file, check that the correct error is raised.
This test serves a second purpose: A badly written identifier
function might raise an error as supposed to returning False when
it cannot identify a file. The fact that this test passes means
that at the very least all identifier functions that have been
tried for that file ending did not fail with an error.
'''
with tempfile.TemporaryDirectory() as tmpdirname:
filename = os.path.join(tmpdirname, name)
with open(filename, 'w') as fp:
fp.write('asdfadasdadvzxcv')

with pytest.raises(IORegistryError):
spec = Spectrum1D.read(filename)