[PATCH 1/2] test: date_relative in notmuch search json output

2011-07-23 Thread pazz
expect the date_relative field for thread entries
in notmuch search's json output
---
 test/json |2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/test/json b/test/json
index 5a2544c..d54bf25 100755
--- a/test/json
+++ b/test/json
@@ -12,6 +12,7 @@ add_message "[subject]=\"json-search-subject\"" 
"[date]=\"Sat, 01 Jan 2000 12:00
 output=$(notmuch search --format=json "json-search-message" | 
notmuch_search_sanitize)
 test_expect_equal "$output" "[{\"thread\": \"XXX\",
 \"timestamp\": 946728000,
+\"date_relative\": \"2000-01-01\",
 \"matched\": 1,
 \"total\": 1,
 \"authors\": \"Notmuch Test Suite\",
@@ -28,6 +29,7 @@ add_message "[subject]=\"json-search-utf8-body-s?bj?ct\"" 
"[date]=\"Sat, 01 Ja
 output=$(notmuch search --format=json "js?n-search-m?ssage" | 
notmuch_search_sanitize)
 test_expect_equal "$output" "[{\"thread\": \"XXX\",
 \"timestamp\": 946728000,
+\"date_relative\": \"2000-01-01\",
 \"matched\": 1,
 \"total\": 1,
 \"authors\": \"Notmuch Test Suite\",
-- 
1.7.4.1



[PATCH 2/2] json: date_relative for threads

2011-07-23 Thread pazz
include the date_relative field in the
json formated output of notmuch search
---
 notmuch-search.c |2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/notmuch-search.c b/notmuch-search.c
index faccaf7..b1adc03 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -176,12 +176,14 @@ format_thread_json (const void *ctx,

 printf ("\"thread\": %s,\n"
"\"timestamp\": %ld,\n"
+   "\"date_relative\": \"%s\",\n"
"\"matched\": %d,\n"
"\"total\": %d,\n"
"\"authors\": %s,\n"
"\"subject\": %s,\n",
json_quote_str (ctx_quote, thread_id),
date,
+   notmuch_time_relative_date (ctx, date),
matched,
total,
json_quote_str (ctx_quote, authors),
-- 
1.7.4.1



[PATCH] interpret Xapian errors from sdterr as exceptions

2011-07-23 Thread pazz
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



xapian exceptions not caught in python bindings?

2011-07-23 Thread Patrick Totzke
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 
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:  
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 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  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
-- next part --
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: 
<http://notmuchmail.org/pipermail/notmuch/attachments/20110723/ff6685b7/attachment.pgp>


segfault: g_type_init() missing

2011-07-23 Thread Jameson Graef Rollins
On Sun, 24 Jul 2011 00:32:39 -0400, Aaron Ecay  wrote:
> According to the Glib docs
> (http://developer.gnome.org/gobject/unstable/gobject-Type-Information.html#g-type-init),
> the g_type_init() function must be called before using any GType
> stuff, which notmuch_filter_discard_uuencode_new does.  There are no
> grep hits for g_type_init in the notmuch sources, though.  The code on
> the error path looks pretty old, so I'm not sure why this bug hasn't
> hit someone else before.  Adding a call to g_type_init() to main()
> (right after g_mime_init(0)) fixes the problem.

Hey, Aaron.  Since you've found a suitable fix, would you mind providing
a patch (from git send-email for instance)?  Simple bug fixes tend to
get applied fairly quickly if well formatted patches are included.
Thanks.

jamie.
-- next part --
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: 
<http://notmuchmail.org/pipermail/notmuch/attachments/20110723/71a763e3/attachment.pgp>


[PATCH 1/2] test: date_relative in notmuch search json output

2011-07-23 Thread pazz
expect the date_relative field for thread entries
in notmuch search's json output
---
 test/json |2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/test/json b/test/json
index 5a2544c..d54bf25 100755
--- a/test/json
+++ b/test/json
@@ -12,6 +12,7 @@ add_message "[subject]=\"json-search-subject\"" 
"[date]=\"Sat, 01 Jan 2000 12:00
 output=$(notmuch search --format=json "json-search-message" | 
notmuch_search_sanitize)
 test_expect_equal "$output" "[{\"thread\": \"XXX\",
 \"timestamp\": 946728000,
+\"date_relative\": \"2000-01-01\",
 \"matched\": 1,
 \"total\": 1,
 \"authors\": \"Notmuch Test Suite\",
@@ -28,6 +29,7 @@ add_message "[subject]=\"json-search-utf8-body-sübjéct\"" 
"[date]=\"Sat, 01 Ja
 output=$(notmuch search --format=json "jsön-search-méssage" | 
notmuch_search_sanitize)
 test_expect_equal "$output" "[{\"thread\": \"XXX\",
 \"timestamp\": 946728000,
+\"date_relative\": \"2000-01-01\",
 \"matched\": 1,
 \"total\": 1,
 \"authors\": \"Notmuch Test Suite\",
-- 
1.7.4.1

___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


[PATCH 2/2] json: date_relative for threads

2011-07-23 Thread pazz
include the date_relative field in the
json formated output of notmuch search
---
 notmuch-search.c |2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/notmuch-search.c b/notmuch-search.c
index faccaf7..b1adc03 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -176,12 +176,14 @@ format_thread_json (const void *ctx,
 
 printf ("\"thread\": %s,\n"
"\"timestamp\": %ld,\n"
+   "\"date_relative\": \"%s\",\n"
"\"matched\": %d,\n"
"\"total\": %d,\n"
"\"authors\": %s,\n"
"\"subject\": %s,\n",
json_quote_str (ctx_quote, thread_id),
date,
+   notmuch_time_relative_date (ctx, date),
matched,
total,
json_quote_str (ctx_quote, authors),
-- 
1.7.4.1

___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


[PATCH] interpret Xapian errors from sdterr as exceptions

2011-07-23 Thread pazz
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

___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


Re: xapian exceptions not caught in python bindings?

2011-07-23 Thread Patrick Totzke
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 
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:  
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 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 
>  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


signature.asc
Description: Digital signature
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


segfault: g_type_init() missing

2011-07-23 Thread Aaron Ecay
Dear all,

I just pulled the last couple weeks' worth of changes and rebuilt
notmuch, only to have the new binary give a segfault on "notmuch new"
(running on OS X.6).  The error message printed was:

(process:21821): GLib-GObject-CRITICAL **: gtype.c:2708: You forgot to
call g_type_init()

And the backtrace (from gdb):

#0  0x000a18f7 in g_object_newv ()
#1  0x000160e4 in notmuch_filter_discard_uuencode_new ()
#2  0x000163f0 in _index_mime_part ()
#3  0x0001667a in _notmuch_message_index_file ()
#4  0x0001247c in notmuch_database_add_message ()
#5  0x65a9 in add_files_recursive ()
#6  0x612c in add_files_recursive ()
#7  0x612c in add_files_recursive ()
#8  0x71ec in notmuch_new_command ()
#9  0x3a5a in main ()

According to the Glib docs
(http://developer.gnome.org/gobject/unstable/gobject-Type-Information.html#g-type-init),
the g_type_init() function must be called before using any GType
stuff, which notmuch_filter_discard_uuencode_new does.  There are no
grep hits for g_type_init in the notmuch sources, though.  The code on
the error path looks pretty old, so I'm not sure why this bug hasn't
hit someone else before.  Adding a call to g_type_init() to main()
(right after g_mime_init(0)) fixes the problem.

Aaron
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


Re: segfault: g_type_init() missing

2011-07-23 Thread Jameson Graef Rollins
On Sun, 24 Jul 2011 00:32:39 -0400, Aaron Ecay  wrote:
> According to the Glib docs
> (http://developer.gnome.org/gobject/unstable/gobject-Type-Information.html#g-type-init),
> the g_type_init() function must be called before using any GType
> stuff, which notmuch_filter_discard_uuencode_new does.  There are no
> grep hits for g_type_init in the notmuch sources, though.  The code on
> the error path looks pretty old, so I'm not sure why this bug hasn't
> hit someone else before.  Adding a call to g_type_init() to main()
> (right after g_mime_init(0)) fixes the problem.

Hey, Aaron.  Since you've found a suitable fix, would you mind providing
a patch (from git send-email for instance)?  Simple bug fixes tend to
get applied fairly quickly if well formatted patches are included.
Thanks.

jamie.


pgpo6FqNqjDGI.pgp
Description: PGP signature
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch