@@ -153,6 +153,54 @@ def test_write_null_data_values():
153153 assert lines == exp_output
154154
155155
156+ @pytest .mark .parametrize (
157+ "val" ,
158+ [float ("nan" ), np .nan , float ("+inf" ), np .inf , float ("-inf" ), - np .inf ],
159+ )
160+ def test_write_non_finite_values_to_mrt (val ):
161+ # see https://github.com/astropy/astropy/issues/17840
162+ out = StringIO ()
163+ data = {"A" : [0 , val ], "B" : [val , 1 ]}
164+ t = Table (data )
165+ expected = (
166+ r"^The following columns contain NaNs or infs, "
167+ r"which are not allowed in MRT\: {columns}\n"
168+ r"Mask these values to write them as null \(blank\) entries\.$"
169+ )
170+ with pytest .raises (ValueError , match = expected .format (columns = "A, B" )):
171+ t .write (out , format = "ascii.mrt" )
172+
173+ t_ma_a = Table (
174+ data = {
175+ "A" : MaskedColumn (data ["A" ], mask = [False , True ]),
176+ "B" : data ["B" ],
177+ }
178+ )
179+ with pytest .raises (ValueError , match = expected .format (columns = "B" )):
180+ t_ma_a .write (out , format = "ascii.mrt" )
181+
182+ t_ma_b = Table (
183+ data = {
184+ "A" : data ["A" ],
185+ "B" : MaskedColumn (data ["B" ], mask = [True , False ]),
186+ }
187+ )
188+ with pytest .raises (ValueError , match = expected .format (columns = "A" )):
189+ t_ma_b .write (out , format = "ascii.mrt" )
190+
191+ t_ma = Table (
192+ data = {
193+ "A" : MaskedColumn (data ["A" ], mask = [False , True ]),
194+ "B" : MaskedColumn (data ["B" ], mask = [True , False ]),
195+ }
196+ )
197+ t_ma .write (out , format = "ascii.mrt" )
198+ assert out .getvalue ().splitlines ()[- 2 :] == [
199+ "0.0 " ,
200+ " 1.0" ,
201+ ]
202+
203+
156204def test_write_byte_by_byte_for_masked_column ():
157205 """
158206 This test differs from the ``test_write_null_data_values``
0 commit comments