X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=bindings%2Fpython%2Fnotmuch%2Fthreads.py;h=0c382d5b8f187213b26fabd45c2c76486b7f5a01;hp=fb053f939de64ca42b968571ab56a68fc0e36ec3;hb=HEAD;hpb=1f08664a6b8f7cba63f63855833f877b66bbbe05 diff --git a/bindings/python/notmuch/threads.py b/bindings/python/notmuch/threads.py index fb053f93..0c382d5b 100644 --- a/bindings/python/notmuch/threads.py +++ b/bindings/python/notmuch/threads.py @@ -12,12 +12,12 @@ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License -along with notmuch. If not, see . +along with notmuch. If not, see . Copyright 2010 Sebastian Spaeth """ -from notmuch.globals import ( +from .globals import ( nmlib, Python3StringMixIn, NotmuchThreadP, @@ -46,7 +46,7 @@ class Threads(Python3StringMixIn): as well as:: - number_of_msgs = len(threads) + list_of_threads = list(threads) will "exhaust" the threads. If you need to re-iterate over a list of messages you will need to retrieve a new :class:`Threads` object. @@ -64,8 +64,7 @@ class Threads(Python3StringMixIn): for thread in threads: threadlist.append(thread) - # threads is "exhausted" now and even len(threads) will raise an - # exception. + # threads is "exhausted" now. # However it will be kept around until all retrieved Thread() objects are # also deleted. If you did e.g. an explicit del(threads) here, the # following lines would fail. @@ -86,7 +85,7 @@ class Threads(Python3StringMixIn): def __init__(self, threads_p, parent=None): """ :param threads_p: A pointer to an underlying *notmuch_threads_t* - structure. These are not publically exposed, so a user + structure. These are not publicly exposed, so a user will almost never instantiate a :class:`Threads` object herself. They are usually handed back as a result, e.g. in :meth:`Query.search_threads`. *threads_p* must be @@ -132,43 +131,16 @@ class Threads(Python3StringMixIn): return thread next = __next__ # python2.x iterator protocol compatibility - def __len__(self): - """len(:class:`Threads`) returns the number of contained Threads - - .. note:: As this iterates over the threads, we will not be able to - iterate over them again! So this will fail:: - - #THIS FAILS - threads = Database().create_query('').search_threads() - if len(threads) > 0: #this 'exhausts' threads - # next line raises :exc:`NotInitializedError`!!! - for thread in threads: print thread - """ - if not self._threads: - raise NotInitializedError() - - i = 0 - # returns 'bool'. On out-of-memory it returns None - while self._valid(self._threads): - self._move_to_next(self._threads) - i += 1 - # reset self._threads to mark as "exhausted" - self._threads = None - return i - def __nonzero__(self): - """Check if :class:`Threads` contains at least one more valid thread + ''' + Implement truth value testing. If __nonzero__ is not + implemented, the python runtime would fall back to `len(..) > + 0` thus exhausting the iterator. - The existence of this function makes 'if Threads: foo' work, as - that will implicitely call len() exhausting the iterator if - __nonzero__ does not exist. This function makes `bool(Threads())` - work repeatedly. - - :return: True if there is at least one more thread in the - Iterator, False if not. None on a "Out-of-memory" error. - """ - return self._threads is not None and \ - self._valid(self._threads) > 0 + :returns: True if the wrapped iterator has at least one more object + left. + ''' + return self._threads and self._valid(self._threads) _destroy = nmlib.notmuch_threads_destroy _destroy.argtypes = [NotmuchThreadsP] @@ -176,5 +148,5 @@ class Threads(Python3StringMixIn): def __del__(self): """Close and free the notmuch Threads""" - if self._threads is not None: + if self._threads: self._destroy(self._threads)