[Zope-Checkins] CVS: Releases/Zope/lib/python/ZTUtils - Iterator.py:1.9
Evan Simpson
evan@zope.com
Wed, 9 Oct 2002 13:33:50 -0400
Update of /cvs-repository/Releases/Zope/lib/python/ZTUtils
In directory cvs.zope.org:/tmp/cvs-serv12964/lib/python/ZTUtils
Modified Files:
Iterator.py
Log Message:
Close Collector #385, #577 and simplify ZTUtils.Iterator implementation.
=== Releases/Zope/lib/python/ZTUtils/Iterator.py 1.8 => 1.9 ===
--- Releases/Zope/lib/python/ZTUtils/Iterator.py:1.8 Thu Sep 26 13:29:45 2002
+++ Releases/Zope/lib/python/ZTUtils/Iterator.py Wed Oct 9 13:33:49 2002
@@ -29,11 +29,10 @@
nextIndex = 0
def __init__(self, seq):
self.seq = seq
- for inner in seqInner, iterInner:
- if inner._supports(seq):
- self._inner = inner
- self._prep_next = inner.prep_next
- return
+ if iterInner._supports(seq, self):
+ self._inner = iterInner
+ self._prep_next = iterInner.prep_next
+ return
raise TypeError, "Iterator does not support %s" % `seq`
def __getattr__(self, name):
@@ -130,48 +129,19 @@
return 0
return not self.prep_next(it)
-class SeqInner(InnerBase):
- '''Inner class for sequence Iterators'''
-
- def _supports(self, ob):
- try: ob[0]
- except (TypeError, AttributeError): return 0
- except: pass
- return 1
-
- def prep_next(self, it):
- i = it.nextIndex
- try:
- it._next = it.seq[i]
- except IndexError:
- it._prep_next = self.no_next
- it.end = 1
- return 0
- it.end = 0
- return 1
-
- def it_length(self, it):
- it.length = l = len(it.seq)
- return l
-
-try:
- StopIteration=StopIteration
-except NameError:
- StopIteration="StopIteration"
-
class IterInner(InnerBase):
- '''Iterator inner class for Python iterators'''
+ '''Iterator inner class for objects with Python iterators'''
- def _supports(self, ob):
+ def _supports(self, ob, it):
try:
- if hasattr(ob, 'next') and (ob is iter(ob)):
- return 1
+ it.iter = iter(ob)
+ return 1
except:
return 0
def prep_next(self, it):
try:
- it._next = it.seq.next()
+ it._next = it.iter.next()
except StopIteration:
it._prep_next = self.no_next
it.end = 1
@@ -192,5 +162,4 @@
return it.item
raise StopIteration
-seqInner = SeqInner()
iterInner = IterInner()