Werkzeug v0.11 I stduy the source code of Werkzeug ,the class ClosingIterator in file wsgi.py,decorted by function implements_iterator:
wsgi.py
@implements_iterator
class ClosingIterator(object):
"""The WSGI specification requires that all middlewares and gateways
respect the `close` callback of an iterator. Because it is useful to add
another close action to a returned iterator and adding a custom iterator
is a boring task this class can be used for that::
return ClosingIterator(app(environ, start_response), [cleanup_session,
cleanup_locals])
If there is just one close function it can be passed instead of the list.
A closing iterator is not needed if the application uses response objects
and finishes the processing if the response is started::
try:
return response(environ, start_response)
finally:
cleanup_session()
cleanup_locals()
"""
I find the define of implements_iterator in file _compat.py:
implements_iterator = _identity
_identity = lambda x: x
the question is : what is the function of implements_iterator ?
Werkzeug targets both Python 2 and Python 3. If you scroll up in compat.py
, you can see that implements_iterator
is defined for Python 2 as follows:
def implements_iterator(cls):
cls.next = cls.__next__
del cls.__next__
return cls
This allows a class to implement a __next__
method (called just next
in Python 2) and work for both Python 2 and 3 without any modifications.