comparison roundup/cgi/ZTUtils/Iterator.py @ 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 35ea9b1efc14
children ef6631409171
comparison
equal deleted inserted replaced
5395:23b8e6067f7c 5396:831787cf6694
167 except: 167 except:
168 return 0 168 return 0
169 169
170 def prep_next(self, it): 170 def prep_next(self, it):
171 try: 171 try:
172 it._next = it.seq.next() 172 it._next = next(it.seq)
173 except StopIteration: 173 except StopIteration:
174 it._prep_next = self.no_next 174 it._prep_next = self.no_next
175 it.end = 1 175 it.end = 1
176 return 0 176 return 0
177 it.end = 0 177 it.end = 0
179 179
180 class IterIter: 180 class IterIter:
181 def __init__(self, it): 181 def __init__(self, it):
182 self.it = it 182 self.it = it
183 self.skip = it.nextIndex > 0 and not it.end 183 self.skip = it.nextIndex > 0 and not it.end
184 def next(self): 184 def __next__(self):
185 it = self.it 185 it = self.it
186 if self.skip: 186 if self.skip:
187 self.skip = 0 187 self.skip = 0
188 return it.item 188 return it.item
189 if it.next(): 189 if it.next():
190 return it.item 190 return it.item
191 raise StopIteration 191 raise StopIteration
192 # Python 2 compatibility:
193 next = __next__
192 194
193 seqInner = SeqInner() 195 seqInner = SeqInner()
194 iterInner = IterInner() 196 iterInner = IterInner()

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