[Zope-dev] SVN: Zope/trunk/ Adding support for ``IStreamIterator`` to WSGI publishing machinery.

Tres Seaver tseaver at palladion.com
Sat Mar 26 13:14:49 EDT 2011


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 03/26/2011 12:53 PM, Malthe Borch wrote:
> Log message for revision 121131:
>   Adding support for ``IStreamIterator`` to WSGI publishing machinery.
> 
> Changed:
>   U   Zope/trunk/doc/CHANGES.rst
>   U   Zope/trunk/src/ZPublisher/WSGIPublisher.py
>   U   Zope/trunk/src/ZPublisher/tests/test_WSGIPublisher.py
> 
> -=-
> Modified: Zope/trunk/doc/CHANGES.rst
> ===================================================================
> --- Zope/trunk/doc/CHANGES.rst	2011-03-25 17:39:14 UTC (rev 121130)
> +++ Zope/trunk/doc/CHANGES.rst	2011-03-26 16:53:52 UTC (rev 121131)
> @@ -11,6 +11,10 @@
>  Bugs Fixed
>  ++++++++++
>  
> +- Fix `WSGIResponse` and `publish_module` functions such that they
> +  support the `IStreamIterator` interface in addition to `file` (as
> +  supported by `ZServer.HTTPResponse`).
> +
>  - Made sure getConfiguration().default_zpublisher_encoding is set correctly.
>  
>  - LP #713253: Prevent publication of acquired attributes, where the acquired
> 
> Modified: Zope/trunk/src/ZPublisher/WSGIPublisher.py
> ===================================================================
> --- Zope/trunk/src/ZPublisher/WSGIPublisher.py	2011-03-25 17:39:14 UTC (rev 121130)
> +++ Zope/trunk/src/ZPublisher/WSGIPublisher.py	2011-03-26 16:53:52 UTC (rev 121131)
> @@ -30,6 +30,7 @@
>  from ZPublisher.Publish import dont_publish_class
>  from ZPublisher.Publish import get_module_info
>  from ZPublisher.Publish import missing_name
> +from ZPublisher.Iterators import IStreamIterator
>  
>  _NOW = None     # overwrite for testing
>  def _now():
> @@ -125,7 +126,7 @@
>          self.stdout.write(data)
>  
>      def setBody(self, body, title='', is_error=0):
> -        if isinstance(body, file):
> +        if isinstance(body, file) or IStreamIterator.providedBy(body):
>              body.seek(0, 2)
>              length = body.tell()
>              body.seek(0)

This part of the patch can't possibly work in the general case:  nothing
in IStreamIterator promises that 'seek' and 'tell' are available.

> @@ -226,8 +227,10 @@
>      status, headers = response.finalize()
>      start_response(status, headers)
>  
> -    if isinstance(response.body, file):
> -        result = response.body
> +    body = response.body
> +
> +    if isinstance(body, file) or IStreamIterator.providedBy(body):
> +        result = body
>      else:
>          # If somebody used response.write, that data will be in the
>          # stdout StringIO, so we put that before the body.
> 
> Modified: Zope/trunk/src/ZPublisher/tests/test_WSGIPublisher.py
> ===================================================================
> --- Zope/trunk/src/ZPublisher/tests/test_WSGIPublisher.py	2011-03-25 17:39:14 UTC (rev 121130)
> +++ Zope/trunk/src/ZPublisher/tests/test_WSGIPublisher.py	2011-03-26 16:53:52 UTC (rev 121131)
> @@ -370,6 +370,32 @@
>          app_iter = self._callFUT(environ, start_response, _publish)
>          self.assertTrue(app_iter is body)
>  
> +    def test_response_is_stream(self):
> +        from ZPublisher.Iterators import IStreamIterator
> +        from zope.interface import implements
> +
> +        class test_streamiterator:
> +            implements(IStreamIterator)
> +            data = "hello"
> +            done = 0
> +
> +            def next(self):
> +                if not self.done:
> +                    self.done = 1
> +                    return self.data
> +                raise StopIteration
> +
> +        _response = DummyResponse()
> +        _response._status = '200 OK'
> +        _response._headers = [('Content-Length', '4')]
> +        body = _response.body = test_streamiterator()
> +        environ = self._makeEnviron()
> +        start_response = DummyCallable()
> +        _publish = DummyCallable()
> +        _publish._result = _response
> +        app_iter = self._callFUT(environ, start_response, _publish)
> +        self.assertTrue(app_iter is body)
> +

This test doesn't exercise the length checking branch (it would raise
AttributeError otherwise).


Tres.
- -- 
===================================================================
Tres Seaver          +1 540-429-0999          tseaver at palladion.com
Palladion Software   "Excellence by Design"    http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk2OHwkACgkQ+gerLs4ltQ4O7wCgyazebfa92fAERR7fYs8jaR8w
Q3kAn2eZg5aqdeHAa8hMMppPTmCURYNF
=gieo
-----END PGP SIGNATURE-----



More information about the Zope-Dev mailing list