On Sun, Jun 06, 2010 at 11:33:20PM +0100, Enrico Zini wrote:

> Now, the WSGI server, even if it supports the file_wrapper
> optimisation[1], will never be able to see that we are returning a
> file_wrapper, so the optimisation will never trigger, in the face of
> what Bottle's documentation says.

The same also defeats the idea that in WSGI, if you return an object
that has a close() method, it will be called at the end of the request.
In this case, Bottle will wrap our returned generator (which has a close
method) in one that doesn't.

Goodbye cleanup after streaming ends.

A possible solution could be, if hasattr(out, 'read'), to skip the
casting altogether:

        # Cast Files into iterables
        if hasattr(out, 'read'):
            if 'wsgi.file_wrapper' in request.environ:
                out = request.environ.get('wsgi.file_wrapper',
                    lambda x, y: iter(lambda: x.read(y), ''))(out, 1024*64)
            return out

And by the way, I see no reason in providing a default value to
environ.get in that piece of code, since one line above we are checking
that the value actually exists.

So, much more simple and readable, and with only one environ lookup:

        # Cast Files into iterables
        if hasattr(out, 'read'):
            wrapper = request.environ.get('wsgi.file_wrapper', None)
            if wrapper: out = wrapper(out)
            return out

This also lets the WSGI server decide the default value for the
blocksize instead of hardcoding an arbitrary one.


Ciao,

Enrico

-- 
GPG key: 4096R/E7AD5568 2009-05-08 Enrico Zini <enr...@enricozini.org>

Attachment: signature.asc
Description: Digital signature

Reply via email to