Skip to content
Myr

Defined keys(), values(), and items() in order to support dict-like client interface. Now, we throw exceptions if __getitem__() or __setitem__() is used when multiple domains are in jar.

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 2d97466 · Browse files · 1 file+58 added−0 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+58 added−0 removed

  • other:+58 added−0 removed

1 file+58 added−0 removed

modified:requests/cookies.py+58 added
154 unmodified lines
155 c = create_cookie(name, value, **kwargs)156 self.set_cookie(c)157 return c158159 def keys():160 keys = []161 for cookie in iter(self):162 keys.append(cookie.name)163 return keys164165 def values():166 values = []167 for cookie in iter(self):168 values.append(cookie.values)169 return values170 171 def items():172 items = []173 for cookie in iter(self):174 items.append((cookies.name, cookie.values))175 return items176177 def list_domains(self):178 domains = []179 for cookie in iter(self):180 if cookie.domain not in domains:181 domains.append(cookie.domain)182 return domains183184 def list_paths(self):185 paths = []186 for cookie in iter(self):187 if cookie.path not in paths:188 paths.append(cookie.path)189 return paths190191 def get_dict(self, domain, path):192 dictionary = {}193 for cookie in iter(self):194 # TODO double check this logic195 if (domain == None or cookie.domain == domain) and (path == None 196 or cookie.path == path):197 dictionary[cookie.name] = cookie.value198 return dictionary199200 def __getitem__(self, name):201 if self._multipleDomainsOrPaths():202 raise KeyError('Multiple domains/paths in jar. Use .get instead.')203 return self._find(name)204205 def __setitem__(self, name, value):206 if self._multipleDomainsOrPaths():207 raise KeyError('Multiple domains/paths in jar. Use .set instead.')208 self.set(name, value)209210 def __delitem__(self, name):211 # consider testing for multiple domains212 remove_cookie_by_name(self, name)213214 def _find(self, name, domain=None, path=None):215 for cookie in iter(self):
3 unmodified lines
219 return cookie.value220221 raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))222223 def _multipleDomainsOrPaths(self):224 domains = []225 paths = []226 for cookie in iter(self):227 if cookie.domain is not None and cookie.domain in domains:228 return True229 domains.append(cookie.domain)230 if cookie.path is not None and cookie.path in paths:231 return True232 paths.append(cookie.path)233 return False # there is only one domains and one path in jar234235 def __getstate__(self):236 state = self.__dict__.copy()237 # remove the unpickleable RLock object238 state.pop('_cookies_lock')
81 unmodified lines