Skip to content

Commit 4136ed1

Browse files
authored
Merge pull request #18907 from meeseeksmachine/auto-backport-of-pr-18882-on-v7.2.x
Backport PR #18882 on branch v7.2.x (BUG: warn instead of raise when failing to read specific files in fitsdiff script)
2 parents 8a4306b + f85f17f commit 4136ed1

3 files changed

Lines changed: 67 additions & 16 deletions

File tree

astropy/io/fits/scripts/fitsdiff.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -400,20 +400,29 @@ def main(args=None):
400400
try:
401401
for a, b in files:
402402
# TODO: pass in any additional arguments here too
403-
diff = fits.diff.FITSDiff(
404-
a,
405-
b,
406-
ignore_hdus=opts.ignore_hdus,
407-
ignore_keywords=opts.ignore_keywords,
408-
ignore_comments=opts.ignore_comments,
409-
ignore_fields=opts.ignore_fields,
410-
numdiffs=opts.numdiffs,
411-
rtol=opts.rtol,
412-
atol=opts.atol,
413-
ignore_blanks=opts.ignore_blanks,
414-
ignore_blank_cards=opts.ignore_blank_cards,
415-
)
416-
403+
try:
404+
diff = fits.diff.FITSDiff(
405+
a,
406+
b,
407+
ignore_hdus=opts.ignore_hdus,
408+
ignore_keywords=opts.ignore_keywords,
409+
ignore_comments=opts.ignore_comments,
410+
ignore_fields=opts.ignore_fields,
411+
numdiffs=opts.numdiffs,
412+
rtol=opts.rtol,
413+
atol=opts.atol,
414+
ignore_blanks=opts.ignore_blanks,
415+
ignore_blank_cards=opts.ignore_blank_cards,
416+
)
417+
except OSError:
418+
if not opts.quiet:
419+
msg = f"Warning: failed to open {a}"
420+
if b != a:
421+
msg += f" (or {b})"
422+
msg += ". Skipping."
423+
print(msg, file=sys.stderr)
424+
identical.append(None)
425+
continue
417426
diff.report(fileobj=out_file)
418427
identical.append(diff.identical)
419428

astropy/io/fits/tests/test_fitsdiff.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from astropy.io.fits.convenience import writeto
1212
from astropy.io.fits.hdu import PrimaryHDU, hdulist
1313
from astropy.io.fits.scripts import fitsdiff
14+
from astropy.utils.compat.optional_deps import HAS_UNCOMPRESSPY
1415
from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH
1516

1617
from .conftest import FitsTestCase
@@ -236,7 +237,6 @@ def test_quiet(self, capsys):
236237
assert out == ""
237238
assert err == ""
238239

239-
@pytest.mark.slow
240240
def test_path(self, capsys):
241241
os.mkdir(self.temp("sub/"))
242242
tmp_b = self.temp("sub/ascii.fits")
@@ -256,7 +256,9 @@ def test_path(self, capsys):
256256
tmp_d = self.temp("sub/")
257257
assert fitsdiff.main(["-q", self.data_dir, tmp_d]) == 1
258258
assert fitsdiff.main(["-q", tmp_d, self.data_dir]) == 1
259-
assert fitsdiff.main(["-q", self.data_dir, self.data_dir]) == 0
259+
260+
expected_retv = int(not HAS_UNCOMPRESSPY)
261+
assert fitsdiff.main(["-q", self.data_dir, self.data_dir]) == expected_retv
260262

261263
# no match
262264
tmp_c = self.data("arange.fits")
@@ -273,6 +275,43 @@ def test_path(self, capsys):
273275
assert fitsdiff.main(["-q", tmp_f, self.data_dir]) == 0
274276
assert fitsdiff.main(["-q", self.data_dir, tmp_f]) == 0
275277

278+
@pytest.mark.filterwarnings("ignore:unclosed file:ResourceWarning")
279+
def test_warning_unreadable_file(self, capsys, monkeypatch):
280+
# simulate not having uncompresspy installed regardless of the actual state
281+
monkeypatch.setattr(fits.file, "HAS_UNCOMPRESSPY", False)
282+
283+
Zfile = self.data("lzw.fits.Z")
284+
assert fitsdiff.main([Zfile, Zfile]) != 0
285+
out, err = capsys.readouterr()
286+
assert out == ""
287+
assert err == f"Warning: failed to open {Zfile}. Skipping.\n"
288+
289+
os.mkdir(self.temp("sub/"))
290+
tmp = self.temp("sub/ascii.fits")
291+
tmp_h = self.data("group.fits")
292+
with hdulist.fitsopen(tmp_h) as hdu:
293+
hdu.writeto(tmp)
294+
295+
assert fitsdiff.main([Zfile, tmp]) != 0
296+
out, err = capsys.readouterr()
297+
assert out == ""
298+
assert err == f"Warning: failed to open {Zfile} (or {tmp}). Skipping.\n"
299+
300+
assert fitsdiff.main(["-q", Zfile, tmp]) != 0
301+
out, err = capsys.readouterr()
302+
assert out == ""
303+
assert err == ""
304+
305+
assert fitsdiff.main([tmp, Zfile]) != 0
306+
out, err = capsys.readouterr()
307+
assert out == ""
308+
assert err == f"Warning: failed to open {tmp} (or {Zfile}). Skipping.\n"
309+
310+
assert fitsdiff.main(["-q", tmp, Zfile]) != 0
311+
out, err = capsys.readouterr()
312+
assert out == ""
313+
assert err == ""
314+
276315
def test_ignore_hdus(self):
277316
a = np.arange(100).reshape(10, 10)
278317
b = a.copy() + 1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed a bug in ``fitsdiff`` script where failing to read a single file could
2+
crash the entire program. A warning is now printed instead, and such files
3+
are simply ignored.

0 commit comments

Comments
 (0)