From 7ad9994ef02d3964c4274d92fd958f313d165a9e Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sat, 9 Nov 2024 20:12:15 +0000 Subject: [PATCH 1/6] enable filterwarnings=['error'] and strict-makers, strict-config --- pyproject.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 5b6910b..3986202 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -77,4 +77,6 @@ module = "msgpack" ignore_missing_imports = true [tool.pytest.ini_options] +addopts = "--strict-markers --strict-config" norecursedirs = ["bin", "lib", "include", "build"] +filterwarnings = ["error"] From 0556cd0df24ac255a107c49dee28e9809dd96395 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sat, 9 Nov 2024 20:12:53 +0000 Subject: [PATCH 2/6] make CallbackFileWrapper closable --- cachecontrol/filewrapper.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cachecontrol/filewrapper.py b/cachecontrol/filewrapper.py index 37d2fa5..ed6ec71 100644 --- a/cachecontrol/filewrapper.py +++ b/cachecontrol/filewrapper.py @@ -117,3 +117,9 @@ def _safe_read(self, amt: int) -> bytes: self._close() return data + + def close(self): + try: + self.__fp.close() + finally: + self.__buf.close() From 628a36783b3776fa9b58753adf90a00759fe6576 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sat, 9 Nov 2024 20:13:36 +0000 Subject: [PATCH 3/6] close streaming responses --- tests/test_chunked_response.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_chunked_response.py b/tests/test_chunked_response.py index 8cc4496..2fbe6b5 100644 --- a/tests/test_chunked_response.py +++ b/tests/test_chunked_response.py @@ -50,7 +50,7 @@ def test_stream_is_cached(self, url, sess): assert content_1 == content_2 def test_stream_is_not_cached_when_content_is_not_read(self, url, sess): - sess.get(url + "stream", stream=True) - resp = sess.get(url + "stream", stream=True) + with sess.get(url + "stream", stream=True) as r1: + with sess.get(url + "stream", stream=True) as resp: - assert not resp.from_cache + assert not resp.from_cache From db688d91ed7f9f512d8bef9d9f60fedca3d37099 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sun, 10 Nov 2024 10:04:55 +0000 Subject: [PATCH 4/6] Update cachecontrol/filewrapper.py with type annotation --- cachecontrol/filewrapper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachecontrol/filewrapper.py b/cachecontrol/filewrapper.py index ed6ec71..efdfeee 100644 --- a/cachecontrol/filewrapper.py +++ b/cachecontrol/filewrapper.py @@ -118,7 +118,7 @@ def _safe_read(self, amt: int) -> bytes: return data - def close(self): + def close(self) -> None: try: self.__fp.close() finally: From fc175f4b4d4f3dfcad8e34169f84e572d2c3c033 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sun, 10 Nov 2024 10:40:26 +0000 Subject: [PATCH 5/6] avoid unused local variable --- tests/test_chunked_response.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_chunked_response.py b/tests/test_chunked_response.py index 2fbe6b5..077bc66 100644 --- a/tests/test_chunked_response.py +++ b/tests/test_chunked_response.py @@ -50,7 +50,6 @@ def test_stream_is_cached(self, url, sess): assert content_1 == content_2 def test_stream_is_not_cached_when_content_is_not_read(self, url, sess): - with sess.get(url + "stream", stream=True) as r1: + with sess.get(url + "stream", stream=True): with sess.get(url + "stream", stream=True) as resp: - assert not resp.from_cache From 8f222106c20365eef9c6d733f67d5705b336f298 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sun, 10 Nov 2024 11:05:51 +0000 Subject: [PATCH 6/6] ensure CallbackFileWrapper.__buf is closed in test_getattr_during_gc --- tests/test_regressions.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 78e8aaa..7e4a76a 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -23,11 +23,12 @@ def test_file_cache_recognizes_consumed_file_handle(self, url): def test_getattr_during_gc(): s = CallbackFileWrapper(None, None) - # normal behavior: - with pytest.raises(AttributeError): - s.x - - # this previously had caused an infinite recursion - vars(s).clear() # gc does this. - with pytest.raises(AttributeError): - s.x + with s._CallbackFileWrapper__buf: + # normal behavior: + with pytest.raises(AttributeError): + s.x + + # this previously had caused an infinite recursion + vars(s).clear() # gc does this. + with pytest.raises(AttributeError): + s.x