Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 5D192431FD0 for ; Sat, 23 Jul 2011 06:36:10 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.799 X-Spam-Level: X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id GLzLMHh5wRPN for ; Sat, 23 Jul 2011 06:36:09 -0700 (PDT) Received: from mail-ww0-f45.google.com (mail-ww0-f45.google.com [74.125.82.45]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 695C4431FB6 for ; Sat, 23 Jul 2011 06:36:09 -0700 (PDT) Received: by wwj40 with SMTP id 40so2969398wwj.2 for ; Sat, 23 Jul 2011 06:36:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=B39JvdJ0sZ3A/s+2N6/Lv4ks2V5ZMMpUY0hXQOVkrnc=; b=pJEs4Abucr9mqhHLDbvQp81H53LWBHlXixvFKhII+dVyAHQwGVgISHNUPMGxgSxYWc +08cmtQkkh0rDaS8dvVtCQkk4NYlZt6AzRIVHpCW2Pqcw+GoUF/AMczUkq5/Bh0H6xld +c4v+tQIYZstrohFQyQjowAjJwJu2ACCtUC4c= Received: by 10.216.173.81 with SMTP id u59mr2304759wel.4.1311428168011; Sat, 23 Jul 2011 06:36:08 -0700 (PDT) Received: from brick.lan (cpc1-sgyl2-0-0-cust47.sgyl.cable.virginmedia.com [80.192.18.48]) by mx.google.com with ESMTPS id l61sm1118717wed.28.2011.07.23.06.36.04 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 23 Jul 2011 06:36:05 -0700 (PDT) Date: Sat, 23 Jul 2011 14:36:02 +0100 From: Patrick Totzke To: David Bremner Subject: Re: xapian exceptions not caught in python bindings? Message-ID: <20110723133602.GA30241@brick.lan> References: <20110626202733.GA26837@brick> <20110717193538.GA16615@brick.lan> <87r55o65yq.fsf@zancas.localnet> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="fdj2RfSjLxBAspz7" Content-Disposition: inline In-Reply-To: <87r55o65yq.fsf@zancas.localnet> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: Patrick Totzke , notmuch@notmuchmail.org X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 23 Jul 2011 13:36:10 -0000 --fdj2RfSjLxBAspz7 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi all, I hope the patch I send is correctly formated, I'm still fumbling with git send-email and the --in-reply-to option. Anyhow, forgive my language, of course I didn't mean to be condescending in= any way by calling these prints garbage! It's just that it's highly unusual and= very 'non-pythonic' that a module directly prints to stderr instead of raising e= xceptions and if you work directly with a curseslike interface on a terminal these errormessages litter my screen. The patch I send is a suggestion how to fix the behaviour described in my l= ast post. It introduces a ContextManager class that can be used to raise messages fro= m stderr as Xapian exceptions like this: > from notmuch.globals import RaiseStderrErrors > with RaiseStderrErrors(): > do_stuff() Now, if one executes:=20 ------------ > from notmuch import Database > bad_querystring =3D "test AND" > query =3D Database().create_query(bad_querystring --------------- one gets a nice Xapian exception ----------------------- File "syntax.py", line 4, in query =3D Database().create_query(bad_querystring) File "/usr/local/lib/python2.7/dist-packages/notmuch/database.py", line 4= 32, in create_query return Query(self, querystring) File "/usr/local/lib/python2.7/dist-packages/notmuch/database.py", line 5= 14, in __init__ self.create(db, querystr) File "/usr/local/lib/python2.7/dist-packages/notmuch/database.py", line 5= 47, in create Query._count_messages(self._query) File "/usr/local/lib/python2.7/dist-packages/notmuch/globals.py", line 12= 3, in __exit__ raise NotmuchError(STATUS.XAPIAN_EXCEPTION, message=3Derr) notmuch.globals.NotmuchError: A Xapian exception occurred: Syntax: AND Query string was: test AND ---------------------------- There are two problems with this suggestion however. First, one needs to redirect sdterr to a tempfile: Using StringIO doesn't w= ork since just replacing sys.stderr with something else is not "low level" enough, th= e messages will still get printed. An alternative is using os.dup2, which replaces the file= descriptor of stderr directly, but cStringIO objects dont have .fileno().. see [0,1]. The second problem is, that creating a query object with a malformed querys= tring doesn't print anything to stderr, only if you use that object errors get printed (n= otmuch/lib/query.cc). Thus, I call count_messages() once after initialising a new notmuch.databas= =2EQuery object (at the end of Query.create). This ensures that Query.__in= it__() raises an exception when given bad=20 querystrings, but at the cost of triggering an unnecessary count_messages(). best, /p ---- [0]: http://stackoverflow.com/questions/5903501/attributeerror-stringio-ins= tance-has-no-attribute-fileno [1]: http://code.activestate.com/recipes/577564-context-manager-for-low-lev= el-redirection-of-stdou/ On Sun, Jul 17, 2011 at 04:51:41PM -0300, David Bremner wrote: > On Sun, 17 Jul 2011 20:35:38 +0100, Patrick Totzke wrote: > > If you run the following snippet, you notice that not only do we get > > xapian-garbage on stderr but we don't really get any exceptions at the > > position where it would make sense: >=20 > I wouldn't call that "xapian-garbage" since it is output from > libnotmuch. >=20 > d --fdj2RfSjLxBAspz7 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iEYEARECAAYFAk4qzkIACgkQlDQDZ9fWxar8EACgrSVyAbS67aXjWLzeBpoJuzBb rwYAn0EJ3Sfqsw6zTlew+oGuubWgRuXz =tQEF -----END PGP SIGNATURE----- --fdj2RfSjLxBAspz7--