[mod_python] Output Filters Redeaux

Deron Meranda deron.meranda at gmail.com
Mon Apr 17 18:03:04 EDT 2006


On 4/17/06, Nick <nick at dd.revealed.net> wrote:
> The iterator would stop when None was read, so that's implicit.  Again, this
> idea assumes a producer/consumer model rather than the repeated calling
> model that is supported now.  And that, of course, implies some kind of
> thread or fork and communication through a pipe or some other form of IPC,
> unless there's some aspect of the apache API that I'm not aware of (which is
> highly possible :)

Not sure why you're thinking about threads and IPC?

Anyway creating a generator-wrapper is the quickest way.  But you
could modify the filter class so it acts like an iterator pretty easy.
Just add a new method something like (this is untested):

   class filter:
      ....
      def __iter__(self):
          class filteriter:
              def __init__(self,f):
                  self.f = f
              def next(self):
                  s = self.f.read()
                  if s is None:
                      raise StopIteration
                  return s
          return filteriter(self)

With a little more work, you could even make the iterator repackage
the data blocks into lines so it acts more like readlines() ... although
that's not a good general solution since not all data will be line-oriented.

And as Graham pointed out, doing the replace '\r\n' as you're
doing it is quite fragile.  It will break when you least expect it to.
--
Deron Meranda



More information about the Mod_python mailing list