From cc957b38434bc682126f67fed99b0ae7d7214854 Mon Sep 17 00:00:00 2001 From: Caio Carrara Date: Wed, 5 Sep 2018 15:29:58 -0300 Subject: [PATCH] Removes bare-exception from handle_exception This change updates the handle_exception function so it doesn't need to use a bare-except anymore. The code except responsible for get the crash directory was kept in a separeted function. This specific code snippet can't raise any exception. Internally it uses the avocado.utils.path.usable_rw_dir that already deals with possible exceptions. Another possible point of exception could be the import of avocado.core.data_dir module before avocado be installed itself. However this import was moved to the initial block of import and dealed proper as ImportError. This way seems all the code insisde try block keep being processed by try-except blocks without bare-except like before. Signed-off-by: Caio Carrara --- scripts/avocado | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/scripts/avocado b/scripts/avocado index 01574a8b..a8ebcbe2 100755 --- a/scripts/avocado +++ b/scripts/avocado @@ -17,9 +17,12 @@ import sys try: import os + import time import tempfile import traceback -except: + + from avocado.core import data_dir +except ImportError: sys.stderr.write("Unable to import basic python libraries, please " "reinstall avocado and dependencies.\n") # This exit code is replicated from avocado/core/exit_codes.py and not @@ -27,6 +30,12 @@ except: sys.exit(-1) +def get_crash_dir(): + crash_dir_path = os.path.join(data_dir.get_data_dir(), "crashes") + os.makedirs(crash_dir_path) + return crash_dir_path + + def handle_exception(*exc_info): # Print traceback if AVOCADO_LOG_DEBUG environment variable is set msg = "Avocado crashed:\n" + "".join(traceback.format_exception(*exc_info)) @@ -34,18 +43,9 @@ def handle_exception(*exc_info): if os.environ.get("AVOCADO_LOG_DEBUG"): os.write(2, msg.encode('utf-8')) # Store traceback in data_dir or TMPDIR - crash_dir = None prefix = "avocado-traceback-" - try: - import time - prefix += time.strftime("%F_%T") + "-" - from avocado.core import data_dir # pylint: disable=E0611 - _crash_dir = os.path.join(data_dir.get_data_dir(), "crashes") - os.makedirs(_crash_dir) - crash_dir = _crash_dir - except: - pass - tmp, name = tempfile.mkstemp(".log", prefix, crash_dir) + prefix += time.strftime("%F_%T") + "-" + tmp, name = tempfile.mkstemp(".log", prefix, get_crash_dir()) os.write(tmp, msg.encode('utf-8')) os.close(tmp) # Print friendly message in console-like output -- GitLab