From: Jan Malakhovski Date: Fri, 31 Jul 2015 01:36:26 +0000 (+0000) Subject: [PATCH] python: fix get_filenames() and make it actually usable X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=2b09d9554f02b3e48bb54668172ba60698673594;p=notmuch-archives.git [PATCH] python: fix get_filenames() and make it actually usable --- diff --git a/6c/4e4dd8a4b6f06d0bb9044e96e2910e4407ee75 b/6c/4e4dd8a4b6f06d0bb9044e96e2910e4407ee75 new file mode 100644 index 000000000..b25370ad4 --- /dev/null +++ b/6c/4e4dd8a4b6f06d0bb9044e96e2910e4407ee75 @@ -0,0 +1,166 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by arlo.cworth.org (Postfix) with ESMTP id CFDBF6DE0B44 + for ; Thu, 30 Jul 2015 18:44:05 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at cworth.org +X-Spam-Flag: NO +X-Spam-Score: -0.001 +X-Spam-Level: +X-Spam-Status: No, score=-0.001 tagged_above=-999 required=5 + tests=[SPF_PASS=-0.001] autolearn=disabled +Received: from arlo.cworth.org ([127.0.0.1]) + by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id LrlbbbVVE8Wv for ; + Thu, 30 Jul 2015 18:44:03 -0700 (PDT) +X-Greylist: delayed 437 seconds by postgrey-1.35 at arlo; + Thu, 30 Jul 2015 18:44:02 PDT +Received: from tricoro.koumakan.jp (tricoro.koumakan.jp [195.154.188.176]) + by arlo.cworth.org (Postfix) with ESMTPS id BD9DD6DE0B20 + for ; Thu, 30 Jul 2015 18:44:02 -0700 (PDT) +Received: from localhost (ppp91-122-53-24.pppoe.avangarddsl.ru [91.122.53.24]) + (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 + bits)) (No client certificate requested) + by tricoro.koumakan.jp (Postfix) with ESMTPSA id 9F1F6103D; + Fri, 31 Jul 2015 10:36:41 +0900 (JST) +From: Jan Malakhovski +To: notmuch@notmuchmail.org +Subject: [PATCH] python: fix get_filenames() and make it actually usable +Date: Fri, 31 Jul 2015 01:36:26 +0000 +Message-Id: <1438306586-7597-1-git-send-email-oxij@oxij.org> +X-Mailer: git-send-email 2.4.1 +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.18 +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: Fri, 31 Jul 2015 01:44:05 -0000 + +The problem with the previous implementation is that different +versions of python exhaust __iter__() differently and the +implementation that can be exhausted is not only absolutely unusable +in the user code, but it also can not be consistently used with both +python 2.* and 3.*. + +This doesn't change the interface. +--- + bindings/python/notmuch/filenames.py | 84 +++++++++++------------------------- + 1 file changed, 26 insertions(+), 58 deletions(-) + +diff --git a/bindings/python/notmuch/filenames.py b/bindings/python/notmuch/filenames.py +index 229f414..e2b8886 100644 +--- a/bindings/python/notmuch/filenames.py ++++ b/bindings/python/notmuch/filenames.py +@@ -65,6 +65,18 @@ class Filenames(Python3StringMixIn): + _get.argtypes = [NotmuchFilenamesP] + _get.restype = c_char_p + ++ _valid = nmlib.notmuch_filenames_valid ++ _valid.argtypes = [NotmuchFilenamesP] ++ _valid.restype = bool ++ ++ _move_to_next = nmlib.notmuch_filenames_move_to_next ++ _move_to_next.argtypes = [NotmuchFilenamesP] ++ _move_to_next.restype = None ++ ++ _destroy = nmlib.notmuch_filenames_destroy ++ _destroy.argtypes = [NotmuchFilenamesP] ++ _destroy.restype = None ++ + def __init__(self, files_p, parent): + """ + :param files_p: A pointer to an underlying *notmuch_tags_t* +@@ -83,68 +95,24 @@ class Filenames(Python3StringMixIn): + if not files_p: + raise NullPointerError() + +- self._files_p = files_p ++ self._list = [] ++ while self._valid(files_p): ++ file_ = self._get(files_p) ++ self._move_to_next(files_p) ++ self._list.append(file_.decode('utf-8', 'ignore')) ++ self._destroy(files_p) ++ + #save reference to parent object so we keep it alive + self._parent = parent + + def __iter__(self): +- """ Make Filenames an iterator """ +- return self +- +- _valid = nmlib.notmuch_filenames_valid +- _valid.argtypes = [NotmuchFilenamesP] +- _valid.restype = bool +- +- _move_to_next = nmlib.notmuch_filenames_move_to_next +- _move_to_next.argtypes = [NotmuchFilenamesP] +- _move_to_next.restype = None +- +- def __next__(self): +- if not self._files_p: +- raise NotInitializedError() +- +- if not self._valid(self._files_p): +- self._files_p = None +- raise StopIteration +- +- file_ = Filenames._get(self._files_p) +- self._move_to_next(self._files_p) +- return file_.decode('utf-8', 'ignore') +- next = __next__ # python2.x iterator protocol compatibility +- +- def __unicode__(self): +- """Represent Filenames() as newline-separated list of full paths +- +- .. note:: +- +- This method exhausts the iterator object, so you will not be able to +- iterate over them again. +- """ +- return "\n".join(self) +- +- _destroy = nmlib.notmuch_filenames_destroy +- _destroy.argtypes = [NotmuchMessageP] +- _destroy.restype = None +- +- def __del__(self): +- """Close and free the notmuch filenames""" +- if self._files_p: +- self._destroy(self._files_p) ++ """Make Filenames an iterator""" ++ return self._list.__iter__() + + def __len__(self): +- """len(:class:`Filenames`) returns the number of contained files ++ """len(:class:`Filenames`) returns the number of contained files""" ++ return self._list.__len__() + +- .. note:: +- +- This method exhausts the iterator object, so you will not be able to +- iterate over them again. +- """ +- if not self._files_p: +- raise NotInitializedError() +- +- i = 0 +- while self._valid(self._files_p): +- self._move_to_next(self._files_p) +- i += 1 +- self._files_p = None +- return i ++ def __unicode__(self): ++ """Represent Filenames() as newline-separated list of full paths""" ++ return "\n".join(self) +-- +2.4.1 +