]> git.notmuchmail.org Git - notmuch/commitdiff
python: Remove {Filenames,Threads}.__len__
authorJustus Winter <4winter@informatik.uni-hamburg.de>
Fri, 8 Jan 2016 11:27:20 +0000 (12:27 +0100)
committerJustus Winter <4winter@informatik.uni-hamburg.de>
Fri, 8 Jan 2016 12:58:33 +0000 (13:58 +0100)
Remove the __len__ functions, as they exhaust the iterator, breaking
'list(x)'.

This is a follow-up to 8866a89e.

Signed-off-by: Justus Winter <4winter@informatik.uni-hamburg.de>
bindings/python/docs/source/filesystem.rst
bindings/python/docs/source/threads.rst
bindings/python/notmuch/filenames.py
bindings/python/notmuch/threads.py

index 4eb781077940046a3e57e50d9f0969cb44806dd4..a23ae41a875685214c45a57bfb41720e29f3538f 100644 (file)
@@ -8,7 +8,11 @@ Files and directories
 
 .. autoclass:: Filenames
 
-   .. automethod:: Filenames.__len__
+   .. method:: Filenames.__len__
+   .. warning::
+      :meth:`__len__` was removed in version 0.22 as it exhausted the
+      iterator and broke list(Filenames()). Use `len(list(names))`
+      instead.
 
 :class:`Directoy` -- A directory entry in the database
 ------------------------------------------------------
index e5a8c8a95d1861ba755c34e7cd7c643a13836aaa..4324ac82a389611d8a633c65f015dabb7c4d4aa0 100644 (file)
@@ -5,6 +5,10 @@
 
 .. autoclass:: Threads
 
-   .. automethod:: __len__
+   .. method:: __len__
+   .. warning::
+      :meth:`__len__` was removed in version 0.22 as it exhausted the
+      iterator and broke list(Threads()). Use `len(list(msgs))`
+      instead.
 
-   .. automethod:: __str__
+.. automethod:: __str__
index 96b22c65360d2bee6f2748c8e00f033079f1a28d..9e5d1a30f744749e8aa0bf1ba8dba4c6aae824cb 100644 (file)
@@ -48,7 +48,7 @@ class Filenames(Python3StringMixIn):
 
         as well as::
 
-           number_of_names = len(names)
+           list_of_names = list(names)
 
         and even a simple::
 
@@ -130,21 +130,3 @@ class Filenames(Python3StringMixIn):
         """Close and free the notmuch filenames"""
         if self._files_p:
             self._destroy(self._files_p)
-
-    def __len__(self):
-        """len(:class:`Filenames`) returns the number of contained files
-
-        .. 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
index f8ca34a9b14cc0f801a019848d4177a836cfc1a0..a550523fae8902636ddbb9c141ea53660158503f 100644 (file)
@@ -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.
@@ -132,30 +131,6 @@ 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):
         '''
         Implement truth value testing. If __nonzero__ is not