Skip to content

Commit

Permalink
fix regression with cookiejar.iteritems()
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Podshumok committed Dec 18, 2013
1 parent 1832e33 commit 537def4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,4 @@ Patches and Suggestions
- Pengfei.X <pengphy@gmail.com>
- Kamil Madac <kamil.madac@gmail.com>
- Michael Becker <mike@beckerfuffle.com> @beckerfuffle
- Константин Подшумок @podshumok
40 changes: 29 additions & 11 deletions requests/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import time
import collections
from .compat import cookielib, urlparse, urlunparse, Morsel
from .compat import cookielib, urlparse, urlunparse, Morsel, is_py3

try:
import threading
Expand Down Expand Up @@ -198,30 +198,48 @@ def set(self, name, value, **kwargs):
self.set_cookie(c)
return c

def iterkeys(self):
"""Dict-like iterkeys() that returns an iterator of names of cookies from the jar.
See itervalues() and iteritems()."""
for cookie in iter(self):
yield cookie.name

def keys(self):
"""Dict-like keys() that returns a list of names of cookies from the jar.
See values() and items()."""
keys = []
if is_py3:
return self.iterkeys()
else:
return list(self.iterkeys())

def itervalues(self):
"""Dict-like itervalues() that returns an iterator of values of cookies from the jar.
See iterkeys() and iteritems()."""
for cookie in iter(self):
keys.append(cookie.name)
return keys
yield cookie.value

def values(self):
"""Dict-like values() that returns a list of values of cookies from the jar.
See keys() and items()."""
values = []
if is_py3:
return self.itervalues()
else:
return list(self.itervalues())

def iteritems(self):
"""Dict-like iteritems() that returns an iterator of name-value tuples from the jar.
See iterkeys() and itervalues()."""
for cookie in iter(self):
values.append(cookie.value)
return values
yield cookie.name, cookie.value

def items(self):
"""Dict-like items() that returns a list of name-value tuples from the jar.
See keys() and values(). Allows client-code to call "dict(RequestsCookieJar)
and get a vanilla python dict of key value pairs."""
items = []
for cookie in iter(self):
items.append((cookie.name, cookie.value))
return items
if is_py3:
return self.iteritems()
else:
return list(self.iteritems())

def list_domains(self):
"""Utility method to list all the domains in the jar."""
Expand Down
23 changes: 23 additions & 0 deletions test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,29 @@ def test_cookie_parameters(self):
assert cookie.domain == domain
assert cookie._rest['HttpOnly'] == rest['HttpOnly']

def test_cookie_as_dict(self):
key = 'some_cookie'
value = 'some_value'

key1 = 'some_cookie1'
value1 = 'some_value1'

jar = requests.cookies.RequestsCookieJar()
jar.set(key, value)
jar.set(key1, value1)

d1 = dict(jar)
d2 = dict(jar.iteritems())
d3 = dict(jar.items())

assert len(d1) == 2
assert len(d2) == 2
assert len(d3) == 2

assert d1['some_cookie'] == 'some_value'
assert d2['some_cookie'] == 'some_value'
assert d3['some_cookie'] == 'some_value'

def test_time_elapsed_blank(self):
r = requests.get(httpbin('get'))
td = r.elapsed
Expand Down

0 comments on commit 537def4

Please sign in to comment.