From patchwork Fri Jan 7 18:48:03 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 464971 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 p07Im5pG012313 for ; Fri, 7 Jan 2011 18:48:06 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752579Ab1AGSrx (ORCPT ); Fri, 7 Jan 2011 13:47:53 -0500 Received: from mx1.redhat.com ([209.132.183.28]:31516 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751590Ab1AGSrw (ORCPT ); Fri, 7 Jan 2011 13:47:52 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id p07Ilphi004716 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 7 Jan 2011 13:47:51 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p07IlobG031879; Fri, 7 Jan 2011 13:47:50 -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 p07IlkZJ032671; Fri, 7 Jan 2011 13:47:49 -0500 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish , Eduardo Habkost Subject: [KVM-AUTOTEST PATCH v3 03/11] Introduce exception context strings Date: Fri, 7 Jan 2011 20:48:03 +0200 Message-Id: <1294426091-16704-3-git-send-email-mgoldish@redhat.com> In-Reply-To: <1294426091-16704-1-git-send-email-mgoldish@redhat.com> References: <1294426091-16704-1-git-send-email-mgoldish@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 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.6 (demeter1.kernel.org [140.211.167.41]); Fri, 07 Jan 2011 18:48:06 +0000 (UTC) diff --git a/client/common_lib/error.py b/client/common_lib/error.py index f1ddaea..b0e33b2 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', + 'exception_context'] def format_error(): @@ -21,6 +22,140 @@ 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(). +# +# @error.context_aware +# def a(): +# error.context("hello") +# b() +# error.context("world") +# error.get_context() ----> 'world' +# +# @error.context_aware +# def b(): +# error.context("foo") +# c() +# +# @error.context_aware +# def c(): +# error.context("bar") +# error.get_context() ----> 'hello --> foo --> bar' +# +# The current context is automatically inserted into exceptions raised in +# context_aware functions, so usually test code doesn't need to call +# error.get_context(). + +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 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 base_context(s="", log=None): + """ + Set the base context for the currently executing function and optionally + log it. The base context is just another context level that is hidden by + default. Functions that require a single context level should not use + base_context(). + + @param s: A string. If not provided, the base 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[-2] = s + if s and log: + log("Context: %s" % get_context()) + + +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 exception_context(e): + """Return the context of a given exception (or None if none is defined).""" + if hasattr(e, "_context"): + return e._context + + +def set_exception_context(e, s): + """Set the context of a given exception.""" + e._context = s + + +def join_contexts(s1, s2): + """Join two context strings.""" + if s1: + if s2: + return "%s --> %s" % (s1, s2) + else: + return s1 + else: + return s2 + + +def context_aware(fn): + """A decorator that must be applied to functions that call context().""" + def new_fn(*args, **kwargs): + _new_context() + _new_context("(%s)" % fn.__name__) + try: + try: + return fn(*args, **kwargs) + except Exception, e: + if not exception_context(e): + set_exception_context(e, get_context()) + raise + finally: + _pop_context() + _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 = exception_context(e) + if s: + return " [context: %s]" % s + else: + return "" + + class JobContinue(SystemExit): """Allow us to bail out requesting continuance.""" pass