提交 cc957b38 编写于 作者: C Caio Carrara

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: NCaio Carrara <ccarrara@redhat.com>
上级 fb605738
......@@ -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)
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册