Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
# We are done with the server response, read a
# possible response body (compliant servers will
# not return one, but we cannot be 100% sure) and
# release the connection back to the pool.
response.read(decode_content=False)
response.release_conn()
response = cached_response
# We always cache the 301 responses
elif response.status == 301:
self.controller.cache_response(request, response)
else:
# Wrap the response file with a wrapper that will cache the
# response when the stream has been consumed.
response._fp = CallbackFileWrapper(
response._fp,
functools.partial(
self.controller.cache_response, request, response
),
)
if response.chunked:
super_update_chunk_length = response._update_chunk_length
def _update_chunk_length(self):
super_update_chunk_length()
if self.chunk_left == 0:
self._fp._close()
response._update_chunk_length = types.MethodType(
_update_chunk_length, response
)
response if appropriate and mark with `from_cache`.
"""
from_cache = getattr(response, 'from_cache', False)
if request.method == 'GET' and not from_cache:
if response.status == 304:
cached_response = self.controller.update_cached_response(
request, response
)
if cached_response is not response:
from_cache = True
response = cached_response
else:
if self.heuristic:
response = self.heuristic.apply(response)
response._fp = CallbackFileWrapper(
response._fp,
functools.partial(
self.controller.cache_response,
request,
response,
)
)
if request.method in INVALIDATING_METHODS and response.ok:
cache_url = self.controller.cache_url(request.url)
self.cache.delete(cache_url)
response.from_cache = from_cache
return request, response