Skip to content

Commit

Permalink
[PR #6916/abafcdac backport][3.9] Add missing __repr__ to streams.Emp…
Browse files Browse the repository at this point in the history
…tyStreamReader (#7106)

**This is a backport of PR #6916 as merged into master
(abafcda).**

**EmptyStreamReader** currently inherits `__repr__` from
**StreamReader** , which then tries to access non-existing fields,
causing AttributeErrors. The bug causes issues like this in upstream
code: miguelgrinberg/python-socketio#1032

This PR adds a custom `__repr__` to EmptyStreamReader to fix the
problem.

Co-authored-by: elonen <elonen@iki.fi>
  • Loading branch information
patchback[bot] and elonen authored Nov 28, 2022
1 parent 5971d43 commit f7e4246
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/6916.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add __repr__ to EmptyStreamReader to avoid AttributeErrors.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Jake Davis
Jakob Ackermann
Jakub Wilk
Jan Buchar
Jarno Elonen
Jashandeep Sohi
Jean-Baptiste Estival
Jens Steinhauser
Expand Down
3 changes: 3 additions & 0 deletions aiohttp/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ class EmptyStreamReader(StreamReader): # lgtm [py/missing-call-to-init]
def __init__(self) -> None:
pass

def __repr__(self) -> str:
return "<%s>" % self.__class__.__name__

def exception(self) -> Optional[BaseException]:
return None

Expand Down
1 change: 1 addition & 0 deletions tests/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,7 @@ async def test_unread_empty(self) -> None:

async def test_empty_stream_reader() -> None:
s = streams.EmptyStreamReader()
assert str(s) is not None
assert s.set_exception(ValueError()) is None
assert s.exception() is None
assert s.feed_eof() is None
Expand Down

0 comments on commit f7e4246

Please sign in to comment.