diff --git a/cachecontrol/filewrapper.py b/cachecontrol/filewrapper.py index 37d2fa5..efdfeee 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) -> None: + try: + self.__fp.close() + finally: + self.__buf.close() 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"] diff --git a/tests/test_chunked_response.py b/tests/test_chunked_response.py index 8cc4496..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): - sess.get(url + "stream", stream=True) - resp = sess.get(url + "stream", stream=True) - - assert not resp.from_cache + with sess.get(url + "stream", stream=True): + with sess.get(url + "stream", stream=True) as resp: + assert not resp.from_cache 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