From: Eduardo Habkost Subject: Re: [Autotest] [KVM-AUTOTEST PATCH 3/7] [RFC] Introduce exception context strings Date: Tue, 4 Jan 2011 13:08:48 -0200 Message-ID: <20110104150848.GQ3361@otherpad.lan.raisama.net> References: <1294079651-21631-1-git-send-email-mgoldish@redhat.com> <1294079651-21631-3-git-send-email-mgoldish@redhat.com> <20110103192620.GJ3361@otherpad.lan.raisama.net> <4D2227F2.7000402@redhat.com> <20110103201622.GL3361@otherpad.lan.raisama.net> <4D223AA9.5030503@redhat.com> <20110103212216.GN3361@otherpad.lan.raisama.net> <20110103220623.GO3361@otherpad.lan.raisama.net> <4D225410.7050300@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: autotest@test.kernel.org, kvm@vger.kernel.org To: Michael Goldish Return-path: Received: from mx1.redhat.com ([209.132.183.28]:20448 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752060Ab1ADPIw (ORCPT ); Tue, 4 Jan 2011 10:08:52 -0500 Content-Disposition: inline In-Reply-To: <4D225410.7050300@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: On Tue, Jan 04, 2011 at 12:56:16AM +0200, Michael Goldish wrote: > On 01/04/2011 12:06 AM, Eduardo Habkost wrote: > > On Mon, Jan 03, 2011 at 07:22:17PM -0200, Eduardo Habkost wrote: [...] > > If you want to be extra careful, you can keep an index of contexts for > > each exception ever seen, and clear it in _call_test_function() code, > > instead of keeping only the last exception. But I think we can keep it > > simple and just store info for the last exception. > > I think that's risky: > > try: > vm.create() # context aware, raises an exception > except: > try: > vm.destroy() # context aware, raises an exception > except: > pass > raise > > The exception raised in VM.destroy() will override last_exception, and > then whoever calls context_for_exception() will get the wrong context. Correct. > But I think this will work and it looks pretty clean: > > def _context_aware(fn, *args, **kwargs): > new_context("") > try: > try: > return fn(*args, **kwargs) > except Exception, e: > if not hasattr(e, "context"): > e.context = get_context() > raise sys.exc_info()[0], e, sys.exc_info()[2] Wouldn't a simple "raise" (without any arguments) work here? > finally: > clear_context() > This way we don't need to keep track of the last exception, and we even > take care of the injection of .context here (instead of in test.py), so > we don't need context_for_exception() at all. > What do you think? Looks good to me. I did the 'is last_exception' trick because I assumed (incorrectly, I guess) I couldn't set arbitrary attributes on Exception objects. I like this solution (as long as you tested it and it works :). > If this makes sense, I'd like to rewrite my patch using this decorator, > unless you feel like doing so yourself. Be my guest. :) -- Eduardo