changeset 5396:831787cf6694

Python 3 preparation: update next() usage for iterators. Tool-assisted patch. Note that various classes in TAL code with next() methods are not actually Python iterators and so are not changed in this patch, but roundup/cgi/ZTUtils/Iterator.py includes the IterIter class which converts between the two styles of iterator.
author Joseph Myers <jsm@polyomino.org.uk>
date Tue, 24 Jul 2018 23:12:32 +0000
parents 23b8e6067f7c
children f2c5e0f6506e
files roundup/cgi/ZTUtils/Iterator.py roundup/mailgw.py roundup/support.py
diffstat 3 files changed, 12 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/roundup/cgi/ZTUtils/Iterator.py	Tue Jul 24 23:04:42 2018 +0000
+++ b/roundup/cgi/ZTUtils/Iterator.py	Tue Jul 24 23:12:32 2018 +0000
@@ -169,7 +169,7 @@
 
     def prep_next(self, it):
         try:
-            it._next = it.seq.next()
+            it._next = next(it.seq)
         except StopIteration:
             it._prep_next = self.no_next
             it.end = 1
@@ -181,7 +181,7 @@
     def __init__(self, it):
         self.it = it
         self.skip = it.nextIndex > 0 and not it.end
-    def next(self):
+    def __next__(self):
         it = self.it
         if self.skip:
             self.skip = 0
@@ -189,6 +189,8 @@
         if it.next():
             return it.item
         raise StopIteration
+    # Python 2 compatibility:
+    next = __next__
 
 seqInner = SeqInner()
 iterInner = IterInner()
--- a/roundup/mailgw.py	Tue Jul 24 23:04:42 2018 +0000
+++ b/roundup/mailgw.py	Tue Jul 24 23:12:32 2018 +0000
@@ -1351,11 +1351,11 @@
             from mailbox import UnixMailbox
             mailbox = UnixMailbox(f, factory=Message)
             # grab one message
-            message = mailbox.next()
+            message = next(mailbox)
             while message:
                 # handle this message
                 self.handle_Message(message)
-                message = mailbox.next()
+                message = next(mailbox)
             # nuke the file contents
             os.ftruncate(f.fileno(), 0)
         except:
--- a/roundup/support.py	Tue Jul 24 23:04:42 2018 +0000
+++ b/roundup/support.py	Tue Jul 24 23:12:32 2018 +0000
@@ -76,19 +76,21 @@
 
     def __iter__(self): return self
 
-    def next(self):
+    def __next__(self):
         self.num += 1
 
         if self.num > self.total:
             print(self.info, 'done', ' '*(75-len(self.info)-6))
             sys.stdout.flush()
-            return self.sequence.next()
+            return next(self.sequence)
 
         if self.num % self.stepsize:
-            return self.sequence.next()
+            return next(self.sequence)
 
         self.display()
-        return self.sequence.next()
+        return next(self.sequence)
+    # Python 2 compatibility:
+    next = __next__
 
     def display(self):
         # figure how long we've spent - guess how long to go

Roundup Issue Tracker: http://roundup-tracker.org/