fix: add __iter__ and __next__ to FileWrapper for PEP 3333 compliance#3550
Merged
benoitc merged 2 commits intobenoitc:masterfrom Mar 24, 2026
Merged
fix: add __iter__ and __next__ to FileWrapper for PEP 3333 compliance#3550benoitc merged 2 commits intobenoitc:masterfrom
benoitc merged 2 commits intobenoitc:masterfrom
Conversation
The WSGI spec (PEP 3333) requires that wsgi.file_wrapper return an iterable object. Gunicorn's FileWrapper only implemented __getitem__, which technically makes it iterable via old-style iteration but breaks code that explicitly relies on the iterator protocol (e.g., calling iter() or using next()). This adds __iter__ (returning self) and __next__ to make FileWrapper a proper iterator, maintaining backward compatibility with existing __getitem__-based usage. Fixes benoitc#3396
Owner
|
Thanks for the contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3396
The WSGI spec (PEP 3333) requires that
wsgi.file_wrapperreturn an iterable object. Currently, Gunicorn'sFileWrapperonly implements__getitem__, which makes it iterable via old-style fallback but breaks code that explicitly relies on the iterator protocol (e.g., callingiter(), usingnext(), or passing to builtins expecting proper iterators).Changes
__iter__returningself__next__reading the next chunk and raisingStopIterationwhen donetests/test_http.pyverifying the iterator protocolBackward Compatibility
This is fully backward compatible:
__getitem__-based iteration continues to workforloops,list(), and other iteration patterns all work__next__logic mirrors the existing__getitem__logic exactly