Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/requests/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import time

from ._internal_utils import to_native_string
from .compat import Morsel, MutableMapping, cookielib, urlparse, urlunparse
from .compat import Morsel, cookielib, urlparse, urlunparse

try:
import threading
Expand Down Expand Up @@ -173,7 +173,7 @@ class CookieConflictError(RuntimeError):
"""


class RequestsCookieJar(cookielib.CookieJar, MutableMapping):
class RequestsCookieJar(cookielib.CookieJar):
"""Compatibility class; is a http.cookiejar.CookieJar, but exposes a dict
interface.

Expand Down Expand Up @@ -320,9 +320,12 @@ def get_dict(self, domain=None, path=None):

def __contains__(self, name):
try:
return super().__contains__(name)
self._find_no_duplicates(name)
except CookieConflictError:
return True
except KeyError:
return False
return True

def __getitem__(self, name):
"""Dict-like __getitem__() for compatibility with client code. Throws
Expand All @@ -333,6 +336,11 @@ def __getitem__(self, name):
"""
return self._find_no_duplicates(name)

def __eq__(self, other):
if not hasattr(other, "items"):
return NotImplemented
return list(self.items()) == list(other.items())

def __setitem__(self, name, value):
"""Dict-like __setitem__ for compatibility with client code. Throws
exception if there is already a cookie of that name in the jar. In that
Expand Down Expand Up @@ -361,7 +369,8 @@ def update(self, other):
for cookie in other:
self.set_cookie(copy.copy(cookie))
else:
super().update(other)
for name, value in other.items():
self.set(name, value)

def _find(self, name, domain=None, path=None):
"""Requests uses this method internally to get cookie values.
Expand Down
5 changes: 5 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,11 @@ def test_cookie_parameters(self):
assert cookie.domain == domain
assert cookie._rest["HttpOnly"] == rest["HttpOnly"]

def test_cookie_jar_is_not_a_mutable_mapping(self):
jar = requests.cookies.RequestsCookieJar()

assert not isinstance(jar, MutableMapping)

def test_cookie_as_dict_keeps_len(self):
key = "some_cookie"
value = "some_value"
Expand Down