From c49d233f0cf527789758295f6a9677f1c1c6d08f Mon Sep 17 00:00:00 2001 From: zhangyunshu Date: Tue, 30 Jun 2020 19:28:36 +0800 Subject: [PATCH] profiler: fixed json decode error in timeline --- mindinsight/profiler/analyser/timeline_analyser.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mindinsight/profiler/analyser/timeline_analyser.py b/mindinsight/profiler/analyser/timeline_analyser.py index e40de69..c266636 100644 --- a/mindinsight/profiler/analyser/timeline_analyser.py +++ b/mindinsight/profiler/analyser/timeline_analyser.py @@ -75,7 +75,7 @@ class TimelineAnalyser(BaseAnalyser): try: with open(file_path, 'r') as f_obj: timeline = json.load(f_obj) - except (IOError, OSError) as err: + except (IOError, OSError, json.JSONDecodeError) as err: logger.error('Error occurred when read timeline display file: %s', err) raise ProfilerIOException else: @@ -104,7 +104,7 @@ class TimelineAnalyser(BaseAnalyser): try: with open(file_path, 'r') as f_obj: timeline_summary = json.load(f_obj) - except (IOError, OSError) as err: + except (IOError, OSError, json.JSONDecodeError) as err: logger.error('Error occurred when read timeline summary file: %s', err) raise ProfilerIOException @@ -128,14 +128,17 @@ class TimelineAnalyser(BaseAnalyser): display_file_path, raise_key='Invalid timeline display json path.' ) + length = len(self._timeline_meta) try: with open(display_file_path, 'w') as json_file: json_file.write('[') - for item in self._timeline_meta: + for index, item in enumerate(self._timeline_meta): json.dump(item, json_file) file_size = os.path.getsize(display_file_path) if file_size > SIZE_LIMIT: break + if index == length - 1: + break json_file.write(',') json_file.write(']') except (IOError, OSError) as err: -- GitLab