CacheControl 0.13.1rc0__tar.gz → 0.14.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/PKG-INFO +6 -4
  2. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/README.rst +1 -1
  3. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/cachecontrol/__init__.py +1 -1
  4. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/cachecontrol/adapter.py +5 -5
  5. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/cachecontrol/caches/file_cache.py +4 -3
  6. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/cachecontrol/controller.py +6 -1
  7. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/cachecontrol/heuristics.py +1 -1
  8. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/cachecontrol/serialize.py +8 -68
  9. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/pyproject.toml +4 -2
  10. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/conftest.py +1 -2
  11. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/test_adapter.py +0 -1
  12. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/test_chunked_response.py +0 -1
  13. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/test_etag.py +25 -0
  14. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/test_max_age.py +0 -1
  15. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/test_regressions.py +0 -5
  16. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/test_storage_filecache.py +0 -1
  17. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/test_storage_redis.py +1 -2
  18. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/LICENSE.txt +0 -0
  19. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/cachecontrol/_cmd.py +0 -0
  20. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/cachecontrol/cache.py +0 -0
  21. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/cachecontrol/caches/__init__.py +0 -0
  22. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/cachecontrol/caches/redis_cache.py +0 -0
  23. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/cachecontrol/filewrapper.py +0 -0
  24. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/cachecontrol/py.typed +0 -0
  25. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/cachecontrol/wrapper.py +0 -0
  26. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/__init__.py +0 -0
  27. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/issue_263.py +0 -0
  28. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/test_cache_control.py +0 -0
  29. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/test_expires_heuristics.py +0 -0
  30. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/test_redirects.py +0 -0
  31. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/test_serialization.py +0 -0
  32. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/test_server_http_version.py +0 -0
  33. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/test_vary.py +0 -0
  34. {cachecontrol-0.13.1rc0 → cachecontrol-0.14.0}/tests/utils.py +1 -1
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: CacheControl
3
- Version: 0.13.1rc0
3
+ Version: 0.14.0
4
4
  Summary: httplib2 caching for requests
5
5
  Keywords: requests,http,caching,web
6
6
  Author-email: Eric Larson <ericlarson@ionrock.com>, Frost Ming <me@frostming.com>, William Woodruff <william@yossarian.net>
@@ -10,15 +10,15 @@ Classifier: Development Status :: 4 - Beta
10
10
  Classifier: Environment :: Web Environment
11
11
  Classifier: License :: OSI Approved :: Apache Software License
12
12
  Classifier: Operating System :: OS Independent
13
- Classifier: Programming Language :: Python :: 3.6
14
13
  Classifier: Programming Language :: Python :: 3.7
15
14
  Classifier: Programming Language :: Python :: 3.8
16
15
  Classifier: Programming Language :: Python :: 3.9
17
16
  Classifier: Programming Language :: Python :: 3.10
18
17
  Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
19
  Classifier: Topic :: Internet :: WWW/HTTP
20
20
  Requires-Dist: requests >= 2.16.0
21
- Requires-Dist: msgpack >= 0.5.2
21
+ Requires-Dist: msgpack >= 0.5.2, < 2.0.0
22
22
  Requires-Dist: CacheControl[filecache,redis] ; extra == "dev"
23
23
  Requires-Dist: build ; extra == "dev"
24
24
  Requires-Dist: mypy ; extra == "dev"
@@ -27,6 +27,8 @@ Requires-Dist: pytest-cov ; extra == "dev"
27
27
  Requires-Dist: pytest ; extra == "dev"
28
28
  Requires-Dist: cherrypy ; extra == "dev"
29
29
  Requires-Dist: sphinx ; extra == "dev"
30
+ Requires-Dist: furo ; extra == "dev"
31
+ Requires-Dist: sphinx-copybutton ; extra == "dev"
30
32
  Requires-Dist: black ; extra == "dev"
31
33
  Requires-Dist: types-redis ; extra == "dev"
32
34
  Requires-Dist: types-requests ; extra == "dev"
@@ -76,7 +78,7 @@ Quickstart
76
78
  sess = requests.session()
77
79
  cached_sess = CacheControl(sess)
78
80
 
79
- response = cached_sess.get('http://google.com')
81
+ response = cached_sess.get('https://google.com')
80
82
 
81
83
  If the URL contains any caching based headers, it will cache the
82
84
  result in a simple dictionary.
@@ -35,7 +35,7 @@ Quickstart
35
35
  sess = requests.session()
36
36
  cached_sess = CacheControl(sess)
37
37
 
38
- response = cached_sess.get('http://google.com')
38
+ response = cached_sess.get('https://google.com')
39
39
 
40
40
  If the URL contains any caching based headers, it will cache the
41
41
  result in a simple dictionary.
@@ -8,7 +8,7 @@ Make it easy to import from cachecontrol without long namespaces.
8
8
  """
9
9
  __author__ = "Eric Larson"
10
10
  __email__ = "eric@ionrock.org"
11
- __version__ = "0.13.1rc0"
11
+ __version__ = "0.14.0"
12
12
 
13
13
  from cachecontrol.adapter import CacheControlAdapter
14
14
  from cachecontrol.controller import CacheController
@@ -125,21 +125,21 @@ class CacheControlAdapter(HTTPAdapter):
125
125
  else:
126
126
  # Wrap the response file with a wrapper that will cache the
127
127
  # response when the stream has been consumed.
128
- response._fp = CallbackFileWrapper( # type: ignore[attr-defined]
129
- response._fp, # type: ignore[attr-defined]
128
+ response._fp = CallbackFileWrapper( # type: ignore[assignment]
129
+ response._fp, # type: ignore[arg-type]
130
130
  functools.partial(
131
131
  self.controller.cache_response, request, response
132
132
  ),
133
133
  )
134
134
  if response.chunked:
135
- super_update_chunk_length = response._update_chunk_length # type: ignore[attr-defined]
135
+ super_update_chunk_length = response._update_chunk_length
136
136
 
137
137
  def _update_chunk_length(self: HTTPResponse) -> None:
138
138
  super_update_chunk_length()
139
139
  if self.chunk_left == 0:
140
- self._fp._close() # type: ignore[attr-defined]
140
+ self._fp._close() # type: ignore[union-attr]
141
141
 
142
- response._update_chunk_length = types.MethodType( # type: ignore[attr-defined]
142
+ response._update_chunk_length = types.MethodType( # type: ignore[method-assign]
143
143
  _update_chunk_length, response
144
144
  )
145
145
 
@@ -6,7 +6,8 @@ from __future__ import annotations
6
6
  import hashlib
7
7
  import os
8
8
  from textwrap import dedent
9
- from typing import IO, TYPE_CHECKING
9
+ from typing import IO, TYPE_CHECKING, Union
10
+ from pathlib import Path
10
11
 
11
12
  from cachecontrol.cache import BaseCache, SeparateBodyBaseCache
12
13
  from cachecontrol.controller import CacheController
@@ -63,7 +64,7 @@ class _FileCacheMixin:
63
64
 
64
65
  def __init__(
65
66
  self,
66
- directory: str,
67
+ directory: str | Path,
67
68
  forever: bool = False,
68
69
  filemode: int = 0o0600,
69
70
  dirmode: int = 0o0700,
@@ -79,7 +80,7 @@ class _FileCacheMixin:
79
80
  """
80
81
  NOTE: In order to use the FileCache you must have
81
82
  filelock installed. You can install it via pip:
82
- pip install filelock
83
+ pip install cachecontrol[filecache]
83
84
  """
84
85
  )
85
86
  raise ImportError(notice)
@@ -142,6 +142,11 @@ class CacheController:
142
142
  """
143
143
  Load a cached response, or return None if it's not available.
144
144
  """
145
+ # We do not support caching of partial content: so if the request contains a
146
+ # Range header then we don't want to load anything from the cache.
147
+ if "Range" in request.headers:
148
+ return None
149
+
145
150
  cache_url = request.url
146
151
  assert cache_url is not None
147
152
  cache_data = self.cache.get(cache_url)
@@ -480,7 +485,7 @@ class CacheController:
480
485
  cached_response.headers.update(
481
486
  {
482
487
  k: v
483
- for k, v in response.headers.items() # type: ignore[no-untyped-call]
488
+ for k, v in response.headers.items()
484
489
  if k.lower() not in excluded_headers
485
490
  }
486
491
  )
@@ -68,7 +68,7 @@ class OneDayCache(BaseHeuristic):
68
68
 
69
69
  if "expires" not in response.headers:
70
70
  date = parsedate(response.headers["date"])
71
- expires = expire_after(timedelta(days=1), date=datetime(*date[:6], tzinfo=timezone.utc)) # type: ignore[misc]
71
+ expires = expire_after(timedelta(days=1), date=datetime(*date[:6], tzinfo=timezone.utc)) # type: ignore[index,misc]
72
72
  headers["expires"] = datetime_to_header(expires)
73
73
  headers["cache-control"] = "public"
74
74
  return headers
@@ -32,13 +32,13 @@ class Serializer:
32
32
  # also update the response with a new file handler to be
33
33
  # sure it acts as though it was never read.
34
34
  body = response.read(decode_content=False)
35
- response._fp = io.BytesIO(body) # type: ignore[attr-defined]
35
+ response._fp = io.BytesIO(body) # type: ignore[assignment]
36
36
  response.length_remaining = len(body)
37
37
 
38
38
  data = {
39
39
  "response": {
40
40
  "body": body, # Empty bytestring if body is stored separately
41
- "headers": {str(k): str(v) for k, v in response.headers.items()}, # type: ignore[no-untyped-call]
41
+ "headers": {str(k): str(v) for k, v in response.headers.items()},
42
42
  "status": response.status,
43
43
  "version": response.version,
44
44
  "reason": str(response.reason),
@@ -72,31 +72,14 @@ class Serializer:
72
72
  if not data:
73
73
  return None
74
74
 
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()):
98
78
  return None
99
79
 
80
+ data = data[5:]
81
+ return self._loads_v4(request, data, body_file)
82
+
100
83
  def prepare_response(
101
84
  self,
102
85
  request: PreparedRequest,
@@ -149,49 +132,6 @@ class Serializer:
149
132
 
150
133
  return HTTPResponse(body=body, preload_content=False, **cached["response"])
151
134
 
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
-
195
135
  def _loads_v4(
196
136
  self,
197
137
  request: PreparedRequest,
@@ -24,16 +24,16 @@ classifiers = [
24
24
  "Environment :: Web Environment",
25
25
  "License :: OSI Approved :: Apache Software License",
26
26
  "Operating System :: OS Independent",
27
- "Programming Language :: Python :: 3.6",
28
27
  "Programming Language :: Python :: 3.7",
29
28
  "Programming Language :: Python :: 3.8",
30
29
  "Programming Language :: Python :: 3.9",
31
30
  "Programming Language :: Python :: 3.10",
32
31
  "Programming Language :: Python :: 3.11",
32
+ "Programming Language :: Python :: 3.12",
33
33
  "Topic :: Internet :: WWW/HTTP",
34
34
  ]
35
35
  keywords = ["requests", "http", "caching", "web"]
36
- dependencies = ["requests >= 2.16.0", "msgpack >= 0.5.2"]
36
+ dependencies = ["requests >= 2.16.0", "msgpack >= 0.5.2, < 2.0.0"]
37
37
  requires-python = ">=3.7"
38
38
 
39
39
  [project.urls]
@@ -56,6 +56,8 @@ dev = [
56
56
  "pytest",
57
57
  "cherrypy",
58
58
  "sphinx",
59
+ "furo",
60
+ "sphinx-copybutton",
59
61
  "black",
60
62
  "types-redis",
61
63
  "types-requests",
@@ -13,7 +13,6 @@ import cherrypy
13
13
 
14
14
 
15
15
  class SimpleApp:
16
-
17
16
  def __init__(self):
18
17
  self.etag_count = 0
19
18
  self.update_etag_string()
@@ -109,7 +108,7 @@ class SimpleApp:
109
108
  headers = [
110
109
  ("Content-Type", "text/plain"),
111
110
  ("Cache-Control", "max-age=5000"),
112
- ("Content-Length", str(len(body)))
111
+ ("Content-Length", str(len(body))),
113
112
  ]
114
113
  start_response("200 OK", headers)
115
114
  return [body]
@@ -35,7 +35,6 @@ def sess(url, request):
35
35
 
36
36
 
37
37
  class TestSessionActions:
38
-
39
38
  def test_get_caches(self, url, sess):
40
39
  r2 = sess.get(url)
41
40
  assert r2.from_cache is True
@@ -21,7 +21,6 @@ def sess():
21
21
 
22
22
 
23
23
  class TestChunkedResponses:
24
-
25
24
  def test_cache_chunked_response(self, url, sess):
26
25
  """
27
26
  Verify that an otherwise cacheable response is cached when the
@@ -79,6 +79,31 @@ class TestETag:
79
79
  # Make sure we updated our cache with the new etag'd response.
80
80
  assert self.cache.get(self.etag_url) == resp.raw
81
81
 
82
+ def test_etags_get_no_cache(self, sess, server):
83
+ """A 'Cache-Control: no-cache' header stops us from using the cache directly,
84
+ but not from using the 'If-None-Match' header on the request."""
85
+ # get our response
86
+ r = sess.get(self.etag_url)
87
+ assert "if-none-match" not in r.request.headers
88
+
89
+ r = sess.get(self.etag_url, headers={"Cache-Control": "no-cache"})
90
+ assert "if-none-match" in r.request.headers
91
+ assert r.status_code == 200
92
+
93
+ # This response does come from the cache, but only after the 304 response from
94
+ # the server told us that was fine.
95
+ assert r.from_cache
96
+
97
+ def test_etags_get_with_range(self, sess, server):
98
+ """A 'Range' header stops us from using the cache altogether."""
99
+ # get our response
100
+ r = sess.get(self.etag_url)
101
+
102
+ r = sess.get(self.etag_url, headers={"Range": "0-10"})
103
+ assert "if-none-match" not in r.request.headers
104
+ assert r.status_code == 200
105
+ assert not r.from_cache
106
+
82
107
 
83
108
  class TestDisabledETags:
84
109
  """Test our use of ETags when the response is stale and the
@@ -11,7 +11,6 @@ from .utils import NullSerializer
11
11
 
12
12
 
13
13
  class TestMaxAge:
14
-
15
14
  @pytest.fixture()
16
15
  def sess(self, url):
17
16
  self.url = url
@@ -2,7 +2,6 @@
2
2
  #
3
3
  # SPDX-License-Identifier: Apache-2.0
4
4
 
5
- import sys
6
5
  import pytest
7
6
 
8
7
 
@@ -13,10 +12,6 @@ from requests import Session
13
12
 
14
13
 
15
14
  class Test39:
16
-
17
- @pytest.mark.skipif(
18
- sys.version.startswith("2"), reason="Only run this for python 3.x"
19
- )
20
15
  def test_file_cache_recognizes_consumed_file_handle(self, url):
21
16
  s = CacheControl(Session(), FileCache("web_cache"))
22
17
  the_url = url + "cache_60"
@@ -25,7 +25,6 @@ def randomdata():
25
25
 
26
26
 
27
27
  class FileCacheTestsMixin:
28
-
29
28
  FileCacheClass = None # Either FileCache or SeparateBodyFileCache
30
29
 
31
30
  @pytest.fixture()
@@ -18,8 +18,7 @@ class TestRedisCache:
18
18
  assert self.conn.setex.called
19
19
 
20
20
  def test_set_expiration_datetime_aware(self):
21
- self.cache.set("foo", "bar",
22
- expires=datetime(2014, 2, 2, tzinfo=timezone.utc))
21
+ self.cache.set("foo", "bar", expires=datetime(2014, 2, 2, tzinfo=timezone.utc))
23
22
  assert self.conn.setex.called
24
23
 
25
24
  def test_set_expiration_int(self):
@@ -8,7 +8,6 @@ from cachecontrol.serialize import Serializer
8
8
 
9
9
 
10
10
  class NullSerializer(Serializer):
11
-
12
11
  def dumps(self, request, response, body=None):
13
12
  return response
14
13
 
@@ -20,6 +19,7 @@ class NullSerializer(Serializer):
20
19
 
21
20
  class DummyResponse:
22
21
  """Match a ``urllib3.response.HTTPResponse``."""
22
+
23
23
  version = "1.1"
24
24
  reason = b"Because"
25
25
  strict = 0