[ZPT] CVS: Releases/Zope/lib/python/ZTUtils - Iterator.py:1.5
Evan Simpson
evan@zope.com
Thu, 13 Dec 2001 13:35:32 -0500
Update of /cvs-repository/Releases/Zope/lib/python/ZTUtils
In directory cvs.zope.org:/tmp/cvs-serv21126
Modified Files:
Iterator.py
Log Message:
Make Iterator better at recognizing Python 2.2 iterators.
=== Releases/Zope/lib/python/ZTUtils/Iterator.py 1.4 => 1.5 ===
def __init__(self, seq):
self.seq = seq
- if hasattr(seq, 'next'):
- inner = iterInner
- else:
- inner = seqInner
- self._inner = inner
- self._prep_next = inner.prep_next
+ for inner in seqInner, iterInner:
+ if inner._supports(seq):
+ self._inner = inner
+ self._prep_next = inner.prep_next
+ return
+ raise TypeError, "Iterator does not support %s" % `seq`
def __getattr__(self, name):
try:
@@ -134,6 +134,13 @@
class SeqInner(InnerBase):
'''Inner class for sequence Iterators'''
+
+ def _supports(self, ob):
+ try: ob[0]
+ except TypeError: return 0
+ except: pass
+ return 1
+
def prep_next(self, it):
i = it.nextIndex
try:
@@ -156,6 +163,13 @@
class IterInner(InnerBase):
'''Iterator inner class for Python iterators'''
+
+ def _supports(self, ob):
+ try:
+ if hasattr(ob, 'next') and (ob is iter(ob)):
+ return 1
+ except:
+ return 0
def prep_next(self, it):
try: