From patchwork Mon Jan 3 18:34:07 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 448891 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p03IXixr027593 for ; Mon, 3 Jan 2011 18:33:44 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754704Ab1ACSdj (ORCPT ); Mon, 3 Jan 2011 13:33:39 -0500 Received: from mx1.redhat.com ([209.132.183.28]:41461 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754385Ab1ACSdi (ORCPT ); Mon, 3 Jan 2011 13:33:38 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id p03IXbU1019951 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 3 Jan 2011 13:33:37 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p03IXbBM023749; Mon, 3 Jan 2011 13:33:37 -0500 Received: from moof.tlv.redhat.com (dhcp-1-185.tlv.redhat.com [10.35.1.185]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id p03IXWvv027804; Mon, 3 Jan 2011 13:33:35 -0500 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish Subject: [KVM-AUTOTEST PATCH 3/7] [RFC] Introduce exception context strings Date: Mon, 3 Jan 2011 20:34:07 +0200 Message-Id: <1294079651-21631-3-git-send-email-mgoldish@redhat.com> In-Reply-To: <1294079651-21631-1-git-send-email-mgoldish@redhat.com> References: <1294079651-21631-1-git-send-email-mgoldish@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter1.kernel.org [140.211.167.41]); Mon, 03 Jan 2011 18:33:45 +0000 (UTC) diff --git a/client/common_lib/error.py b/client/common_lib/error.py index f1ddaea..394f555 100644 --- a/client/common_lib/error.py +++ b/client/common_lib/error.py @@ -2,13 +2,13 @@ Internal global error types """ -import sys, traceback +import sys, traceback, threading, logging from traceback import format_exception # Add names you want to be imported by 'from errors import *' to this list. # This must be list not a tuple as we modify it to include all of our # the Exception classes we define below at the end of this file. -__all__ = ['format_error'] +__all__ = ['format_error', 'context', 'get_context'] def format_error(): @@ -21,6 +21,84 @@ def format_error(): return ''.join(trace) +# Exception context information: +# ------------------------------ +# Every function can have some context string associated with it. +# The context string can be changed by calling context(str) and cleared by +# calling context() with no parameters. +# get_context() joins the current context strings of all functions in the +# provided traceback. The result is a brief description of what the test was +# doing in the provided traceback (which should be the traceback of a caught +# exception). +# +# For example: assume a() calls b() and b() calls c(). +# +# def a(): +# error.context("hello") +# b() +# error.context("world") +# get_context() ----> 'world' +# +# def b(): +# error.context("foo") +# c() +# +# def c(): +# error.context("bar") +# get_context() ----> 'hello --> foo --> bar' + +context_data = threading.local() +context_data.contexts = {} + + +def context(c=None, log=logging.info): + """ + Set the context for the currently executing function and log it as an INFO + message by default. + + @param c: A string or None. If c is None or an empty string, the context + for the current function is cleared. + @param log: A logging function to pass the string to. If None, no function + will be called. + """ + stack = traceback.extract_stack() + try: + key_parts = ["%s:%s:%s:" % (filename, func, lineno) + for filename, lineno, func, text in stack[:-2]] + filename, lineno, func, text = stack[-2] + key_parts.append("%s:%s" % (filename, func)) + key = "".join(key_parts) + if c: + context_data.contexts[key] = c + if log: + log("Context: %s" % c) + elif key in context_data.contexts: + del context_data.contexts[key] + finally: + # Prevent circular references + del stack + + +def get_context(tb): + """ + Construct a context string from the given traceback. + + @param tb: A traceback object (usually sys.exc_info()[2]). + """ + l = [] + key = "" + for filename, lineno, func, text in traceback.extract_tb(tb): + key += "%s:%s" % (filename, func) + # An exception's traceback is relative to the function that handles it, + # so instead of an exact match, we expect the traceback entries to be + # suffixes of the stack entries recorded using extract_stack(). + for key_ in context_data.contexts: + if key_.endswith(key): + l.append(context_data.contexts[key_]) + key += ":%s:" % lineno + return " --> ".join(l) + + class JobContinue(SystemExit): """Allow us to bail out requesting continuance.""" pass