In cookies.py, CookieConflictError is now thrown if there is more than one cookie with same name.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Josh Imhoff · committed by Josh Imhoff · parent b7d0925 · Browse files · 1 file·+18 added−19 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+18 added−19 removed
- other:+18 added−19 removed
Provenance
- Commit
9f26d0c- Tree
2e0d1c7- Author
Josh Imhoff <joshimhoff13@gmail.com>- Committer
Josh Imhoff <joshimhoff13@gmail.com>- Parent
b7d0925
1 file changed+18 added−19 removed
modified:requests/cookies.py+18 added-19 removed
118 unmodified lines119 120 for domain, path, name in clearables:121 cookiejar.clear(domain, path, name)122 123+class CookieConflictError(RuntimeError):124+ """There are two cookies that meet the criteria specified in the cookie jar. 125+ Use .get and .set and include domain and path args in order to be more specific."""126+127 class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):128 """Compatibility class; is a cookielib.CookieJar, but exposes a dict interface.129 130 This is the CookieJar we create by default for requests and sessions that10 unmodified lines141 Unlike a regular CookieJar, this class is pickleable.142 """143 144 # TODO test, test, test141- # TODO should get and set require domain and/or path?145 def get(self, name, default=None, domain=None, path=None):146 """Dict-like get() that also supports optional domain and path args in147 order to resolve naming collisions from using one cookie jar over148 multiple domains. Caution: operation is O(n), not O(1)."""70 unmodified lines219 return dictionary220 221 def __getitem__(self, name):222 """Dict-like __getitem__() for compatibility with client code. Throws exception220- if there are multiple domains or paths in the jar. In that case, use the more223+ if there are more than one cookie with name. In that case, use the more224 explicit get() method instead. Caution: operation is O(n), not O(1)."""222- if self._multipleDomainsOrPaths():223- raise KeyError('Multiple domains/paths in jar. Use .get instead.')225 return self._find(name)226 227 def __setitem__(self, name, value):228 """Dict-like __setitem__ for compatibility with client code. Throws exception228- if there are multiple domains or paths in the jar. In that case, use the more229+ if there is already a cookie of that name in the jar. In that case, use the more230 explicit set() method instead."""230- # TODO is this check nesc?231- if self._multipleDomainsOrPaths():232- raise KeyError('Multiple domains/paths in jar. Use .set instead.')231 self.set(name, value)232 233 def __delitem__(self, name):234 """Deletes a cookie given a name. Wraps cookielib.CookieJar's remove_cookie_by_name()."""1 unmodified line236 237 def _find(self, name, domain=None, path=None):238 """Requests uses this method internally to find cookies. Takes as args name 239 and optional domain and path. Returns a cookie.value. Throws KeyError242- if cookie is not found."""240+ if cookie is not found and CookieConflictError if there are multiple cookies241+ that match name and optionally domain and path."""242+ toReturn = None243 for cookie in iter(self):244 if cookie.name == name:245 if domain is None or cookie.domain == domain:246 if path is None or cookie.path == path:247- return cookie.value247+ if toReturn != None: # if there are multiple cookies that meet passed in criteria248+ raise CookieConflictError('There are multiple cookies with name, %r' % (name))249+ toReturn = cookie.value # we will eventually return this as long as no cookie conflict250 251+ if toReturn:252+ return toReturn253 raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))254 251- def _multipleDomainsOrPaths(self):252- """Returns True if there are multiple domains or paths in the jar.255+ def _multipleDomains(self):256+ """Returns True if there are multiple domains in the jar.257 Returns False otherwise."""254- # TODO: possible bug -- always multiple paths, right?258 domains = []256- paths = []259 for cookie in iter(self):260 if cookie.domain is not None and cookie.domain in domains:261 return True262 domains.append(cookie.domain)261- if cookie.path is not None and cookie.path in paths:262- return True263- paths.append(cookie.path)264- return False # there is only one domains and one path in jar263+ return False # there is only one domain in jar264 265 # TODO docstrings266 def __getstate__(self):267 state = self.__dict__.copy()6 unmodified lines274 if '_cookies_lock' not in self.__dict__:275 self._cookies_lock = threading.RLock()276 277 def copy(self):279- """This is not currently implemented. Calling copy() will throw an exception."""278+ """This is not implemented. Calling this will throw an exception."""279 raise NotImplementedError280 281 def create_cookie(name, value, **kwargs):282 """Make a cookie from underspecified parameters.68 unmodified lines