6
6
7
7
import os
8
8
import warnings
9
+ from contextlib import suppress
9
10
10
11
import numpy as np
11
12
@@ -331,6 +332,8 @@ def _read_header(cls, fileobj):
331
332
f .seek (1 , os .SEEK_CUR ) # Skip \n
332
333
333
334
found_end = False
335
+ key = None
336
+ tmp_hdr = {}
334
337
335
338
# Read all key-value pairs contained in the header, stop at EOF
336
339
for n_line , line in enumerate (f , 1 ):
@@ -343,15 +346,22 @@ def _read_header(cls, fileobj):
343
346
found_end = True
344
347
break
345
348
346
- if ':' not in line : # Invalid header line
349
+ # Set new key if available, otherwise append to last known key
350
+ with suppress (ValueError ):
351
+ key , line = line .split (':' , 1 )
352
+ key = key .strip ()
353
+
354
+ # Apparent continuation line before any keys are found
355
+ if key is None :
347
356
raise HeaderError (f'Invalid header (line { n_line } ): { line } ' )
348
357
349
- key , value = line .split (':' , 1 )
350
- hdr [key .strip ()] = value .strip ()
358
+ tmp_hdr .setdefault (key , []).append (line .strip ())
351
359
352
360
if not found_end :
353
361
raise HeaderError ('Missing END in the header.' )
354
362
363
+ hdr .update ({key : '\n ' .join (val ) for key , val in tmp_hdr .items ()})
364
+
355
365
offset_data = f .tell ()
356
366
357
367
# Set the file position where it was, in case it was previously open
0 commit comments