Skip to content

Commit

Permalink
Merge pull request #2710 from embray/fits/issue-2710
Browse files Browse the repository at this point in the history
FITS objects created with `fromstring` have problems?
  • Loading branch information
embray committed Sep 17, 2014
1 parent 70bfb08 commit a794bde
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.rst
Expand Up @@ -18,6 +18,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
Expand Down
5 changes: 4 additions & 1 deletion astropy/io/fits/hdu/image.py
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions astropy/io/fits/tests/test_image.py
Expand Up @@ -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):
Expand Down

0 comments on commit a794bde

Please sign in to comment.