fix regression with cookiejar.iteritems()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Konstantin Podshumok · committed by Konstantin Podshumok · parent 1832e33 · Browse files · 3 files·+53 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+53 added−11 removed
- other:+53 added−11 removed
Provenance
- Commit
537def4- Tree
401ca3c- Author
Konstantin Podshumok <kpp.live@gmail.com>- Committer
Konstantin Podshumok <kpp.live@gmail.com>- Parent
1832e33
3 files changed+53 added−11 removed
modified:requests/cookies.py+29 added-11 removed
6 unmodified lines7 """8 9 import time10 import collections11-from .compat import cookielib, urlparse, urlunparse, Morsel11+from .compat import cookielib, urlparse, urlunparse, Morsel, is_py312 13 try:14 import threading15 # grr, pyflakes: this fixes "redefinition of unused 'threading'"181 unmodified lines197 c = create_cookie(name, value, **kwargs)198 self.set_cookie(c)199 return c200 201+ def iterkeys(self):202+ """Dict-like iterkeys() that returns an iterator of names of cookies from the jar.203+ See itervalues() and iteritems()."""204+ for cookie in iter(self):205+ yield cookie.name206+207 def keys(self):208 """Dict-like keys() that returns a list of names of cookies from the jar.209 See values() and items()."""204- keys = []210+ if is_py3:211+ return self.iterkeys()212+ else:213+ return list(self.iterkeys())214+215+ def itervalues(self):216+ """Dict-like itervalues() that returns an iterator of values of cookies from the jar.217+ See iterkeys() and iteritems()."""218 for cookie in iter(self):206- keys.append(cookie.name)207- return keys219+ yield cookie.value220 221 def values(self):222 """Dict-like values() that returns a list of values of cookies from the jar.223 See keys() and items()."""212- values = []224+ if is_py3:225+ return self.itervalues()226+ else:227+ return list(self.itervalues())228+229+ def iteritems(self):230+ """Dict-like iteritems() that returns an iterator of name-value tuples from the jar.231+ See iterkeys() and itervalues()."""232 for cookie in iter(self):214- values.append(cookie.value)215- return values233+ yield cookie.name, cookie.value234 235 def items(self):236 """Dict-like items() that returns a list of name-value tuples from the jar.237 See keys() and values(). Allows client-code to call "dict(RequestsCookieJar)238 and get a vanilla python dict of key value pairs."""221- items = []222- for cookie in iter(self):223- items.append((cookie.name, cookie.value))224- return items239+ if is_py3:240+ return self.iteritems()241+ else:242+ return list(self.iteritems())243 244 def list_domains(self):245 """Utility method to list all the domains in the jar."""246 domains = []217 unmodified linesmodified:AUTHORS.rst+1 added
143 unmodified lines144 - Jayson Vantuyl <jayson@aggressive.ly> @kagato145 - Pengfei.X <pengphy@gmail.com>146 - Kamil Madac <kamil.madac@gmail.com>147 - Michael Becker <mike@beckerfuffle.com> @beckerfuffle148+- Константин Подшумок @podshumokmodified:test_requests.py+23 added
568 unmodified lines569 assert cookie.secure == secure570 assert cookie.domain == domain571 assert cookie._rest['HttpOnly'] == rest['HttpOnly']572 573+ def test_cookie_as_dict(self):574+ key = 'some_cookie'575+ value = 'some_value'576+577+ key1 = 'some_cookie1'578+ value1 = 'some_value1'579+580+ jar = requests.cookies.RequestsCookieJar()581+ jar.set(key, value)582+ jar.set(key1, value1)583+584+ d1 = dict(jar)585+ d2 = dict(jar.iteritems())586+ d3 = dict(jar.items())587+588+ assert len(d1) == 2589+ assert len(d2) == 2590+ assert len(d3) == 2591+592+ assert d1['some_cookie'] == 'some_value'593+ assert d2['some_cookie'] == 'some_value'594+ assert d3['some_cookie'] == 'some_value'595+596 def test_time_elapsed_blank(self):597 r = requests.get(httpbin('get'))598 td = r.elapsed599 total_seconds = ((td.microseconds + (td.seconds + td.days * 24 * 3600)492 unmodified lines