Skip to content

Commit

Permalink
Merge pull request #2783 abostroem/gzip_fix
Browse files Browse the repository at this point in the history
Conflicts:

	CHANGES.rst
  • Loading branch information
embray committed Aug 29, 2014
1 parent 2270a5b commit 34daed3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -18,6 +18,9 @@ Bug Fixes

- ``astropy.io.fits``

- Fixed crash when reading gzip-compressed FITS tables through the Astropy
``Table`` interface. [#2783]

- ``astropy.io.misc``

- ``astropy.io.registry``
Expand Down
14 changes: 8 additions & 6 deletions astropy/io/fits/file.py
Expand Up @@ -2,7 +2,8 @@

from __future__ import division, with_statement

import gzip
from ...utils.compat import gzip as _astropy_gzip
import gzip as _system_gzip
import mmap
import os
import tempfile
Expand Down Expand Up @@ -70,6 +71,7 @@
GZIP_MAGIC = b('\x1f\x8b\x08')
PKZIP_MAGIC = b('\x50\x4b\x03\x04')

_GZIP_FILE_TYPES = (_astropy_gzip, _system_gzip)

class _File(object):
"""
Expand Down Expand Up @@ -152,7 +154,7 @@ def __init__(self, fileobj=None, mode=None, memmap=False, clobber=False):

self.fileobj_mode = fileobj_mode(self.__file)

if isinstance(fileobj, gzip.GzipFile):
if isinstance(fileobj, (_astropy_gzip.GzipFile, _system_gzip.GzipFile)):
self.compression = 'gzip'
elif isinstance(fileobj, zipfile.ZipFile):
# Reading from zip files is supported but not writing (yet)
Expand Down Expand Up @@ -297,7 +299,7 @@ def seek(self, offset, whence=0):
# present, we implement our own support for it here
if not hasattr(self.__file, 'seek'):
return
if isinstance(self.__file, gzip.GzipFile):
if isinstance(self.__file, (_astropy_gzip.GzipFile, _system_gzip.GzipFile)):
if whence:
if whence == 1:
offset = self.__file.offset + offset
Expand Down Expand Up @@ -383,7 +385,7 @@ def _open_fileobj(self, fileobj, mode, clobber):
elif isfile(fileobj):
self.__file = fileobj_open(self.name, PYFITS_MODES[mode])
else:
self.__file = gzip.open(self.name, PYFITS_MODES[mode])
self.__file = _astropy_gzip.open(self.name, PYFITS_MODES[mode])

if fmode == 'ab+':
# Return to the beginning of the file--in Python 3 when opening in
Expand Down Expand Up @@ -447,7 +449,7 @@ def _open_filename(self, filename, mode, clobber):

if ext == '.gz' or magic.startswith(GZIP_MAGIC):
# Handle gzip files
self.__file = gzip.open(self.name, PYFITS_MODES[mode])
self.__file = _astropy_gzip.open(self.name, PYFITS_MODES[mode])
self.compression = 'gzip'
elif ext == '.zip' or magic.startswith(PKZIP_MAGIC):
# Handle zip files
Expand Down Expand Up @@ -538,4 +540,4 @@ def _is_random_access_file_backed(fileobj):
from an already opened `zipfile.ZipFile` object.
"""

return isfile(fileobj) or isinstance(fileobj, gzip.GzipFile)
return isfile(fileobj) or isinstance(fileobj, (_astropy_gzip.GzipFile, _system_gzip.GzipFile))
16 changes: 16 additions & 0 deletions astropy/io/fits/tests/test_core.py
Expand Up @@ -644,6 +644,22 @@ def test_open_zipped_writeable(self):
pytest.raises(IOError, fits.open, zf, 'update')
pytest.raises(IOError, fits.open, zf, 'append')

def test_read_open_astropy_gzip_file(self):
"""
Regression test for https://github.com/astropy/astropy/issues/2774
This tests reading from a ``GzipFile`` object from Astropy's
compatibility copy of the ``gzip`` module.
"""

from ....utils.compat import gzip

gf = gzip.GzipFile(self._make_gzip_file())
try:
assert len(fits.open(gf)) == 5
finally:
gf.close()

@raises(IOError)
def test_open_multiple_member_zipfile(self):
"""
Expand Down

0 comments on commit 34daed3

Please sign in to comment.