New submission from Olly <>:

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/", line 107, in handle_request
        for item in respiter:
    File "util/", line 132, in next
        return self._next()
    File "eggs/repoze.who-2.0-py2.7.egg/repoze/who/", line 145, in
        yield first
    NameError: free variable 'first' referenced before assignment in enclosing 

I think the bug is in repoze/who/, 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 
        # of the generator function to ensure it is called.
        for iter in result:
            first = iter

        # 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
        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 <>
Repoze-dev mailing list

Reply via email to