unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Patrick Totzke <patricktotzke@googlemail.com>
To: David Bremner <david@tethera.net>
Cc: Patrick Totzke <patricktotzke@googlemail.com>, notmuch@notmuchmail.org
Subject: Re: xapian exceptions not caught in python bindings?
Date: Sat, 23 Jul 2011 14:36:02 +0100	[thread overview]
Message-ID: <20110723133602.GA30241@brick.lan> (raw)
In-Reply-To: <87r55o65yq.fsf@zancas.localnet>

[-- Attachment #1: Type: text/plain, Size: 3340 bytes --]

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 exceptions
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 last post.
It introduces a ContextManager class that can be used to raise messages from stderr
as Xapian exceptions like this:

> from notmuch.globals import RaiseStderrErrors
> with RaiseStderrErrors():
>  do_stuff()

Now, if one executes: 
------------
> from notmuch import Database
> bad_querystring = "test AND"
> query = Database().create_query(bad_querystring
---------------
one gets a nice Xapian exception
-----------------------
  File "syntax.py", line 4, in <module>
    query = Database().create_query(bad_querystring)
  File "/usr/local/lib/python2.7/dist-packages/notmuch/database.py", line 432, in create_query
    return Query(self, querystring)
  File "/usr/local/lib/python2.7/dist-packages/notmuch/database.py", line 514, in __init__
    self.create(db, querystr)
  File "/usr/local/lib/python2.7/dist-packages/notmuch/database.py", line 547, in create
    Query._count_messages(self._query)
  File "/usr/local/lib/python2.7/dist-packages/notmuch/globals.py", line 123, in __exit__
    raise NotmuchError(STATUS.XAPIAN_EXCEPTION, message=err)
notmuch.globals.NotmuchError: A Xapian exception occurred: Syntax: <expression> AND <expression>
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 work since
just replacing sys.stderr with something else is not "low level" enough, the 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 querystring doesn't
print anything to stderr, only if you use that object errors get printed (notmuch/lib/query.cc).
Thus, I call count_messages() once after initialising a new notmuch.databas.Query object (at the end of  Query.create). This ensures that Query.__init__() raises an exception when given bad 
querystrings, but at the cost of triggering an unnecessary count_messages().

best,
/p



----
[0]: http://stackoverflow.com/questions/5903501/attributeerror-stringio-instance-has-no-attribute-fileno
[1]: http://code.activestate.com/recipes/577564-context-manager-for-low-level-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 <patricktotzke@googlemail.com> 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:
> 
> I wouldn't call that "xapian-garbage" since it is output from
> libnotmuch.
> 
> d

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

  parent reply	other threads:[~2011-07-23 13:36 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-26 20:27 xapian exceptions not caught in python bindings? Patrick Totzke
2011-07-17 19:35 ` Patrick Totzke
2011-07-17 19:51   ` David Bremner
2011-07-23 13:12     ` [PATCH] interpret Xapian errors from sdterr as exceptions pazz
2011-07-23 13:36     ` Patrick Totzke [this message]
2011-08-09 15:00       ` xapian exceptions not caught in python bindings? Sebastian Spaeth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110723133602.GA30241@brick.lan \
    --to=patricktotzke@googlemail.com \
    --cc=david@tethera.net \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).