Skip to content

Commit f5f2327

Browse files
authored
fix(serialize): simplify loading logic (#317)
All other versions are effectively no-ops, so we can remove them entirely, along with their associated version sniffing.
1 parent 23471ad commit f5f2327

File tree

1 file changed

+6
-66
lines changed

1 file changed

+6
-66
lines changed

cachecontrol/serialize.py

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,14 @@ def loads(
7272
if not data:
7373
return None
7474

75-
# Determine what version of the serializer the data was serialized
76-
# with
77-
try:
78-
ver, data = data.split(b",", 1)
79-
except ValueError:
80-
ver = b"cc=0"
81-
82-
# Make sure that our "ver" is actually a version and isn't a false
83-
# positive from a , being in the data stream.
84-
if ver[:3] != b"cc=":
85-
data = ver + data
86-
ver = b"cc=0"
87-
88-
# Get the version number out of the cc=N
89-
verstr = ver.split(b"=", 1)[-1].decode("ascii")
90-
91-
# Dispatch to the actual load method for the given version
92-
try:
93-
return getattr(self, f"_loads_v{verstr}")(request, data, body_file) # type: ignore[no-any-return]
94-
95-
except AttributeError:
96-
# This is a version we don't have a loads function for, so we'll
97-
# just treat it as a miss and return None
75+
# Previous versions of this library supported other serialization
76+
# formats, but these have all been removed.
77+
if not data.startswith(f"cc={self.serde_version},".encode()):
9878
return None
9979

80+
data = data[5:]
81+
return self._loads_v4(request, data, body_file)
82+
10083
def prepare_response(
10184
self,
10285
request: PreparedRequest,
@@ -149,49 +132,6 @@ def prepare_response(
149132

150133
return HTTPResponse(body=body, preload_content=False, **cached["response"])
151134

152-
def _loads_v0(
153-
self,
154-
request: PreparedRequest,
155-
data: bytes,
156-
body_file: IO[bytes] | None = None,
157-
) -> None:
158-
# The original legacy cache data. This doesn't contain enough
159-
# information to construct everything we need, so we'll treat this as
160-
# a miss.
161-
return None
162-
163-
def _loads_v1(
164-
self,
165-
request: PreparedRequest,
166-
data: bytes,
167-
body_file: IO[bytes] | None = None,
168-
) -> HTTPResponse | None:
169-
# The "v1" pickled cache format. This is no longer supported
170-
# for security reasons, so we treat it as a miss.
171-
return None
172-
173-
def _loads_v2(
174-
self,
175-
request: PreparedRequest,
176-
data: bytes,
177-
body_file: IO[bytes] | None = None,
178-
) -> HTTPResponse | None:
179-
# The "v2" compressed base64 cache format.
180-
# This has been removed due to age and poor size/performance
181-
# characteristics, so we treat it as a miss.
182-
return None
183-
184-
def _loads_v3(
185-
self,
186-
request: PreparedRequest,
187-
data: bytes,
188-
body_file: IO[bytes] | None = None,
189-
) -> None:
190-
# Due to Python 2 encoding issues, it's impossible to know for sure
191-
# exactly how to load v3 entries, thus we'll treat these as a miss so
192-
# that they get rewritten out as v4 entries.
193-
return None
194-
195135
def _loads_v4(
196136
self,
197137
request: PreparedRequest,

0 commit comments

Comments
 (0)