CacheControl 0.14.1__tar.gz → 0.14.3__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.14.1 → cachecontrol-0.14.3}/PKG-INFO +5 -5
  2. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/cachecontrol/__init__.py +1 -1
  3. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/cachecontrol/adapter.py +13 -6
  4. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/cachecontrol/caches/file_cache.py +10 -47
  5. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/cachecontrol/controller.py +12 -1
  6. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/pyproject.toml +4 -5
  7. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/test_adapter.py +30 -1
  8. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/test_cache_control.py +3 -3
  9. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/LICENSE.txt +0 -0
  10. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/README.rst +0 -0
  11. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/cachecontrol/_cmd.py +0 -0
  12. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/cachecontrol/cache.py +0 -0
  13. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/cachecontrol/caches/__init__.py +0 -0
  14. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/cachecontrol/caches/redis_cache.py +0 -0
  15. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/cachecontrol/filewrapper.py +0 -0
  16. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/cachecontrol/heuristics.py +0 -0
  17. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/cachecontrol/py.typed +0 -0
  18. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/cachecontrol/serialize.py +0 -0
  19. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/cachecontrol/wrapper.py +0 -0
  20. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/__init__.py +0 -0
  21. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/conftest.py +0 -0
  22. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/issue_263.py +0 -0
  23. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/test_chunked_response.py +0 -0
  24. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/test_etag.py +0 -0
  25. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/test_expires_heuristics.py +0 -0
  26. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/test_max_age.py +0 -0
  27. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/test_redirects.py +0 -0
  28. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/test_regressions.py +0 -0
  29. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/test_serialization.py +0 -0
  30. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/test_server_http_version.py +0 -0
  31. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/test_storage_filecache.py +0 -0
  32. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/test_storage_redis.py +0 -0
  33. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/test_vary.py +0 -0
  34. {cachecontrol-0.14.1 → cachecontrol-0.14.3}/tests/utils.py +0 -0
@@ -1,22 +1,22 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: CacheControl
3
- Version: 0.14.1
3
+ Version: 0.14.3
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>
7
- Requires-Python: >=3.8
7
+ Requires-Python: >=3.9
8
8
  Description-Content-Type: text/x-rst
9
+ License-Expression: Apache-2.0
9
10
  Classifier: Development Status :: 4 - Beta
10
11
  Classifier: Environment :: Web Environment
11
- Classifier: License :: OSI Approved :: Apache Software License
12
12
  Classifier: Operating System :: OS Independent
13
- Classifier: Programming Language :: Python :: 3.8
14
13
  Classifier: Programming Language :: Python :: 3.9
15
14
  Classifier: Programming Language :: Python :: 3.10
16
15
  Classifier: Programming Language :: Python :: 3.11
17
16
  Classifier: Programming Language :: Python :: 3.12
18
17
  Classifier: Programming Language :: Python :: 3.13
19
18
  Classifier: Topic :: Internet :: WWW/HTTP
19
+ License-File: LICENSE.txt
20
20
  Requires-Dist: requests >= 2.16.0
21
21
  Requires-Dist: msgpack >= 0.5.2, < 2.0.0
22
22
  Requires-Dist: CacheControl[filecache, redis] ; extra == "dev"
@@ -9,7 +9,7 @@ Make it easy to import from cachecontrol without long namespaces.
9
9
 
10
10
  __author__ = "Eric Larson"
11
11
  __email__ = "eric@ionrock.org"
12
- __version__ = "0.14.1"
12
+ __version__ = "0.14.3"
13
13
 
14
14
  from cachecontrol.adapter import CacheControlAdapter
15
15
  from cachecontrol.controller import CacheController
@@ -5,6 +5,7 @@ from __future__ import annotations
5
5
 
6
6
  import functools
7
7
  import types
8
+ import weakref
8
9
  import zlib
9
10
  from typing import TYPE_CHECKING, Any, Collection, Mapping
10
11
 
@@ -128,19 +129,25 @@ class CacheControlAdapter(HTTPAdapter):
128
129
  response._fp = CallbackFileWrapper( # type: ignore[assignment]
129
130
  response._fp, # type: ignore[arg-type]
130
131
  functools.partial(
131
- self.controller.cache_response, request, response
132
+ self.controller.cache_response, request, weakref.ref(response)
132
133
  ),
133
134
  )
134
135
  if response.chunked:
135
- super_update_chunk_length = response._update_chunk_length
136
+ super_update_chunk_length = response.__class__._update_chunk_length
136
137
 
137
- def _update_chunk_length(self: HTTPResponse) -> None:
138
- super_update_chunk_length()
138
+ def _update_chunk_length(
139
+ weak_self: weakref.ReferenceType[HTTPResponse],
140
+ ) -> None:
141
+ self = weak_self()
142
+ if self is None:
143
+ return
144
+
145
+ super_update_chunk_length(self)
139
146
  if self.chunk_left == 0:
140
147
  self._fp._close() # type: ignore[union-attr]
141
148
 
142
- response._update_chunk_length = types.MethodType( # type: ignore[method-assign]
143
- _update_chunk_length, response
149
+ response._update_chunk_length = functools.partial( # type: ignore[method-assign]
150
+ _update_chunk_length, weakref.ref(response)
144
151
  )
145
152
 
146
153
  resp: Response = super().build_response(request, response)
@@ -5,6 +5,7 @@ from __future__ import annotations
5
5
 
6
6
  import hashlib
7
7
  import os
8
+ import tempfile
8
9
  from textwrap import dedent
9
10
  from typing import IO, TYPE_CHECKING
10
11
  from pathlib import Path
@@ -18,47 +19,6 @@ if TYPE_CHECKING:
18
19
  from filelock import BaseFileLock
19
20
 
20
21
 
21
- def _secure_open_write(filename: str, fmode: int) -> IO[bytes]:
22
- # We only want to write to this file, so open it in write only mode
23
- flags = os.O_WRONLY
24
-
25
- # os.O_CREAT | os.O_EXCL will fail if the file already exists, so we only
26
- # will open *new* files.
27
- # We specify this because we want to ensure that the mode we pass is the
28
- # mode of the file.
29
- flags |= os.O_CREAT | os.O_EXCL
30
-
31
- # Do not follow symlinks to prevent someone from making a symlink that
32
- # we follow and insecurely open a cache file.
33
- if hasattr(os, "O_NOFOLLOW"):
34
- flags |= os.O_NOFOLLOW
35
-
36
- # On Windows we'll mark this file as binary
37
- if hasattr(os, "O_BINARY"):
38
- flags |= os.O_BINARY
39
-
40
- # Before we open our file, we want to delete any existing file that is
41
- # there
42
- try:
43
- os.remove(filename)
44
- except OSError:
45
- # The file must not exist already, so we can just skip ahead to opening
46
- pass
47
-
48
- # Open our file, the use of os.O_CREAT | os.O_EXCL will ensure that if a
49
- # race condition happens between the os.remove and this line, that an
50
- # error will be raised. Because we utilize a lockfile this should only
51
- # happen if someone is attempting to attack us.
52
- fd = os.open(filename, flags, fmode)
53
- try:
54
- return os.fdopen(fd, "wb")
55
-
56
- except:
57
- # An error occurred wrapping our FD in a file object
58
- os.close(fd)
59
- raise
60
-
61
-
62
22
  class _FileCacheMixin:
63
23
  """Shared implementation for both FileCache variants."""
64
24
 
@@ -122,15 +82,18 @@ class _FileCacheMixin:
122
82
  Safely write the data to the given path.
123
83
  """
124
84
  # Make sure the directory exists
125
- try:
126
- os.makedirs(os.path.dirname(path), self.dirmode)
127
- except OSError:
128
- pass
85
+ dirname = os.path.dirname(path)
86
+ os.makedirs(dirname, self.dirmode, exist_ok=True)
129
87
 
130
88
  with self.lock_class(path + ".lock"):
131
89
  # Write our actual file
132
- with _secure_open_write(path, self.filemode) as fh:
133
- fh.write(data)
90
+ (fd, name) = tempfile.mkstemp(dir=dirname)
91
+ try:
92
+ os.write(fd, data)
93
+ finally:
94
+ os.close(fd)
95
+ os.chmod(name, self.filemode)
96
+ os.replace(name, path)
134
97
 
135
98
  def _delete(self, key: str, suffix: str) -> None:
136
99
  name = self._fn(key) + suffix
@@ -12,6 +12,7 @@ import calendar
12
12
  import logging
13
13
  import re
14
14
  import time
15
+ import weakref
15
16
  from email.utils import parsedate_tz
16
17
  from typing import TYPE_CHECKING, Collection, Mapping
17
18
 
@@ -323,7 +324,7 @@ class CacheController:
323
324
  def cache_response(
324
325
  self,
325
326
  request: PreparedRequest,
326
- response: HTTPResponse,
327
+ response_or_ref: HTTPResponse | weakref.ReferenceType[HTTPResponse],
327
328
  body: bytes | None = None,
328
329
  status_codes: Collection[int] | None = None,
329
330
  ) -> None:
@@ -332,6 +333,16 @@ class CacheController:
332
333
 
333
334
  This assumes a requests Response object.
334
335
  """
336
+ if isinstance(response_or_ref, weakref.ReferenceType):
337
+ response = response_or_ref()
338
+ if response is None:
339
+ # The weakref can be None only in case the user used streamed request
340
+ # and did not consume or close it, and holds no reference to requests.Response.
341
+ # In such case, we don't want to cache the response.
342
+ return
343
+ else:
344
+ response = response_or_ref
345
+
335
346
  # From httplib2: Don't cache 206's since we aren't going to
336
347
  # handle byte range requests
337
348
  cacheable_status_codes = status_codes or self.cacheable_status_codes
@@ -1,5 +1,5 @@
1
1
  [build-system]
2
- requires = ["flit_core >=3.2,<4"]
2
+ requires = ["flit_core >=3.11,<4"]
3
3
  build-backend = "flit_core.buildapi"
4
4
 
5
5
  [tool.flit.module]
@@ -13,7 +13,8 @@ name = "CacheControl"
13
13
  dynamic = ["version"]
14
14
  description = "httplib2 caching for requests"
15
15
  readme = "README.rst"
16
- license = { file = "LICENSE.txt" }
16
+ license = "Apache-2.0"
17
+ license-files = ["LICENSE.txt"]
17
18
  authors = [
18
19
  { name = "Eric Larson", email = "ericlarson@ionrock.com" },
19
20
  { name = "Frost Ming", email = "me@frostming.com" },
@@ -22,9 +23,7 @@ authors = [
22
23
  classifiers = [
23
24
  "Development Status :: 4 - Beta",
24
25
  "Environment :: Web Environment",
25
- "License :: OSI Approved :: Apache Software License",
26
26
  "Operating System :: OS Independent",
27
- "Programming Language :: Python :: 3.8",
28
27
  "Programming Language :: Python :: 3.9",
29
28
  "Programming Language :: Python :: 3.10",
30
29
  "Programming Language :: Python :: 3.11",
@@ -34,7 +33,7 @@ classifiers = [
34
33
  ]
35
34
  keywords = ["requests", "http", "caching", "web"]
36
35
  dependencies = ["requests >= 2.16.0", "msgpack >= 0.5.2, < 2.0.0"]
37
- requires-python = ">=3.8"
36
+ requires-python = ">=3.9"
38
37
 
39
38
  [project.urls]
40
39
  Homepage = "https://pypi.org/project/CacheControl/"
@@ -2,10 +2,14 @@
2
2
  #
3
3
  # SPDX-License-Identifier: Apache-2.0
4
4
 
5
+ import gc
6
+ import platform
7
+ import weakref
5
8
  from unittest import mock
6
- import pytest
7
9
 
10
+ import pytest
8
11
  from requests import Session
12
+
9
13
  from cachecontrol.adapter import CacheControlAdapter
10
14
  from cachecontrol.cache import DictCache
11
15
  from cachecontrol.wrapper import CacheControl
@@ -65,3 +69,28 @@ class TestSessionActions:
65
69
 
66
70
  sess.close()
67
71
  assert cache.close.called
72
+
73
+ def test_do_not_leak_response(self, url, sess):
74
+ resp = sess.get(url + "stream", stream=True)
75
+ resp.raise_for_status()
76
+ r1_weak = weakref.ref(resp.raw)
77
+
78
+ # This is a mis-use of requests, becase we should either consume
79
+ # the body, or call .close().
80
+ # But requests without cachecontrol handle this anyway, because
81
+ # urllib3.response.HTTPResponse has a __del__ finalizer on it that closes it
82
+ # once there are no more references to it.
83
+ # We should not break this.
84
+
85
+ resp = None
86
+ if platform.python_implementation() == "PyPy":
87
+ # NOTE: Need to explicitly tell PyPy to collect at this point.
88
+ # See: https://github.com/psf/cachecontrol/issues/351
89
+ # See: https://doc.pypy.org/en/latest/cpython_differences.html#differences-related-to-garbage-collection-strategies
90
+ gc.collect()
91
+
92
+ # Below this point, it should be closed because there are no more references
93
+ # to the session response.
94
+
95
+ r1 = r1_weak()
96
+ assert r1 is None or r1.closed
@@ -6,8 +6,8 @@
6
6
  Unit tests that verify our caching methods work correctly.
7
7
  """
8
8
 
9
+ import os
9
10
  import time
10
- from tempfile import mkdtemp
11
11
  from unittest.mock import ANY, Mock
12
12
 
13
13
  import pytest
@@ -143,7 +143,7 @@ class TestCacheControllerResponse:
143
143
  result = cc.update_cached_response(req, resp)
144
144
  assert result is resp
145
145
 
146
- def test_update_cached_response_with_valid_headers_separate_body(self):
146
+ def test_update_cached_response_with_valid_headers_separate_body(self, tmp_path):
147
147
  """
148
148
  If the local cache has the given URL ``update_cached_response()`` will:
149
149
 
@@ -152,7 +152,7 @@ class TestCacheControllerResponse:
152
152
 
153
153
  This is the version for a cache that stores a separate body.
154
154
  """
155
- cache = SeparateBodyFileCache(mkdtemp())
155
+ cache = SeparateBodyFileCache(os.fsdecode(tmp_path))
156
156
  self.update_cached_response_with_valid_headers_test(cache)
157
157
 
158
158
  def test_update_cached_response_with_valid_headers(self):
File without changes
File without changes