2121
2222ECSV_VERSION = '1.0'
2323DELIMITERS = (' ' , ',' )
24+ ECSV_DATATYPES = (
25+ 'bool' , 'int8' , 'int16' , 'int32' , 'int64' , 'uint8' , 'uint16' ,
26+ 'uint32' , 'uint64' , 'float16' , 'float32' , 'float64' ,
27+ 'float128' , 'complex64' , 'complex128' , 'complex256' , 'string' )
2428
2529
2630class EcsvHeader (basic .BasicHeader ):
@@ -176,7 +180,11 @@ def get_cols(self, lines):
176180 for attr in ('description' , 'format' , 'unit' , 'meta' , 'shape' , 'subtype' ):
177181 if attr in header_cols [col .name ]:
178182 setattr (col , attr , header_cols [col .name ][attr ])
183+
179184 col .dtype = header_cols [col .name ]['datatype' ]
185+ if col .dtype not in ECSV_DATATYPES :
186+ raise ValueError (f'datatype { col .dtype !r} of column { col .name !r} '
187+ f'is not in allowed values { ECSV_DATATYPES } ' )
180188
181189 # ECSV "string" means numpy dtype.kind == 'U' AKA str in Python 3
182190 for attr in ('dtype' , 'subtype' ):
@@ -186,6 +194,11 @@ def get_cols(self, lines):
186194 raise TypeError ('ecsv reader does not support complex number types' )
187195
188196
197+ def _check_dtype_is_str (col ):
198+ if col .dtype != 'str' :
199+ raise ValueError (f'datatype of column { col .name !r} must be "string"' )
200+
201+
189202class EcsvOutputter (core .TableOutputter ):
190203 """
191204 After reading the input lines and processing, convert the Reader columns
@@ -220,13 +233,15 @@ def _convert_vals(self, cols):
220233 try :
221234 # 1-d or N-d object columns are serialized as JSON.
222235 if col .subtype == 'object' :
236+ _check_dtype_is_str (col )
223237 col_vals = [json .loads (val ) for val in col .str_vals ]
224238 col .data = np .array (col_vals , dtype = col .subtype )
225239
226240 # Variable length arrays with shape (n, m, ..., *) for fixed
227241 # n, m, .. and variable in last axis. Masked values here are
228242 # not currently supported.
229243 elif col .shape and col .shape [- 1 ] is None :
244+ _check_dtype_is_str (col )
230245 # Remake as a 1-d object column of numpy ndarrays using the
231246 # datatype specified in the ECSV file.
232247 col_vals = [np .array (json .loads (val ), dtype = col .subtype )
@@ -240,11 +255,12 @@ def _convert_vals(self, cols):
240255 # Multidim columns with consistent shape (n, m, ...). These
241256 # might be masked.
242257 elif col .shape :
258+ _check_dtype_is_str (col )
243259 col_vals = [json .loads (val ) for val in col .str_vals ]
244260 # Make a numpy object array of col_vals to look for None
245261 # (masked values)
246262 data = np .array (col_vals , dtype = object )
247- mask = (data == None ) # noqa
263+ mask = (data == None ) # noqa: E711
248264 if not np .any (mask ):
249265 # No None's, just convert to required dtype
250266 col .data = data .astype (col .subtype )
@@ -253,7 +269,7 @@ def _convert_vals(self, cols):
253269 kind = np .dtype (col .subtype ).kind
254270 data [mask ] = {'U' : '' , 'S' : b'' }.get (kind , 0 )
255271 # Finally make a MaskedArray with the filled data + mask
256- col .data = np .ma .array (data .astype (col .dtype ), mask = mask )
272+ col .data = np .ma .array (data .astype (col .subtype ), mask = mask )
257273
258274 # Regular scalar value column
259275 else :
@@ -311,12 +327,14 @@ def str_vals(self):
311327 - Only replace masked values with "", not the generalized filling
312328 """
313329 for col in self .cols :
314- if len (col .shape ) > 1 :
315- def format_col_item (idx ):
316- return json .dumps (col [idx ].tolist (), separators = (',' , ':' ))
317- elif col .info .dtype .kind == 'O' :
330+ if len (col .shape ) > 1 or col .info .dtype .kind == 'O' :
318331 def format_col_item (idx ):
319- return json .dumps (col [idx ], separators = (',' , ':' ))
332+ obj = col [idx ]
333+ try :
334+ obj = obj .tolist ()
335+ except AttributeError :
336+ pass
337+ return json .dumps (obj , separators = (',' , ':' ))
320338 else :
321339 def format_col_item (idx ):
322340 return str (col [idx ])
0 commit comments