Skip to content

Commit bff05e5

Browse files
itamarstpythonspeed
authored andcommitted
Switch to proposed upstream fix.
1 parent 3dbba12 commit bff05e5

File tree

2 files changed

+28
-32
lines changed

2 files changed

+28
-32
lines changed

src/pip/_internal/network/cache.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,5 @@ def get_body(self, key: str) -> Optional[BinaryIO]:
7979
return open(path, "rb")
8080

8181
def set_body(self, key: str, body: Optional[bytes]) -> None:
82-
if body is None:
83-
# Workaround for https://github.com/ionrock/cachecontrol/issues/276
84-
return
8582
path = self._get_cache_path(key) + ".body"
8683
self._write(path, body)

src/pip/_vendor/cachecontrol/controller.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ def parse_cache_control(self, headers):
122122

123123
return retval
124124

125+
def _load_from_cache(self, request):
126+
"""
127+
Load a cached response, or return None if it's not available.
128+
"""
129+
cache_url = request.url
130+
cache_data = self.cache.get(cache_url)
131+
if cache_data is None:
132+
logger.debug("No cache entry available")
133+
return None
134+
135+
if isinstance(self.cache, SeparateBodyBaseCache):
136+
body_file = self.cache.get_body(cache_url)
137+
else:
138+
body_file = None
139+
140+
result = self.serializer.loads(request, cache_data, body_file)
141+
if result is None:
142+
logger.warning("Cache entry deserialization failed, entry ignored")
143+
return result
144+
125145
def cached_request(self, request):
126146
"""
127147
Return a cached response if it exists in the cache, otherwise
@@ -140,21 +160,9 @@ def cached_request(self, request):
140160
logger.debug('Request header has "max_age" as 0, cache bypassed')
141161
return False
142162

143-
# Request allows serving from the cache, let's see if we find something
144-
cache_data = self.cache.get(cache_url)
145-
if cache_data is None:
146-
logger.debug("No cache entry available")
147-
return False
148-
149-
if isinstance(self.cache, SeparateBodyBaseCache):
150-
body_file = self.cache.get_body(cache_url)
151-
else:
152-
body_file = None
153-
154-
# Check whether it can be deserialized
155-
resp = self.serializer.loads(request, cache_data, body_file)
163+
# Check whether we can load the response from the cache:
164+
resp = self._load_from_cache(request)
156165
if not resp:
157-
logger.warning("Cache entry deserialization failed, entry ignored")
158166
return False
159167

160168
# If we have a cached permanent redirect, return it immediately. We
@@ -240,8 +248,7 @@ def cached_request(self, request):
240248
return False
241249

242250
def conditional_headers(self, request):
243-
cache_url = self.cache_url(request.url)
244-
resp = self.serializer.loads(request, self.cache.get(cache_url))
251+
resp = self._load_from_cache(request)
245252
new_headers = {}
246253

247254
if resp:
@@ -267,7 +274,10 @@ def _cache_set(self, cache_url, request, response, body=None, expires_time=None)
267274
self.serializer.dumps(request, response, b""),
268275
expires=expires_time,
269276
)
270-
self.cache.set_body(cache_url, body)
277+
# body is None can happen when, for example, we're only updating
278+
# headers, as is the case in update_cached_response().
279+
if body is not None:
280+
self.cache.set_body(cache_url, body)
271281
else:
272282
self.cache.set(
273283
cache_url,
@@ -406,18 +416,7 @@ def update_cached_response(self, request, response):
406416
gotten a 304 as the response.
407417
"""
408418
cache_url = self.cache_url(request.url)
409-
410-
# NOTE: This is a hot-patch for
411-
# https://github.com/ionrock/cachecontrol/issues/276 until it's fixed
412-
# upstream.
413-
if isinstance(self.cache, SeparateBodyBaseCache):
414-
body_file = self.cache.get_body(cache_url)
415-
else:
416-
body_file = None
417-
418-
cached_response = self.serializer.loads(
419-
request, self.cache.get(cache_url), body_file
420-
)
419+
cached_response = self._load_from_cache(request)
421420

422421
if not cached_response:
423422
# we didn't have a cached response

0 commit comments

Comments
 (0)