Implemented #439. Added supporting test.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Shrikant Sharat · committed by Shrikant Sharat · parent 7548bc7 · Browse files · 2 files·+31 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+31 added−8 removed
- tests:+14 added−0 removed
- other:+17 added−8 removed
Provenance
- Commit
590ce29- Tree
f88dfd6- Author
Shrikant Sharat <shrikantsharat.k@gmail.com>- Committer
Shrikant Sharat <shrikantsharat.k@gmail.com>- Parent
7548bc7
2 files changed+31 added−8 removed
modified:requests/sessions.py+17 added-8 removed
50 unmodified lines51 """A Requests session."""52 53 __attrs__ = [54 'headers', 'cookies', 'auth', 'timeout', 'proxies', 'hooks',55- 'params', 'config']55+ 'params', 'config', 'verify']56 57 58 def __init__(self,59 headers=None,18 unmodified lines78 79 for (k, v) in list(defaults.items()):80 self.config.setdefault(k, v)81 82- self.poolmanager = PoolManager(83- num_pools=self.config.get('pool_connections'),84- maxsize=self.config.get('pool_maxsize')85- )82+ self.init_poolmanager()83 84 # Set up a CookieJar to be used by default85 self.cookies = {}86 87 # Add passed cookies in.88 if cookies is not None:89 self.cookies.update(cookies)90 91+ def init_poolmanager(self):92+ self.poolmanager = PoolManager(93+ num_pools=self.config.get('pool_connections'),94+ maxsize=self.config.get('pool_maxsize')95+ )96+97 def __repr__(self):98 return '<requests-client at 0x%x>' % (id(self))99 100 def __enter__(self):47 unmodified lines148 headers = {} if headers is None else headers149 params = {} if params is None else params150 hooks = {} if hooks is None else hooks151 149- if verify is None:150- verify = self.verify151-152 # use session's hooks as defaults153 for key, cb in list(self.hooks.items()):154 hooks.setdefault(key, cb)155 124 unmodified lines280 """281 282 return self.request('delete', url, **kwargs)283 284+ def __getstate__(self):285+ return dict((attr, getattr(self, attr, None)) for attr in self.__attrs__)286 287+ def __setstate__(self, state):288+ for attr, value in state.items():289+ setattr(self, attr, value)290+291+ self.init_poolmanager()292+293+294 def session(**kwargs):295 """Returns a :class:`Session` for context-management."""296 297 return Session(**kwargs)modified:tests/test_requests.py+14 added
11 unmodified lines12 import time13 import os14 import sys15 import unittest16+import pickle17 18 import requests19 from requests.compat import str, bytes, StringIO20 # import envoy708 unmodified lines729 assert not params['a'] in r3.text730 assert params3['b'] in r3.text731 assert params3['c'] in r3.text732 733+ def test_session_pickling(self):734+735+ s = requests.session(736+ headers={'header': 'value'},737+ cookies={'a-cookie': 'cookie-value'},738+ auth=('username', 'password'))739+740+ ds = pickle.loads(pickle.dumps(s))741+742+ self.assertEqual(s.headers, ds.headers)743+ self.assertEqual(s.cookies, ds.cookies)744+ self.assertEqual(s.auth, ds.auth)745+746 def test_invalid_content(self):747 # WARNING: if you're using a terrible DNS provider (comcast),748 # this will fail.749 try:84 unmodified lines