CacheControl 0.14.0__py3-none-any.whl → 0.14.2__py3-none-any.whl

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.
cachecontrol/__init__.py CHANGED
@@ -6,9 +6,10 @@
6
6
 
7
7
  Make it easy to import from cachecontrol without long namespaces.
8
8
  """
9
+
9
10
  __author__ = "Eric Larson"
10
11
  __email__ = "eric@ionrock.org"
11
- __version__ = "0.14.0"
12
+ __version__ = "0.14.2"
12
13
 
13
14
  from cachecontrol.adapter import CacheControlAdapter
14
15
  from cachecontrol.controller import CacheController
cachecontrol/adapter.py CHANGED
@@ -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
 
@@ -77,7 +78,7 @@ class CacheControlAdapter(HTTPAdapter):
77
78
 
78
79
  return resp
79
80
 
80
- def build_response(
81
+ def build_response( # type: ignore[override]
81
82
  self,
82
83
  request: PreparedRequest,
83
84
  response: HTTPResponse,
@@ -128,22 +129,28 @@ 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
- resp: Response = super().build_response(request, response) # type: ignore[no-untyped-call]
153
+ resp: Response = super().build_response(request, response)
147
154
 
148
155
  # See if we should invalidate the cache.
149
156
  if request.method in self.invalidating_methods and resp.ok:
cachecontrol/cache.py CHANGED
@@ -6,6 +6,7 @@
6
6
  The cache object API for implementing caches. The default is a thread
7
7
  safe in-memory dictionary.
8
8
  """
9
+
9
10
  from __future__ import annotations
10
11
 
11
12
  from threading import Lock
@@ -5,8 +5,9 @@ from __future__ import annotations
5
5
 
6
6
  import hashlib
7
7
  import os
8
+ import tempfile
8
9
  from textwrap import dedent
9
- from typing import IO, TYPE_CHECKING, Union
10
+ from typing import IO, TYPE_CHECKING
10
11
  from pathlib import Path
11
12
 
12
13
  from cachecontrol.cache import BaseCache, SeparateBodyBaseCache
@@ -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
@@ -5,12 +5,14 @@
5
5
  """
6
6
  The httplib2 algorithms ported for use with requests.
7
7
  """
8
+
8
9
  from __future__ import annotations
9
10
 
10
11
  import calendar
11
12
  import logging
12
13
  import re
13
14
  import time
15
+ import weakref
14
16
  from email.utils import parsedate_tz
15
17
  from typing import TYPE_CHECKING, Collection, Mapping
16
18
 
@@ -322,7 +324,7 @@ class CacheController:
322
324
  def cache_response(
323
325
  self,
324
326
  request: PreparedRequest,
325
- response: HTTPResponse,
327
+ response_or_ref: HTTPResponse | weakref.ReferenceType[HTTPResponse],
326
328
  body: bytes | None = None,
327
329
  status_codes: Collection[int] | None = None,
328
330
  ) -> None:
@@ -331,6 +333,16 @@ class CacheController:
331
333
 
332
334
  This assumes a requests Response object.
333
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
+
334
346
  # From httplib2: Don't cache 206's since we aren't going to
335
347
  # handle byte range requests
336
348
  cacheable_status_codes = status_codes or self.cacheable_status_codes
@@ -38,10 +38,10 @@ class CallbackFileWrapper:
38
38
  self.__callback = callback
39
39
 
40
40
  def __getattr__(self, name: str) -> Any:
41
- # The vaguaries of garbage collection means that self.__fp is
41
+ # The vagaries of garbage collection means that self.__fp is
42
42
  # not always set. By using __getattribute__ and the private
43
43
  # name[0] allows looking up the attribute value and raising an
44
- # AttributeError when it doesn't exist. This stop thigns from
44
+ # AttributeError when it doesn't exist. This stop things from
45
45
  # infinitely recursing calls to getattr in the case where
46
46
  # self.__fp hasn't been set.
47
47
  #
@@ -68,7 +68,10 @@ 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[index,misc]
71
+ expires = expire_after(
72
+ timedelta(days=1),
73
+ date=datetime(*date[:6], tzinfo=timezone.utc), # type: ignore[index,misc]
74
+ )
72
75
  headers["expires"] = datetime_to_header(expires)
73
76
  headers["cache-control"] = "public"
74
77
  return headers
@@ -1,35 +1,36 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: CacheControl
3
- Version: 0.14.0
3
+ Version: 0.14.2
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.7
7
+ Requires-Python: >=3.8
8
8
  Description-Content-Type: text/x-rst
9
9
  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.7
14
13
  Classifier: Programming Language :: Python :: 3.8
15
14
  Classifier: Programming Language :: Python :: 3.9
16
15
  Classifier: Programming Language :: Python :: 3.10
17
16
  Classifier: Programming Language :: Python :: 3.11
18
17
  Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
19
  Classifier: Topic :: Internet :: WWW/HTTP
20
20
  Requires-Dist: requests >= 2.16.0
21
21
  Requires-Dist: msgpack >= 0.5.2, < 2.0.0
22
- Requires-Dist: CacheControl[filecache,redis] ; extra == "dev"
22
+ Requires-Dist: CacheControl[filecache, redis] ; extra == "dev"
23
23
  Requires-Dist: build ; extra == "dev"
24
+ Requires-Dist: cherrypy ; extra == "dev"
25
+ Requires-Dist: codespell[tomli] ; extra == "dev"
26
+ Requires-Dist: furo ; extra == "dev"
24
27
  Requires-Dist: mypy ; extra == "dev"
25
- Requires-Dist: tox ; extra == "dev"
26
- Requires-Dist: pytest-cov ; extra == "dev"
27
28
  Requires-Dist: pytest ; extra == "dev"
28
- Requires-Dist: cherrypy ; extra == "dev"
29
+ Requires-Dist: pytest-cov ; extra == "dev"
30
+ Requires-Dist: ruff ; extra == "dev"
29
31
  Requires-Dist: sphinx ; extra == "dev"
30
- Requires-Dist: furo ; extra == "dev"
31
32
  Requires-Dist: sphinx-copybutton ; extra == "dev"
32
- Requires-Dist: black ; extra == "dev"
33
+ Requires-Dist: tox ; extra == "dev"
33
34
  Requires-Dist: types-redis ; extra == "dev"
34
35
  Requires-Dist: types-requests ; extra == "dev"
35
36
  Requires-Dist: filelock >= 3.8.0 ; extra == "filecache"
@@ -0,0 +1,18 @@
1
+ cachecontrol/__init__.py,sha256=GW28Jizgz3q_vQN7OpVRVYVDBy-t_nNKN7cJP0jPR2E,641
2
+ cachecontrol/_cmd.py,sha256=4l2UbK9N85Vr_KTkF4LjvSlsr-TZnOXTfT2NODlIMtc,1672
3
+ cachecontrol/adapter.py,sha256=6WA-glAfcEfzWiMbM5XZFVnCilEGOWFwIzYaMXCENx8,6491
4
+ cachecontrol/cache.py,sha256=OXwv7Fn2AwnKNiahJHnjtvaKLndvVLv_-zO-ltlV9qI,1953
5
+ cachecontrol/controller.py,sha256=sBPfFSG1GY1gQ_-pPIFLrnShodU0D2h7KmPZv_l9cn4,19029
6
+ cachecontrol/filewrapper.py,sha256=2ktXNPE0KqnyzF24aOsKCA58HQq1xeC6l2g6_zwjghc,4291
7
+ cachecontrol/heuristics.py,sha256=ccecGyeEycWDY_Kfnx41ebYl7eSdDCLM5X6Sx0f5fZE,4869
8
+ cachecontrol/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
+ cachecontrol/serialize.py,sha256=dP5k_UKYUAano6TKG9qlbKHbFMonz0AV_lYR3IFCGMg,5110
10
+ cachecontrol/wrapper.py,sha256=KyaQ4Bq1RqsX-zA4pXH62nmcU511ECIGn5gCvmNIj20,1328
11
+ cachecontrol/caches/__init__.py,sha256=3-BaJBXjdt-4iFbv-799XjzRBsd6zF9wOEqOdDuAAro,279
12
+ cachecontrol/caches/file_cache.py,sha256=0Df9CcKBaGjKF4dhkFGncLx-YHae0BOPd0jo67mG1rQ,4093
13
+ cachecontrol/caches/redis_cache.py,sha256=94Qw4INGwjHcCDwMnOe0gbbcP2fttrlui-e1-Yutclw,1374
14
+ cachecontrol-0.14.2.dist-info/entry_points.txt,sha256=1NtpBDZSbqMMbkIxhMBtvQoF7c5Mmfd7EE9hBPG_sLs,54
15
+ cachecontrol-0.14.2.dist-info/LICENSE.txt,sha256=hu7uh74qQ_P_H1ZJb0UfaSQ5JvAl_tuwM2ZsMExMFhs,558
16
+ cachecontrol-0.14.2.dist-info/WHEEL,sha256=CpUCUxeHQbRN5UGRQHYRJorO5Af-Qy_fHMctcQ8DSGI,82
17
+ cachecontrol-0.14.2.dist-info/METADATA,sha256=dMsEu-cLt5UCDVb-HQEmJkmZ1srnjRNM3OIHptu5Yvg,3121
18
+ cachecontrol-0.14.2.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: flit 3.9.0
2
+ Generator: flit 3.10.1
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,18 +0,0 @@
1
- cachecontrol/__init__.py,sha256=SMGrJRpX7KUHcXGkaeWdDgI7dI1XAXzNPOir-i6KmcQ,640
2
- cachecontrol/_cmd.py,sha256=4l2UbK9N85Vr_KTkF4LjvSlsr-TZnOXTfT2NODlIMtc,1672
3
- cachecontrol/adapter.py,sha256=45loQAiioWqu4LMtMwtMOkfbxAgoQb3HVnsl-pmjouA,6247
4
- cachecontrol/cache.py,sha256=OTQj72tUf8C1uEgczdl3Gc8vkldSzsTITKtDGKMx4z8,1952
5
- cachecontrol/controller.py,sha256=Mn_xrzRWwvfU8iWvQJbsIyvhVSm9Rwlb69FeSGce90k,18503
6
- cachecontrol/filewrapper.py,sha256=STttGmIPBvZzt2b51dUOwoWX5crcMCpKZOisM3f5BNc,4292
7
- cachecontrol/heuristics.py,sha256=0FgcTfzFRQ9kUQFirhY3LOIg5wBWUsCBuxx_GGr_UuQ,4822
8
- cachecontrol/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
- cachecontrol/serialize.py,sha256=dP5k_UKYUAano6TKG9qlbKHbFMonz0AV_lYR3IFCGMg,5110
10
- cachecontrol/wrapper.py,sha256=KyaQ4Bq1RqsX-zA4pXH62nmcU511ECIGn5gCvmNIj20,1328
11
- cachecontrol/caches/__init__.py,sha256=3-BaJBXjdt-4iFbv-799XjzRBsd6zF9wOEqOdDuAAro,279
12
- cachecontrol/caches/file_cache.py,sha256=ylcOG9re-vjxiBi6Dm_wNP-KyK_i0NIop1OqHMAz460,5382
13
- cachecontrol/caches/redis_cache.py,sha256=94Qw4INGwjHcCDwMnOe0gbbcP2fttrlui-e1-Yutclw,1374
14
- cachecontrol-0.14.0.dist-info/entry_points.txt,sha256=1NtpBDZSbqMMbkIxhMBtvQoF7c5Mmfd7EE9hBPG_sLs,54
15
- cachecontrol-0.14.0.dist-info/LICENSE.txt,sha256=hu7uh74qQ_P_H1ZJb0UfaSQ5JvAl_tuwM2ZsMExMFhs,558
16
- cachecontrol-0.14.0.dist-info/WHEEL,sha256=EZbGkh7Ie4PoZfRQ8I0ZuP9VklN_TvcZ6DSE5Uar4z4,81
17
- cachecontrol-0.14.0.dist-info/METADATA,sha256=oLjmqL8HBxsz7qNjhDDhuRMaL_kfJbw4ED70tnNPCTk,3071
18
- cachecontrol-0.14.0.dist-info/RECORD,,