From f7e4246114ce48335f224f36c2d4b058b02bea7b Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 28 Nov 2022 20:29:16 +0000 Subject: [PATCH] [PR #6916/abafcdac backport][3.9] Add missing __repr__ to streams.EmptyStreamReader (#7106) **This is a backport of PR #6916 as merged into master (abafcdac6cbef06bc555cb77b1ebe724d2a098ec).** **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: https://github.com/miguelgrinberg/python-socketio/issues/1032 This PR adds a custom `__repr__` to EmptyStreamReader to fix the problem. Co-authored-by: elonen --- CHANGES/6916.bugfix | 1 + CONTRIBUTORS.txt | 1 + aiohttp/streams.py | 3 +++ tests/test_streams.py | 1 + 4 files changed, 6 insertions(+) create mode 100644 CHANGES/6916.bugfix diff --git a/CHANGES/6916.bugfix b/CHANGES/6916.bugfix new file mode 100644 index 00000000000..4c4329c1abe --- /dev/null +++ b/CHANGES/6916.bugfix @@ -0,0 +1 @@ +Add __repr__ to EmptyStreamReader to avoid AttributeErrors. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 23ea188a074..7c633b30169 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -155,6 +155,7 @@ Jake Davis Jakob Ackermann Jakub Wilk Jan Buchar +Jarno Elonen Jashandeep Sohi Jean-Baptiste Estival Jens Steinhauser diff --git a/aiohttp/streams.py b/aiohttp/streams.py index d5f4c2baaf8..32ec7f148f6 100644 --- a/aiohttp/streams.py +++ b/aiohttp/streams.py @@ -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 diff --git a/tests/test_streams.py b/tests/test_streams.py index a16f7ebf4ad..29eab256578 100644 --- a/tests/test_streams.py +++ b/tests/test_streams.py @@ -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