Skip to content
Myr

Properly serialize RecentlyUsedContainers for cache

Before adoption: this commit came in with the history the project adopted, so no landing records it.

RecentlyUsedContainers are threadsafe so they require a lock and as such cannot be serialized with pickle directly. To handle it, we need to convert it to a dictionary first and then back when deserializing. Fixes #2345

authored by Ian Cordasco · committed by Ian Cordasco · parent 431282e · Browse files · 1 file+12 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+12 added−3 removed

  • other:+12 added−3 removed

1 file+12 added−3 removed

modified:requests/sessions.py+12 added-3 removed
34 unmodified lines
3536# formerly defined here, reexposed here for backward compatibility37from .models import REDIRECT_STATI3839REDIRECT_CACHE_SIZE = 1000404142def merge_setting(request_setting, session_setting, dict_class=OrderedDict):43 """44 Determines appropriate setting for a given request, taking into account the45 explicit setting on that request, and the setting in the session. If a
229 unmodified lines
275276 __attrs__ = [277 'headers', 'cookies', 'auth', 'proxies', 'hooks', 'params', 'verify',278 'cert', 'prefetch', 'adapters', 'stream', 'trust_env',277 'max_redirects', 'redirect_cache'279 'max_redirects',280 ]281282 def __init__(self):283
46 unmodified lines
330 self.mount('https://', HTTPAdapter())331 self.mount('http://', HTTPAdapter())332333 # Only store 1000 redirects to prevent using infinite memory332 self.redirect_cache = RecentlyUsedContainer(1000)334 self.redirect_cache = RecentlyUsedContainer(REDIRECT_CACHE_SIZE)335336 def __enter__(self):337 return self338
322 unmodified lines
661 for key in keys_to_move:662 self.adapters[key] = self.adapters.pop(key)663664 def __getstate__(self):663 return dict((attr, getattr(self, attr, None)) for attr in self.__attrs__)665 state = dict((attr, getattr(self, attr, None)) for attr in self.__attrs__)666 state['redirect_cache'] = dict(self.redirect_cache)667 return state668669 def __setstate__(self, state):670 redirect_cache = state.pop('redirect_cache', {})671 for attr, value in state.items():672 setattr(self, attr, value)673674 self.redirect_cache = RecentlyUsedContainer(REDIRECT_CACHE_SIZE)675 for redirect, to in redirect_cache.items():676 self.redirect_cache[redirect] = to677678679def session():680 """Returns a :class:`Session` for context-management."""681682 return Session()