From 408681d0803c0632e21d6d9f94664f75938106dd Mon Sep 17 00:00:00 2001 From: "Erik M. Bray" Date: Tue, 16 Sep 2014 18:37:11 -0400 Subject: [PATCH] Fixes #2710 --- CHANGES.rst | 4 ++++ astropy/io/fits/hdu/image.py | 5 ++++- astropy/io/fits/tests/test_image.py | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 77ed81b879b..15abed3ada0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -260,6 +260,10 @@ Bug Fixes - ``astropy.io.fits`` + - Fixed a crash when reading scaled float data out of a FITS file that was + loaded from a string (using ``HDUList.fromfile``) rather than from a file. + [#2710] + - Fixed a crash when reading data from an HDU whose header contained in invalid value for the BLANK keyword (eg. a string value instead of an integer as required by the FITS Standard). Invalid BLANK keywords are now diff --git a/astropy/io/fits/hdu/image.py b/astropy/io/fits/hdu/image.py index f382aa93036..3cfb4bfc50f 100644 --- a/astropy/io/fits/hdu/image.py +++ b/astropy/io/fits/hdu/image.py @@ -616,7 +616,10 @@ def _get_scaled_image_data(self, offset, shape): if new_dtype is not None: data = np.array(raw_data, dtype=new_dtype) else: # floating point cases - if self._file.memmap: + if self._file is not None and self._file.memmap: + data = raw_data.copy() + elif not raw_data.flags.writeable: + # create a writeable copy if needed data = raw_data.copy() # if not memmap, use the space already in memory else: diff --git a/astropy/io/fits/tests/test_image.py b/astropy/io/fits/tests/test_image.py index 4cef8911930..7983b8068f1 100644 --- a/astropy/io/fits/tests/test_image.py +++ b/astropy/io/fits/tests/test_image.py @@ -809,6 +809,24 @@ def test_invalid_blank(self): msg = "Invalid 'BLANK' keyword" assert msg in str(w[1].message) + def test_scaled_image_fromfile(self): + """ + Regression test for https://github.com/astropy/astropy/issues/2710 + """ + + # Make some sample data + a = np.arange(100, dtype=np.float32) + + hdu = fits.PrimaryHDU(data=a.copy()) + hdu.scale(bscale=1.1) + hdu.writeto(self.temp('test.fits')) + + with open(self.temp('test.fits'), 'rb') as f: + file_data = f.read() + + hdul = fits.HDUList.fromstring(file_data) + assert np.allclose(hdul[0].data, a) + class TestCompressedImage(FitsTestCase): def test_empty(self):