Docstrings and bug fixes in cookies.py
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 686db5d · Browse files · 1 file·+36 added−4 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+36 added−4 removed
- other:+36 added−4 removed
Provenance
- Commit
b7d0925- Tree
f22ce97- Author
Josh Imhoff <joshimhoff13@gmail.com>- Committer
Josh Imhoff <joshimhoff13@gmail.com>- Parent
686db5d
1 file changed+36 added−4 removed
modified:requests/cookies.py+36 added-4 removed
135 unmodified lines136 137 Unlike a regular CookieJar, this class is pickleable.138 """139 140+ # TODO test, test, test141+ # TODO should get and set require domain and/or path?142 def get(self, name, default=None, domain=None, path=None):143+ """Dict-like get() that also supports optional domain and path args in144+ order to resolve naming collisions from using one cookie jar over145+ multiple domains. Caution: operation is O(n), not O(1)."""146 try:147 return self._find(name, domain, path)148 except KeyError:149 return default150 151 def set(self, name, value, **kwargs):152+ """Dict-like set() that also supports optional domain and path args in153+ order to resolve naming collisions from using one cookie jar over154+ multiple domains."""155 # support client code that unsets cookies by assignment of a None value:156 if value is None:157 remove_cookie_by_name(self, name, domain=kwargs.get('domain'), path=kwargs.get('path'))158 return5 unmodified lines164 self.set_cookie(c)165 return c166 167 def keys():168+ """Dict-like keys() that returns a list of names of cookies from the jar.169+ See values() and items()."""170 keys = []171 for cookie in iter(self):172 keys.append(cookie.name)173 return keys174 175 def values():176+ """Dict-like values() that returns a list of values of cookies from the jar.177+ See keys() and items()."""178 values = []179 for cookie in iter(self):180 values.append(cookie.values)181 return values182 183 def items():184+ """Dict-like items() that returns a list of name-value tuples from the jar.185+ See keys() and values(). Allows client-code to call "dict(RequestsCookieJar)186+ and get a vanilla python dict of key value pairs."""187 items = []188 for cookie in iter(self):189 items.append((cookies.name, cookie.values))190 return items191 192 def list_domains(self):193+ """Utility method to list all the domains in the jar."""194 domains = []195 for cookie in iter(self):196 if cookie.domain not in domains:197 domains.append(cookie.domain)198 return domains199 200 def list_paths(self):201+ """Utility method to list all the paths in the jar."""202 paths = []203 for cookie in iter(self):204 if cookie.path not in paths:205 paths.append(cookie.path)206 return paths207 191- def get_dict(self, domain, path):208+ def get_dict(self, domain=None, path=None):209+ """Takes as an argument an optional domain and path and returns a plain old210+ Python dict of name-value pairs of cookies that meet the requirements."""211 dictionary = {}212 for cookie in iter(self):194- # TODO double check this logic213 if (domain == None or cookie.domain == domain) and (path == None 214 or cookie.path == path):215 dictionary[cookie.name] = cookie.value216 return dictionary217 218 def __getitem__(self, name):219+ """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 more221+ 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.')224 return self._find(name)225 226 def __setitem__(self, name, value):227+ """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+ 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.')233 self.set(name, value)234 235 def __delitem__(self, name):211- # consider testing for multiple domains236+ """Deletes a cookie given a name. Wraps cookielib.CookieJar's remove_cookie_by_name()."""237 remove_cookie_by_name(self, name)238 239 def _find(self, name, domain=None, path=None):240+ """Requests uses this method internally to find cookies. Takes as args name 241+ and optional domain and path. Returns a cookie.value. Throws KeyError242+ if cookie is not found."""243 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:1 unmodified line248 249 raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))250 251 def _multipleDomainsOrPaths(self):252+ """Returns True if there are multiple domains or paths in the jar.253+ Returns False otherwise."""254+ # TODO: possible bug -- always multiple paths, right?255 domains = []256 paths = []257 for cookie in iter(self):258 if cookie.domain is not None and cookie.domain in domains:3 unmodified lines262 return True263 paths.append(cookie.path)264 return False # there is only one domains and one path in jar265 266+ # TODO docstrings267 def __getstate__(self):268 state = self.__dict__.copy()269 # remove the unpickleable RLock object270 state.pop('_cookies_lock')4 unmodified lines275 if '_cookies_lock' not in self.__dict__:276 self._cookies_lock = threading.RLock()277 278 def copy(self):247- """We're probably better off forbidding this."""279+ """This is not currently implemented. Calling copy() will throw an exception."""280 raise NotImplementedError281 282 def create_cookie(name, value, **kwargs):283 """Make a cookie from underspecified parameters.68 unmodified lines