From 6e2f084150bce7e187387b242c0dc0cc7e7c5853 Mon Sep 17 00:00:00 2001 From: Felipe Rodrigues Date: Sat, 17 Oct 2020 09:47:56 -0300 Subject: [PATCH 1/4] Switch assert statements to if...raise Assert statements are removed when optimization is requested, which in turn may cause that those safeguards are lifted --- Lib/http/client.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Lib/http/client.py b/Lib/http/client.py index 15abcfeada5915..81be8352a7f8e0 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -572,7 +572,9 @@ def _get_chunk_left(self): return chunk_left def _read_chunked(self, amt=None): - assert self.chunked != _UNKNOWN + if self.chunked == _UNKNOWN: + raise IncompleteRead("Unkown chunk detected.") + value = [] try: while True: @@ -594,7 +596,8 @@ def _read_chunked(self, amt=None): raise IncompleteRead(b''.join(value)) def _readinto_chunked(self, b): - assert self.chunked != _UNKNOWN + if self.chunked == _UNKNOWN: + raise IncompleteRead("Unkown chunk detected.") total_bytes = 0 mvb = memoryview(b) try: @@ -1363,7 +1366,8 @@ def getresponse(self): except ConnectionError: self.close() raise - assert response.will_close != _UNKNOWN + if self.chunked == _UNKNOWN: + raise HTTPException("Unkown chunk detected.") self.__state = _CS_IDLE if response.will_close: From c228be3c985784d049e443e8d98f66fde7ac0f5f Mon Sep 17 00:00:00 2001 From: Felipe Rodrigues Date: Sat, 17 Oct 2020 10:55:00 -0300 Subject: [PATCH 2/4] Fix wrong exception checking & raising --- Lib/http/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/http/client.py b/Lib/http/client.py index 81be8352a7f8e0..b294033846dae3 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -1366,8 +1366,8 @@ def getresponse(self): except ConnectionError: self.close() raise - if self.chunked == _UNKNOWN: - raise HTTPException("Unkown chunk detected.") + if response.will_close == _UNKNOWN: + raise ConnectionError("Unknown if response will close.") self.__state = _CS_IDLE if response.will_close: From a9bab0bad3f9a8b83465b02203250fae4fb351e3 Mon Sep 17 00:00:00 2001 From: Felipe Rodrigues Date: Sat, 17 Oct 2020 11:42:28 -0300 Subject: [PATCH 3/4] Fix whitespacing issues --- Lib/http/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/client.py b/Lib/http/client.py index b294033846dae3..a550e661aad842 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -574,7 +574,7 @@ def _get_chunk_left(self): def _read_chunked(self, amt=None): if self.chunked == _UNKNOWN: raise IncompleteRead("Unkown chunk detected.") - + value = [] try: while True: From 111f8ca3a3cde30df2bf7166b2c8fd2c32a1fdde Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 17 Oct 2020 14:46:17 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2020-10-17-14-46-16.bpo-42060.du-IYq.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-10-17-14-46-16.bpo-42060.du-IYq.rst diff --git a/Misc/NEWS.d/next/Library/2020-10-17-14-46-16.bpo-42060.du-IYq.rst b/Misc/NEWS.d/next/Library/2020-10-17-14-46-16.bpo-42060.du-IYq.rst new file mode 100644 index 00000000000000..03a695e4a537aa --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-10-17-14-46-16.bpo-42060.du-IYq.rst @@ -0,0 +1 @@ +Replace `asserts` on HTTPResponse and HTTPConnection with `if...raises` so that the conditional checks work while running with the optimization flag switched on \ No newline at end of file