summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook.git@proton.me>2022-10-20 13:11:04 -0500
committerGitHub <noreply@github.com>2022-10-20 13:11:04 -0500
commitacdbe633a80ae9a8d9deb20c1255124f7e159b33 (patch)
treed9dd9003a4297c268cff7096e4aaf7a81cba5254
parent2cfccd966876349ffbfd5a44a6db684966468b95 (diff)
parent54d85c12bf4aa98e2798f18b7c4719b1ba493770 (diff)
downloadrequests-cache-acdbe633a80ae9a8d9deb20c1255124f7e159b33.tar.gz
Merge pull request #708 from requests-cache/sliced-pickles
Explicitly disable pickling CachedSession objects
-rw-r--r--HISTORY.md22
-rw-r--r--requests_cache/session.py6
-rw-r--r--tests/unit/test_session.py6
3 files changed, 31 insertions, 3 deletions
diff --git a/HISTORY.md b/HISTORY.md
index b3ba499..d4a03bb 100644
--- a/HISTORY.md
+++ b/HISTORY.md
@@ -92,8 +92,12 @@
* Fix usage of memory backend with `install_cache()`
* Fix issue on Windows with occasional missing `CachedResponse.created_at` timestamp
* Add `CachedRequest.path_url` property for compatibility with `RequestEncodingMixin`
-* Add compatibility with cattrs 22.1+
* Fix potential `AttributeError` due to undetected imports when requests-cache is bundled in a PyInstaller package
+* Fix `AttributeError` when attempting to unpickle a `CachedSession` object, and instead disable
+ pickling by raising a `NotImplementedError`
+* Add compatibility with cattrs 22.1+
+* Fix forwarding connection parameters passed to `RedisCache` for redis-py 4.2 and python <=3.8
+* Fix forwarding connection parameters passed to `MongoCache` for pymongo 4.1 and python <=3.8
**Dependencies:**
* Replace `appdirs` with `platformdirs`
@@ -129,15 +133,27 @@ If you encounter a problem not listed here after updating to 1.0, please create
* Internal utility module changes:
* The `cache_control` module (added in `0.7`) has been split up into multiple modules in a new `policy` subpackage
-### 0.9.6 (2022-08-24)
+## 0.9.7 (Unreleased)
+Backport fixes from 1.0:
+* Fix potential `AttributeError` due to undetected imports when requests-cache is bundled in a PyInstaller package
+* Fix `AttributeError` when attempting to unpickle a `CachedSession` object, and instead disable
+ pickling by raising a `NotImplementedError`
+* Update to cattrs 22.2
+
+## 0.9.6 (2022-08-24)
+Backport fixes from 1.0:
* Remove potentially problematic row count from `BaseCache.__str__()`
* Remove upper version constraints for all non-dev dependencies
* Make dependency specification consistent between PyPI and Conda-Forge packages
### 0.9.5 (2022-06-29)
-* Backport bugfixes from 1.0
+Backport fixes from 1.0:
+* Fix usage of memory backend with `install_cache()`
+* Add `CachedRequest.path_url` property
+* Add compatibility with cattrs 22.1
### 0.9.4 (2022-04-22)
+Backport fixes from 1.0:
* Fix forwarding connection parameters passed to `RedisCache` for redis-py 4.2 and python <=3.8
* Fix forwarding connection parameters passed to `MongoCache` for pymongo 4.1 and python <=3.8
diff --git a/requests_cache/session.py b/requests_cache/session.py
index e58cc57..3dbdb05 100644
--- a/requests_cache/session.py
+++ b/requests_cache/session.py
@@ -311,6 +311,12 @@ class CacheMixin(MIXIN_BASE):
"""**Deprecated:** Use ``session.cache.remove(expired=True)`` instead"""
self.cache.delete(expired=True, invalid=True)
+ def __getstate__(self):
+ # Unlike requests.Session, CachedSession may contain backend connection objects that can't
+ # be pickled. Support for this could be added if necessary, but for now it's explicitly
+ # disabled to avoid confusing errors upon unpickling.
+ raise NotImplementedError('CachedSession cannot be pickled')
+
def __repr__(self):
return f'<CachedSession(cache={repr(self.cache)}, settings={self.settings})>'
diff --git a/tests/unit/test_session.py b/tests/unit/test_session.py
index 53e368c..60e8bb4 100644
--- a/tests/unit/test_session.py
+++ b/tests/unit/test_session.py
@@ -1,5 +1,6 @@
"""CachedSession tests that use mocked responses only"""
import json
+import pickle
from collections import UserDict, defaultdict
from datetime import datetime, timedelta
from logging import getLogger
@@ -76,6 +77,11 @@ def test_repr(mock_session):
assert 'cache_control=True' in repr(mock_session)
+def test_pickle__disabled():
+ with pytest.raises(NotImplementedError):
+ pickle.dumps(CachedSession(backend='memory'))
+
+
def test_response_defaults(mock_session):
"""Both cached and new responses should always have the following attributes"""
mock_session.settings.expire_after = datetime.utcnow() + timedelta(days=1)