From patchwork Wed Jan 5 15:45:26 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 453751 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 p05FjOuC016604 for ; Wed, 5 Jan 2011 15:45:24 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751798Ab1AEPpS (ORCPT ); Wed, 5 Jan 2011 10:45:18 -0500 Received: from mx1.redhat.com ([209.132.183.28]:5663 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751522Ab1AEPpR (ORCPT ); Wed, 5 Jan 2011 10:45:17 -0500 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id p05FjGxx000799 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 5 Jan 2011 10:45:16 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p05FjGO5026222; Wed, 5 Jan 2011 10:45:16 -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 p05FjBQI027486; Wed, 5 Jan 2011 10:45:15 -0500 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish , Eduardo Habkost Subject: [KVM-AUTOTEST PATCH v2 3/6] [RFC] Introduce exception context strings Date: Wed, 5 Jan 2011 17:45:26 +0200 Message-Id: <1294242329-11034-3-git-send-email-mgoldish@redhat.com> In-Reply-To: <1294242329-11034-1-git-send-email-mgoldish@redhat.com> References: <1294242329-11034-1-git-send-email-mgoldish@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 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]); Wed, 05 Jan 2011 15:45:25 +0000 (UTC) diff --git a/client/common_lib/error.py b/client/common_lib/error.py index f1ddaea..952f977 100644 --- a/client/common_lib/error.py +++ b/client/common_lib/error.py @@ -2,13 +2,14 @@ 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_aware', 'context', 'get_context', + 'context_for_exception'] def format_error(): @@ -21,6 +22,98 @@ 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' + +ctx = threading.local() + + +def _new_context(s=""): + if not hasattr(ctx, "contexts"): + ctx.contexts = [] + ctx.contexts.append(s) + + +def _pop_context(): + ctx.contexts.pop() + + +def get_context(): + """Return the current context (or None if none is defined).""" + if hasattr(ctx, "contexts"): + return " --> ".join([s for s in ctx.contexts if s]) + + +def context_for_exception(e): + """Return the context of a given exception (or None if none is defined).""" + if hasattr(e, "_context"): + return e._context + + +def context(s="", log=None): + """ + Set the context for the currently executing function and optionally log it. + + @param s: A string. If not provided, the context for the current function + will be cleared. + @param log: A logging function to pass the context message to. If None, no + function will be called. + """ + ctx.contexts[-1] = s + if s and log: + log("Context: %s" % get_context()) + + +def context_aware(fn): + """A decorator that must be applied to functions that call context().""" + def new_fn(*args, **kwargs): + _new_context("(%s)" % fn.__name__) + try: + try: + return fn(*args, **kwargs) + except Exception, e: + if not hasattr(e, "_context"): + e._context = get_context() + raise + finally: + _pop_context() + new_fn.__name__ = fn.__name__ + new_fn.__doc__ = fn.__doc__ + new_fn.__dict__.update(fn.__dict__) + return new_fn + + +def _context_message(e): + s = context_for_exception(e) + if s: + return " [context: %s]" % s + else: + return "" + + class JobContinue(SystemExit): """Allow us to bail out requesting continuance.""" pass