From 30812b87536e25cb446e27e01499c1bd7be7ac2b Mon Sep 17 00:00:00 2001
From: Guy Jacob <guy.jacob@intel.com>
Date: Sat, 24 Nov 2018 23:33:07 +0200
Subject: [PATCH] Fix un-handled exception traces showing twice in stdout

---
 examples/classifier_compression/compress_classifier.py | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/examples/classifier_compression/compress_classifier.py b/examples/classifier_compression/compress_classifier.py
index d56c50f..62f1af9 100755
--- a/examples/classifier_compression/compress_classifier.py
+++ b/examples/classifier_compression/compress_classifier.py
@@ -57,6 +57,7 @@ import os
 import sys
 import random
 import traceback
+import logging
 from collections import OrderedDict, defaultdict
 from functools import partial
 import numpy as np
@@ -790,7 +791,14 @@ if __name__ == '__main__':
         print("\n-- KeyboardInterrupt --")
     except Exception as e:
         if msglogger is not None:
+            # We catch unhandled exceptions here in order to log them to the log file
+            # However, using the msglogger as-is to do that means we get the trace twice in stdout - once from the
+            # logging operation and once from re-raising the exception. So we remove the stdout logging handler
+            # before logging the exception
+            handlers_bak = msglogger.handlers
+            msglogger.handlers = [h for h in msglogger.handlers if type(h) != logging.StreamHandler]
             msglogger.error(traceback.format_exc())
+            msglogger.handlers = handlers_bak
         raise
     finally:
         if msglogger is not None:
-- 
GitLab