8000 Merge pull request #2783 abostroem/gzip_fix · astropy/astropy@5d7acd6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d7acd6

Browse files
committed
Merge pull request #2783 abostroem/gzip_fix
2 parents 4bbfb33 + 81f389d commit 5d7acd6

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

CHANGES.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ New Features
2020

2121
- ``astropy.io.ascii``
2222

23-
- Simplify the way new Reader classes are defined, allowing custom behavior
24-
entirely by overriding inherited class attributes instead of setting
23+
- Simplify the way new Reader classes are defined, allowing custom behavior
24+
entirely by overriding inherited class attributes instead of setting
2525
instance attributes in the Reader ``__init__`` method. [#2812]
2626

2727
- ``astropy.io.fits``
@@ -204,6 +204,9 @@ Bug Fixes
204204

205205
- ``astropy.io.fits``
206206

207+
- Fixed crash when reading gzip-compressed FITS tables through the Astropy
208+
``Table`` interface. [#2783]
209+
207210
- ``astropy.io.misc``
208211

209212
- Fixed a bug that prevented h5py ``Dataset`` objects from being

astropy/io/fits/file.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
from __future__ import division, with_statement
44

5-
import gzip
5+
from ...utils.compat import gzip as _astropy_gzip
6+
import gzip as _system_gzip
67
import mmap
78
import os
89
import tempfile
@@ -70,6 +71,7 @@
7071
GZIP_MAGIC = b('\x1f\x8b\x08')
7172
PKZIP_MAGIC = b('\x50\x4b\x03\x04')
7273

74+
_GZIP_FILE_TYPES = (_astropy_gzip, _system_gzip)
7375

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

153155
self.fileobj_mode = fileobj_mode(self.__file)
154156

155-
if isinstance(fileobj, gzip.GzipFile):
157+
if isinstance(fileobj, (_astropy_gzip.GzipFile, _system_gzip.GzipFile)):
156158
self.compression = 'gzip'
157159
elif isinstance(fileobj, zipfile.ZipFile):
158160
# Reading from zip files is supported but not writing (yet)
@@ -297,7 +299,7 @@ def seek(self, offset, whence=0):
297299
# present, we implement our own support for it here
298300
if not hasattr(self.__file, 'seek'):
299301
return
300-
if isinstance(self.__file, gzip.GzipFile):
302+
if isinstance(self.__file, (_astropy_gzip.GzipFile, _system_gzip.GzipFile)):
301303
if whence:
302304
if whence == 1:
303305
offset = self.__file.offset + offset
@@ -379,7 +381,7 @@ def _open_fileobj(self, fileobj, mode, clobber):
379381
elif isfile(fileobj):
380382
self.__file = fileobj_open(self.name, PYFITS_MODES[mode])
381383
else:
382-
self.__file = gzip.open(self.name, PYFITS_MODES[mode])
384+
self.__file = _astropy_gzip.open(self.name, PYFITS_MODES[mode])
383385

384386
if fmode == 'ab+':
385387
# Return to the beginning of the file--in Python 3 when opening in
@@ -443,7 +445,7 @@ def _open_filename(self, filename, mode, clobber):
443445

444446
if ext == '.gz' or magic.startswith(GZIP_MAGIC):
445447
# Handle gzip files
446-
self.__file = gzip.open(self.name, PYFITS_MODES[mode])
448+
self.__file = _astropy_gzip.open(self.name, PYFITS_MODES[mode])
447449
self.compression = 'gzip'
448450
elif ext == '.zip' or magic.startswith(PKZIP_MAGIC):
449451
# Handle zip files
@@ -534,4 +536,4 @@ def _is_random_access_file_backed(fileobj):
534536
from an already opened `zipfile.ZipFile` object.
535537
"""
536538

537-
return isfile(fileobj) or isinstance(fileobj, gzip.GzipFile)
539+
return isfile(fileobj) or isinstance(fileobj, (_astropy_gzip.GzipFile, _system_gzip.GzipFile))

astropy/io/fits/tests/test_core.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,22 @@ def test_open_zipped_writeable(self):
644644
pytest.raises(IOError, fits.open, zf, 'update')
645645
pytest.raises(IOError, fits.open, zf, 'append')
646646

647+
def test_read_open_astropy_gzip_file(self):
648+
"""
649+
Regression test for https://github.com/astropy/astropy/issues/2774
650+
651+
This tests reading from a ``GzipFile`` object from Astropy's
652+
compatibility copy of the ``gzip`` module.
653+
"""
654+
655+
from ....utils.compat import gzip
656+
657+
gf = gzip.GzipFile(self._make_gzip_file())
658+
try:
659+
assert len(fits.open(gf)) == 5
660+
finally:
661+
gf.close()
662+
647663
@raises(IOError)
648664
def test_open_multiple_member_zipfile(self):
649665
"""

0 commit comments

Comments
 (0)
0