Skip to content
Myr

Copy a PreparedRequest's CookieJar reliably

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

When a PreparedRequests's cookie jar is not a RequestsCookieJar instance, it will not have a "copy" method. By adding _copy_cookie_jar we can reliably copy cookie jars so that we have an actual copy instead of the same instance on different prepared requests. This also updates the RequestsCookieJar.update logic to create copies of cookies from the other jar. Closes #2527

authored by Ian Cordasco · committed by Ian Cordasco · parent 18e678c · Browse files · 2 files+19 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

2 files+19 added−3 removed

  • other:+19 added−3 removed

2 files+19 added−3 removed

modified:requests/cookies.py+17 added-1 removed
4 unmodified lines
56requests.utils imports from here, so be careful with imports.7"""89import copy10import time11import collections12from .compat import cookielib, urlparse, urlunparse, Morsel13
288 unmodified lines
302 def update(self, other):303 """Updates this jar with cookies from another CookieJar or dict-like"""304 if isinstance(other, cookielib.CookieJar):305 for cookie in other:305 self.set_cookie(cookie)306 self.set_cookie(copy.copy(cookie))307 else:308 super(RequestsCookieJar, self).update(other)309310 def _find(self, name, domain=None, path=None):
48 unmodified lines
359 new_cj.update(self)360 return new_cj361362363def _copy_cookie_jar(jar):364 if jar is None:365 return None366367 if hasattr(jar, 'copy'):368 # We're dealing with an instane of RequestsCookieJar369 return jar.copy()370 # We're dealing with a generic CookieJar instance371 new_jar = copy.copy(jar)372 new_jar.clear()373 for cookie in jar:374 new_jar.set_cookie(copy.copy(cookie))375 return new_jar376377378def create_cookie(name, value, **kwargs):379 """Make a cookie from underspecified parameters.380381 By default, the pair of `name` and `value` will be set for the domain ''
98 unmodified lines
modified:requests/models.py+2 added-2 removed
13 unmodified lines
14from .hooks import default_hooks15from .structures import CaseInsensitiveDict1617from .auth import HTTPBasicAuth18from .cookies import cookiejar_from_dict, get_cookie_header18from .cookies import cookiejar_from_dict, get_cookie_header, _copy_cookie_jar19from .packages.urllib3.fields import RequestField20from .packages.urllib3.filepost import encode_multipart_formdata21from .packages.urllib3.util import parse_url22from .packages.urllib3.exceptions import (
296 unmodified lines
319 p = PreparedRequest()320 p.method = self.method321 p.url = self.url322 p.headers = self.headers.copy() if self.headers is not None else None323 p._cookies = self._cookies.copy() if self._cookies is not None else None323 p._cookies = _copy_cookie_jar(self._cookies)324 p.body = self.body325 p.hooks = self.hooks326 return p327
519 unmodified lines