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 24044431FD0 for ; Sat, 23 Jul 2011 06:12:59 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" 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 Vzw8zLDDSchG for ; Sat, 23 Jul 2011 06:12:57 -0700 (PDT) Received: from mail-wy0-f181.google.com (mail-wy0-f181.google.com [74.125.82.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 9A710431FB6 for ; Sat, 23 Jul 2011 06:12:56 -0700 (PDT) Received: by wyh22 with SMTP id 22so2327529wyh.26 for ; Sat, 23 Jul 2011 06:12:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :in-reply-to:references; bh=CjckEnceHtxo/0XwRNhzNET2z4Lhakgf+cqrhkCOV70=; b=TQmTPYpn8qGK6NIl3Go2htxPv0KvYp9e8rkaR+B7fwk82RjKW2PTEemnJxqsDIwltM odP2Rkx5jNA546ozIKn+xIGWGiE3B5nGFQCNjPisu8d9i8LDEwYrJEPQmqBzLug6WNuF dmdDWObBKp4mTLycAisIPMW2LFZIymIAZnUyo= Received: by 10.227.6.18 with SMTP id 18mr2307644wbx.66.1311426775086; Sat, 23 Jul 2011 06:12:55 -0700 (PDT) Received: from localhost (cpc1-sgyl2-0-0-cust47.sgyl.cable.virginmedia.com [80.192.18.48]) by mx.google.com with ESMTPS id gb1sm2710367wbb.37.2011.07.23.06.12.53 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 23 Jul 2011 06:12:54 -0700 (PDT) From: pazz To: notmuch@notmuchmail.org Subject: [PATCH] interpret Xapian errors from sdterr as exceptions Date: Sat, 23 Jul 2011 14:12:39 +0100 Message-Id: <1311426759-32441-1-git-send-email-patricktotzke@gmail.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <87r55o65yq.fsf@zancas.localnet> References: <87r55o65yq.fsf@zancas.localnet> In-Reply-To: <87r55o65yq.fsf@zancas.localnet> References: <87r55o65yq.fsf@zancas.localnet> 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:12:59 -0000 This introduces globals.RaiseStderrErrors, a ContextManager that raises error messages printed by libnotmuch to stderr as NotmuchError(STATUS.XAPIAN_EXCEPTION, message=err). --- bindings/python/notmuch/database.py | 5 +++++ bindings/python/notmuch/globals.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 0 deletions(-) diff --git a/bindings/python/notmuch/database.py b/bindings/python/notmuch/database.py index 874087e..443980b 100644 --- a/bindings/python/notmuch/database.py +++ b/bindings/python/notmuch/database.py @@ -18,8 +18,10 @@ Copyright 2010 Sebastian Spaeth ' """ import os + from ctypes import c_int, c_char_p, c_void_p, c_uint, c_long, byref from notmuch.globals import nmlib, STATUS, NotmuchError, Enum +from notmuch.globals import RaiseStderrErrors from notmuch.thread import Threads from notmuch.message import Messages, Message from notmuch.tag import Tags @@ -540,6 +542,9 @@ class Query(object): if query_p is None: NotmuchError(STATUS.NULL_POINTER) self._query = query_p + # ensure Xapian errors from stderr get raised if query syntax is bad + with RaiseStderrErrors(): + Query._count_messages(self._query) def set_sort(self, sort): """Set the sort order future results will be delivered in diff --git a/bindings/python/notmuch/globals.py b/bindings/python/notmuch/globals.py index 77f2905..5e527ca 100644 --- a/bindings/python/notmuch/globals.py +++ b/bindings/python/notmuch/globals.py @@ -17,6 +17,10 @@ along with notmuch. If not, see . Copyright 2010 Sebastian Spaeth ' """ +import tempfile +import sys +import os + from ctypes import CDLL, c_char_p, c_int from ctypes.util import find_library @@ -98,3 +102,23 @@ class NotmuchError(Exception): return self.args[0] else: return STATUS.status2str(self.args[1]) + + +class RaiseStderrErrors: + def __enter__(self): + sys.stderr.flush() + (self.errfd, fn) = tempfile.mkstemp() + self.ferr = os.fdopen(self.errfd, 'r') + os.unlink(fn) + self.oldstderr = os.dup(sys.stderr.fileno()) + os.dup2(self.errfd, sys.stderr.fileno()) + + def __exit__(self, *args): + sys.stderr.flush() + os.dup2(self.oldstderr, sys.stderr.fileno()) + os.close(self.oldstderr) + os.lseek(self.errfd, 0, 0) + err = self.ferr.read() + if err: + raise NotmuchError(STATUS.XAPIAN_EXCEPTION, message=err) + self.ferr.close() -- 1.7.4.1