New submission from Olly <oli...@redgecko.org>:

repoze.who middleware raises an exception when the WSGI response iterator is
empty. This
causes responses with no content body (eg 304 not modified) to fail::

    2012-07-15 20:49:36,759 ERROR [gunicorn.error] Error handling request
    Traceback (most recent call last):
    File "gunicorn/workers/sync.py", line 107, in handle_request
        for item in respiter:
    File "util/wsgi.py", line 132, in next
        return self._next()
    File "eggs/repoze.who-2.0-py2.7.egg/repoze/who/middleware.py", line 145, in
wrapper
        yield first
    NameError: free variable 'first' referenced before assignment in enclosing 
scope


I think the bug is in repoze/who/middleware.py:wrap_generator, which only
assigns a value to `first` if there is at least one iteration::

    def wrap_generator(result):
        """\
        This function returns a generator that behaves exactly the same as the
        original.  It's only difference is it pulls the first iteration off and
        caches it to trigger any immediate side effects (in a WSGI world, this
        ensures start_response is called).
        """
        # PEP 333 requires that we call the original iterator's
        # 'close' method, if it exists, before releasing it.
        close = getattr(result, 'close', lambda: None)
        # Neat trick to pull the first iteration only. We need to do this 
outside
        # of the generator function to ensure it is called.
        for iter in result:
            first = iter
            break

        # Wrapper yields the first iteration, then passes result's iterations
        # directly up.
        def wrapper():
            yield first
            for iter in result:
                # We'll let result's StopIteration bubble up directly.
                yield iter
            close()
        return wrapper()

----------
messages: 541
nosy: olly
priority: bug
status: unread
title: repoze.who raises an exception when the WSGI response iterator is empty.
topic: repoze.who

__________________________________
Repoze Bugs <b...@bugs.repoze.org>
<http://bugs.repoze.org/issue184>
__________________________________
_______________________________________________
Repoze-dev mailing list
Repoze-dev@lists.repoze.org
http://lists.repoze.org/listinfo/repoze-dev

Reply via email to