From d830e566e31daeaa2ad2ad16b1a9cd3a69229690 Mon Sep 17 00:00:00 2001 From: Greg Quinn <greg.quinn@ssec.wisc.edu> Date: Wed, 13 Feb 2019 16:12:01 +0000 Subject: [PATCH] Use crio.ReadError for CR input exceptions --- edosl0util/crio.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/edosl0util/crio.py b/edosl0util/crio.py index 776978c..fa0d98b 100644 --- a/edosl0util/crio.py +++ b/edosl0util/crio.py @@ -6,6 +6,10 @@ import numbers from edosl0util.headers import BaseStruct, DaySegmentedTimecode +class ReadError(ValueError): + """Unable to parse construction record due to improper format""" + + def read(cr_file): """Parse a PDS construction record from a file (*00.PDS)""" @@ -42,14 +46,17 @@ def read(cr_file): rv['file_info'].append(d) extra = f.read() if extra: - raise ValueError('{} bytes remain after reading CR'.format(len(extra))) + raise ReadError('{} bytes remain after reading CR'.format(len(extra))) return rv def read_into_dict(f, struct, data): data.update(read_struct(f, struct)) def read_struct(f, struct): - rv = struct_to_dict(struct.from_buffer_copy(f.read(c.sizeof(struct)))) + buf = f.read(c.sizeof(struct)) + if len(buf) < c.sizeof(struct): + raise ReadError('Unexpected EOF reading CR') + rv = struct_to_dict(struct.from_buffer_copy(buf)) rv = {k: v for k, v in rv.items() if not k.startswith('spare_')} # no spare fields return {k: int(v) if isinstance(v, numbers.Integral) else v for k, v in rv.items()} # no longs -- GitLab