Skip to content

Commit d151baf

Browse files
committed
Avoid crash when table contains mixin info and yaml is missing
Fix astropy#7783
1 parent 4469c44 commit d151baf

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎astropy/io/fits/connect.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,21 @@ def _decode_mixins(tbl):
9393
del tbl.meta['comments'][i0:i1 + 1]
9494
if not tbl.meta['comments']:
9595
del tbl.meta['comments']
96-
info = meta.get_header_from_yaml(lines)
96+
97+
try:
98+
info = meta.get_header_from_yaml(lines)
99+
except ImportError as exc:
100+
if 'PyYAML package is required' in str(exc):
101+
warnings.warn(
102+
"the file contains information about Astropy native objects "
103+
"(mixin columns) that have been serialized when writing it, "
104+
"but the PyYAML package is required to read those. Without "
105+
"this package some information will be missing in the table",
106+
AstropyUserWarning
107+
)
108+
return tbl
109+
else:
110+
raise
97111

98112
# Add serialized column information to table meta for use in constructing mixins
99113
tbl.meta['__serialized_columns__'] = info['meta']['__serialized_columns__']

‎astropy/io/fits/tests/test_connect.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import gc
3+
import sys
34
import pathlib
45
import warnings
56

@@ -885,3 +886,17 @@ def test_round_trip_masked_table_serialize_mask(tmpdir, method):
885886
t[name].mask = False
886887
t2[name].mask = False
887888
assert np.all(t2[name] == t[name])
889+
890+
891+
@pytest.mark.skipif('not HAS_YAML')
892+
def test_read_serialized_without_yaml(tmpdir, monkeypatch):
893+
filename = str(tmpdir.join('test.fits'))
894+
t = Table([mixin_cols['sc']])
895+
t.write(filename)
896+
897+
monkeypatch.setitem(sys.modules, 'yaml', None)
898+
with pytest.warns(AstropyUserWarning):
899+
t2 = Table.read(filename)
900+
901+
assert t2.colnames == ['col0.ra', 'col0.dec']
902+
assert len(t2) == 2

0 commit comments

Comments
 (0)