Skip to content

Commit

Permalink
Merge 75d887f into e6d1a9e
Browse files Browse the repository at this point in the history
  • Loading branch information
ritiek committed Feb 28, 2018
2 parents e6d1a9e + 75d887f commit 0873ee2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -187,6 +187,9 @@ astropy.io.misc
astropy.io.fits
^^^^^^^^^^^^^^^

- Override ``HDUList.copy()`` to return a copy instead
of ``list``. [#7218]

astropy.io.registry
^^^^^^^^^^^^^^^^^^^

Expand Down
21 changes: 21 additions & 0 deletions astropy/io/fits/hdu/hdulist.py
Expand Up @@ -510,6 +510,27 @@ def fileinfo(self, index):

return output

def copy(self, deep=True):
"""
Return a copy of the object.
Parameters
----------
deep : bool (default=True)
Whether to create a deep copy or a shallow copy.
Returns
-------
self : HDUList
A copy of this :class:`HDUList` object.
"""

if deep:
return HDUList([hdu.copy() for hdu in self])
else:
return self[:]

def pop(self, index=-1):
# Make sure that HDUs are loaded before attempting to pop
self.readall()
Expand Down
29 changes: 29 additions & 0 deletions astropy/io/fits/tests/test_hdulist.py
Expand Up @@ -376,6 +376,35 @@ def test_file_like_3(self):
info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 5, (100,), 'int32', '')]
assert fits.info(self.temp('tmpfile.fits'), output=False) == info

def test_copy_list(self):
"""
Tests that HDUList.copy() returns a shallow copy (regression test
for ticket:7211).
"""

n = np.arange(10.0)
primary_hdu = fits.PrimaryHDU(n)
hdu = fits.ImageHDU(n)
hdul = fits.HDUList([primary_hdu, hdu])

shallow_copy = hdul.copy(deep=False)
deep_copy = hdul.copy(deep=True)

assert isinstance(shallow_copy, fits.HDUList)
assert isinstance(deep_copy, fits.HDUList)

assert shallow_copy is not hdul
assert shallow_copy[0] is hdul[0]
assert shallow_copy[1] is hdul[1]

assert deep_copy is not hdul
assert deep_copy[0] is not hdul[0]
assert deep_copy[1] is not hdul[1]
assert deep_copy[0].header == hdul[0].header
assert deep_copy[1].header == hdul[1].header
assert (deep_copy[0].data == hdul[0].data).all()
assert (deep_copy[1].data == hdul[1].data).all()

def test_new_hdu_extname(self):
"""
Tests that new extension HDUs that are added to an HDUList can be
Expand Down

0 comments on commit 0873ee2

Please sign in to comment.