Skip to content

Commit f4ec3d2

Browse files
committed
Clean up cookie docs and display them.
1 parent b831317 commit f4ec3d2

File tree

2 files changed

+54
-40
lines changed

2 files changed

+54
-40
lines changed

docs/api.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ Cookies
8888
.. autofunction:: requests.utils.cookiejar_from_dict
8989
.. autofunction:: requests.utils.add_dict_to_cookiejar
9090

91+
.. autoclass:: requests.cookies.RequestsCookieJar
92+
:inherited-members:
93+
94+
.. autoclass:: requests.cookies.CookieConflictError
95+
:inherited-members:
96+
9197

9298
Encodings
9399
~~~~~~~~~

requests/cookies.py

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -157,26 +157,28 @@ class CookieConflictError(RuntimeError):
157157

158158

159159
class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
160-
"""Compatibility class; is a cookielib.CookieJar, but exposes a dict interface.
160+
"""Compatibility class; is a cookielib.CookieJar, but exposes a dict
161+
interface.
161162
162163
This is the CookieJar we create by default for requests and sessions that
163164
don't specify one, since some clients may expect response.cookies and
164165
session.cookies to support dict operations.
165166
166-
Don't use the dict interface internally; it's just for compatibility with
167-
with external client code. All `requests` code should work out of the box
168-
with externally provided instances of CookieJar, e.g., LWPCookieJar and
169-
FileCookieJar.
170-
171-
Caution: dictionary operations that are normally O(1) may be O(n).
167+
Requests does not use the dict interface internally; it's just for
168+
compatibility with external client code. All `requests` code should work
169+
out of the box with externally provided instances of ``CookieJar``, e.g.
170+
``LWPCookieJar`` and ``FileCookieJar``.
172171
173172
Unlike a regular CookieJar, this class is pickleable.
174-
"""
175173
174+
.. warning:: dictionary operations that are normally O(1) may be O(n).
175+
"""
176176
def get(self, name, default=None, domain=None, path=None):
177177
"""Dict-like get() that also supports optional domain and path args in
178178
order to resolve naming collisions from using one cookie jar over
179-
multiple domains. Caution: operation is O(n), not O(1)."""
179+
multiple domains.
180+
181+
.. warning:: operation is O(n), not O(1)."""
180182
try:
181183
return self._find_no_duplicates(name, domain, path)
182184
except KeyError:
@@ -199,37 +201,38 @@ def set(self, name, value, **kwargs):
199201
return c
200202

201203
def iterkeys(self):
202-
"""Dict-like iterkeys() that returns an iterator of names of cookies from the jar.
203-
See itervalues() and iteritems()."""
204+
"""Dict-like iterkeys() that returns an iterator of names of cookies
205+
from the jar. See itervalues() and iteritems()."""
204206
for cookie in iter(self):
205207
yield cookie.name
206208

207209
def keys(self):
208-
"""Dict-like keys() that returns a list of names of cookies from the jar.
209-
See values() and items()."""
210+
"""Dict-like keys() that returns a list of names of cookies from the
211+
jar. See values() and items()."""
210212
return list(self.iterkeys())
211213

212214
def itervalues(self):
213-
"""Dict-like itervalues() that returns an iterator of values of cookies from the jar.
214-
See iterkeys() and iteritems()."""
215+
"""Dict-like itervalues() that returns an iterator of values of cookies
216+
from the jar. See iterkeys() and iteritems()."""
215217
for cookie in iter(self):
216218
yield cookie.value
217219

218220
def values(self):
219-
"""Dict-like values() that returns a list of values of cookies from the jar.
220-
See keys() and items()."""
221+
"""Dict-like values() that returns a list of values of cookies from the
222+
jar. See keys() and items()."""
221223
return list(self.itervalues())
222224

223225
def iteritems(self):
224-
"""Dict-like iteritems() that returns an iterator of name-value tuples from the jar.
225-
See iterkeys() and itervalues()."""
226+
"""Dict-like iteritems() that returns an iterator of name-value tuples
227+
from the jar. See iterkeys() and itervalues()."""
226228
for cookie in iter(self):
227229
yield cookie.name, cookie.value
228230

229231
def items(self):
230-
"""Dict-like items() that returns a list of name-value tuples from the jar.
231-
See keys() and values(). Allows client-code to call "dict(RequestsCookieJar)
232-
and get a vanilla python dict of key value pairs."""
232+
"""Dict-like items() that returns a list of name-value tuples from the
233+
jar. See keys() and values(). Allows client-code to call
234+
``dict(RequestsCookieJar)`` and get a vanilla python dict of key value
235+
pairs."""
233236
return list(self.iteritems())
234237

235238
def list_domains(self):
@@ -259,8 +262,9 @@ def multiple_domains(self):
259262
return False # there is only one domain in jar
260263

261264
def get_dict(self, domain=None, path=None):
262-
"""Takes as an argument an optional domain and path and returns a plain old
263-
Python dict of name-value pairs of cookies that meet the requirements."""
265+
"""Takes as an argument an optional domain and path and returns a plain
266+
old Python dict of name-value pairs of cookies that meet the
267+
requirements."""
264268
dictionary = {}
265269
for cookie in iter(self):
266270
if (domain is None or cookie.domain == domain) and (path is None
@@ -269,21 +273,23 @@ def get_dict(self, domain=None, path=None):
269273
return dictionary
270274

271275
def __getitem__(self, name):
272-
"""Dict-like __getitem__() for compatibility with client code. Throws exception
273-
if there are more than one cookie with name. In that case, use the more
274-
explicit get() method instead. Caution: operation is O(n), not O(1)."""
276+
"""Dict-like __getitem__() for compatibility with client code. Throws
277+
exception if there are more than one cookie with name. In that case,
278+
use the more explicit get() method instead. Caution: operation is O(n),
279+
not O(1)."""
275280

276281
return self._find_no_duplicates(name)
277282

278283
def __setitem__(self, name, value):
279-
"""Dict-like __setitem__ for compatibility with client code. Throws exception
280-
if there is already a cookie of that name in the jar. In that case, use the more
281-
explicit set() method instead."""
284+
"""Dict-like __setitem__ for compatibility with client code. Throws
285+
exception if there is already a cookie of that name in the jar. In that
286+
case, use the more explicit set() method instead."""
282287

283288
self.set(name, value)
284289

285290
def __delitem__(self, name):
286-
"""Deletes a cookie given a name. Wraps cookielib.CookieJar's remove_cookie_by_name()."""
291+
"""Deletes a cookie given a name. Wraps ``cookielib.CookieJar``'s
292+
``remove_cookie_by_name()``."""
287293
remove_cookie_by_name(self, name)
288294

289295
def set_cookie(self, cookie, *args, **kwargs):
@@ -300,10 +306,11 @@ def update(self, other):
300306
super(RequestsCookieJar, self).update(other)
301307

302308
def _find(self, name, domain=None, path=None):
303-
"""Requests uses this method internally to get cookie values. Takes as args name
304-
and optional domain and path. Returns a cookie.value. If there are conflicting cookies,
305-
_find arbitrarily chooses one. See _find_no_duplicates if you want an exception thrown
306-
if there are conflicting cookies."""
309+
"""Requests uses this method internally to get cookie values. Takes as
310+
args name and optional domain and path. Returns a cookie.value. If
311+
there are conflicting cookies, _find arbitrarily chooses one. See
312+
_find_no_duplicates if you want an exception thrown if there are
313+
conflicting cookies."""
307314
for cookie in iter(self):
308315
if cookie.name == name:
309316
if domain is None or cookie.domain == domain:
@@ -313,10 +320,11 @@ def _find(self, name, domain=None, path=None):
313320
raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
314321

315322
def _find_no_duplicates(self, name, domain=None, path=None):
316-
"""__get_item__ and get call _find_no_duplicates -- never used in Requests internally.
317-
Takes as args name and optional domain and path. Returns a cookie.value.
318-
Throws KeyError if cookie is not found and CookieConflictError if there are
319-
multiple cookies that match name and optionally domain and path."""
323+
"""__get_item__ and get call _find_no_duplicates -- never used in
324+
Requests internally. Takes as args name and optional domain and path.
325+
Returns a cookie.value. Throws KeyError if cookie is not found and
326+
CookieConflictError if there are multiple cookies that match name and
327+
optionally domain and path."""
320328
toReturn = None
321329
for cookie in iter(self):
322330
if cookie.name == name:
@@ -440,7 +448,7 @@ def merge_cookies(cookiejar, cookies):
440448
"""
441449
if not isinstance(cookiejar, cookielib.CookieJar):
442450
raise ValueError('You can only merge into CookieJar')
443-
451+
444452
if isinstance(cookies, dict):
445453
cookiejar = cookiejar_from_dict(
446454
cookies, cookiejar=cookiejar, overwrite=False)

0 commit comments

Comments
 (0)