@@ -157,26 +157,28 @@ class CookieConflictError(RuntimeError):
157
157
158
158
159
159
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.
161
162
162
163
This is the CookieJar we create by default for requests and sessions that
163
164
don't specify one, since some clients may expect response.cookies and
164
165
session.cookies to support dict operations.
165
166
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``.
172
171
173
172
Unlike a regular CookieJar, this class is pickleable.
174
- """
175
173
174
+ .. warning:: dictionary operations that are normally O(1) may be O(n).
175
+ """
176
176
def get (self , name , default = None , domain = None , path = None ):
177
177
"""Dict-like get() that also supports optional domain and path args in
178
178
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)."""
180
182
try :
181
183
return self ._find_no_duplicates (name , domain , path )
182
184
except KeyError :
@@ -199,37 +201,38 @@ def set(self, name, value, **kwargs):
199
201
return c
200
202
201
203
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()."""
204
206
for cookie in iter (self ):
205
207
yield cookie .name
206
208
207
209
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()."""
210
212
return list (self .iterkeys ())
211
213
212
214
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()."""
215
217
for cookie in iter (self ):
216
218
yield cookie .value
217
219
218
220
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()."""
221
223
return list (self .itervalues ())
222
224
223
225
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()."""
226
228
for cookie in iter (self ):
227
229
yield cookie .name , cookie .value
228
230
229
231
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."""
233
236
return list (self .iteritems ())
234
237
235
238
def list_domains (self ):
@@ -259,8 +262,9 @@ def multiple_domains(self):
259
262
return False # there is only one domain in jar
260
263
261
264
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."""
264
268
dictionary = {}
265
269
for cookie in iter (self ):
266
270
if (domain is None or cookie .domain == domain ) and (path is None
@@ -269,21 +273,24 @@ def get_dict(self, domain=None, path=None):
269
273
return dictionary
270
274
271
275
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.
279
+
280
+ .. warning:: operation is O(n), not O(1)."""
275
281
276
282
return self ._find_no_duplicates (name )
277
283
278
284
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."""
285
+ """Dict-like __setitem__ for compatibility with client code. Throws
286
+ exception if there is already a cookie of that name in the jar. In that
287
+ case, use the more explicit set() method instead."""
282
288
283
289
self .set (name , value )
284
290
285
291
def __delitem__ (self , name ):
286
- """Deletes a cookie given a name. Wraps cookielib.CookieJar's remove_cookie_by_name()."""
292
+ """Deletes a cookie given a name. Wraps ``cookielib.CookieJar``'s
293
+ ``remove_cookie_by_name()``."""
287
294
remove_cookie_by_name (self , name )
288
295
289
296
def set_cookie (self , cookie , * args , ** kwargs ):
@@ -300,10 +307,11 @@ def update(self, other):
300
307
super (RequestsCookieJar , self ).update (other )
301
308
302
309
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."""
310
+ """Requests uses this method internally to get cookie values. Takes as
311
+ args name and optional domain and path. Returns a cookie.value. If
312
+ there are conflicting cookies, _find arbitrarily chooses one. See
313
+ _find_no_duplicates if you want an exception thrown if there are
314
+ conflicting cookies."""
307
315
for cookie in iter (self ):
308
316
if cookie .name == name :
309
317
if domain is None or cookie .domain == domain :
@@ -313,10 +321,11 @@ def _find(self, name, domain=None, path=None):
313
321
raise KeyError ('name=%r, domain=%r, path=%r' % (name , domain , path ))
314
322
315
323
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."""
324
+ """Both ``__get_item__`` and ``get`` call this function: it's never
325
+ used elsewhere in Requests. Takes as args name and optional domain and
326
+ path. Returns a cookie.value. Throws KeyError if cookie is not found
327
+ and CookieConflictError if there are multiple cookies that match name
328
+ and optionally domain and path."""
320
329
toReturn = None
321
330
for cookie in iter (self ):
322
331
if cookie .name == name :
@@ -440,7 +449,7 @@ def merge_cookies(cookiejar, cookies):
440
449
"""
441
450
if not isinstance (cookiejar , cookielib .CookieJar ):
442
451
raise ValueError ('You can only merge into CookieJar' )
443
-
452
+
444
453
if isinstance (cookies , dict ):
445
454
cookiejar = cookiejar_from_dict (
446
455
cookies , cookiejar = cookiejar , overwrite = False )
0 commit comments