Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate compatibility mode from HDF5 I/O #8899

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ astropy.io.ascii
astropy.io.misc
^^^^^^^^^^^^^^^

- Eliminate deprecated compatibility mode when writing ``Table`` metadata to HDF5 format. [#8899]

astropy.io.fits
^^^^^^^^^^^^^^^

Expand Down
21 changes: 4 additions & 17 deletions astropy/io/misc/hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ def _encode_mixins(tbl):


def write_table_hdf5(table, output, path=None, compression=False,
append=False, overwrite=False, serialize_meta=False,
compatibility_mode=False):
append=False, overwrite=False, serialize_meta=False):
"""
Write a Table object to an HDF5 file

Expand Down Expand Up @@ -304,8 +303,7 @@ def write_table_hdf5(table, output, path=None, compression=False,
return write_table_hdf5(table, f, path=path,
compression=compression, append=append,
overwrite=overwrite,
serialize_meta=serialize_meta,
compatibility_mode=compatibility_mode)
serialize_meta=serialize_meta)
finally:
f.close()

Expand Down Expand Up @@ -357,19 +355,8 @@ def write_table_hdf5(table, output, path=None, compression=False,
header_yaml = meta.get_yaml_from_table(table)

header_encoded = [h.encode('utf-8') for h in header_yaml]
if compatibility_mode:
warnings.warn("compatibility mode for writing is deprecated",
AstropyDeprecationWarning)
try:
dset.attrs[META_KEY] = header_encoded
except Exception as e:
warnings.warn(
"Attributes could not be written to the output HDF5 "
"file: {0}".format(e))

else:
output_group.create_dataset(meta_path(name),
data=header_encoded)
output_group.create_dataset(meta_path(name),
data=header_encoded)

else:
# Write the Table meta dict key:value pairs to the file as HDF5
Expand Down
Binary file added astropy/io/misc/tests/data/old_meta_example.hdf5
Binary file not shown.
35 changes: 7 additions & 28 deletions astropy/io/misc/tests/test_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from astropy.time import Time, TimeDelta
from astropy.units.quantity import QuantityInfo
from astropy.utils.exceptions import AstropyUserWarning
from astropy.utils.data import get_pkg_data_filename

try:
import h5py
Expand Down Expand Up @@ -456,8 +457,12 @@ def test_preserve_serialized(tmpdir):


@pytest.mark.skipif('not HAS_H5PY or not HAS_YAML')
def test_preserve_serialized_compatibility_mode(tmpdir):
test_file = str(tmpdir.join('test.hdf5'))
def test_preserve_serialized_old_meta_format(tmpdir):
"""Test the old meta format

Only for some files created prior to v4.0, in compatibility mode.
"""
test_file = get_pkg_data_filename('data/old_meta_example.hdf5')

t1 = Table()
t1['a'] = Column(data=[1, 2, 3], unit="s")
Expand All @@ -468,13 +473,6 @@ def test_preserve_serialized_compatibility_mode(tmpdir):
t1.meta['b'] = 1
t1.meta['c'] = {"c0": [0, 1]}

with catch_warnings() as w:
t1.write(test_file, path='the_table', serialize_meta=True,
overwrite=True, compatibility_mode=True)

assert str(w[0].message).startswith(
"compatibility mode for writing is deprecated")

t2 = Table.read(test_file, path='the_table')

assert t1['a'].unit == t2['a'].unit
Expand Down Expand Up @@ -536,25 +534,6 @@ def test_metadata_very_large(tmpdir):
assert t1.meta == t2.meta


@pytest.mark.skipif('not HAS_H5PY or not HAS_YAML')
def test_metadata_very_large_fails_compatibility_mode(tmpdir):
"""Test that very large metadata do not work in compatibility mode."""
test_file = str(tmpdir.join('test.hdf5'))
t1 = Table()
t1['a'] = Column(data=[1, 2, 3])
t1.meta["meta"] = "0" * (2 ** 16 + 1)
with catch_warnings() as w:
t1.write(test_file, path='the_table', serialize_meta=True,
overwrite=True, compatibility_mode=True)
assert len(w) == 2

# Error message slightly changed in h5py 2.7.1, thus the 2part assert
assert str(w[1].message).startswith(
"Attributes could not be written to the output HDF5 "
"file: Unable to create attribute ")
assert "bject header message is too large" in str(w[1].message)


@pytest.mark.skipif('not HAS_H5PY')
def test_skip_meta(tmpdir):

Expand Down