diff --git a/src/requests/cookies.py b/src/requests/cookies.py index f69d0cda9e..48359634d0 100644 --- a/src/requests/cookies.py +++ b/src/requests/cookies.py @@ -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 @@ -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. @@ -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 @@ -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 @@ -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. diff --git a/tests/test_requests.py b/tests/test_requests.py index 257d9d7ab1..160a2ef26c 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -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"