[Web-SIG] Iterator protocols.

Alan Kennedy py-web-sig at xhaus.com
Mon Aug 30 22:02:57 CEST 2004

[Alan Kennedy]
 >> However, even though jython is based on python 2.1, and thus doesn't
 >> have built-in support for either iterators or generators, I have still
 >> implemented the iterator protocol in my java/jython framework

[Philip J. Eby]
 > Unfortunately, your technique doesn't actually work, unless you're also
 > going to patch the Jython __builtins__ to include 'StopIteration',
 > 'iter', and so forth.  You would have to use the pre-2.2 iteration
 > protocol, which uses __getitem__ and IndexError.  I think this would
 > have to be something you document as a spinoff or "application note" for
 > WSGI users who must use a pre-2.2 version of Python.  One of the reasons
 > we decided to go ahead and require 2.2.2 was to avoid having to deal
 > with the absence of True/False, iterators, and generators.

[Ian Bicking]
 > That's an interesting question.  I guess with both Jython and Zope
 > 2.6 and earlier being Python 2.1, it should be given some consideration.
 > One question: should the application iterable be a Python 2.2 style
 > iterable?  I.e., it is up to Python 2.1 servers to implement the Python
 > 2.2 iterator protocol themselves?  Or, should the application be
 > responsible to return an iterator, appropriate for the Python version?
 > In Python <2.2 (including 1.5.2) the protocol was that you called
 > __getitem__ with ever-increasing integers, until an IndexError was
 > raised.  There was no concept of a special __iter__() function.  But I
 > guess Python 2.2's iter() builtin could be simulated:

Well, now I'm confused :-)

Firstly, my 2.1 implementation of the 2.2 iterator protocol does work, 
because I do create a StopIteration exception and poke it into 
__builtin__. Which isn't the prettiest of approaches, but it works.

I'm currently testing on an application object defined like this:

class handler:

   def __init__(self, environ, start_response):
     start_response("200 OK", [])
     self.i = 0

   def __iter__(self):
     return self

   def next(self):
     if self.i < 6:
       self.i += 1
       return "<h%d>Hello WSGI World!</h%d>\n" % (self.i, self.i)
       raise StopIteration()

And it works as expected: as I expected ;-)

So the two consequent questions I have are

1. Is there something wrong with my approach of defining a StopIteration 
exception, and poking it into __builtin__?

2. Do I need to implement the old pre-2.2 iterator protocol as well? It 
had never occurred to me to implement that: I was focussed only on 2.2 

While we're on the subject of python 2.2 requisites, it's also trivial 
for me to define True and False. Which leaves generators as the only 2.2 
facility I can't do anything about. But since generators are optional 
for application/middleware authors, doesn't that mean that 2.2.2 is not 
required as the minimum version for framework authors, only for 
2.2-dependent components that are plugged into their framework?

Keep up the good work!



More information about the Web-SIG mailing list