summaryrefslogtreecommitdiffstats
path: root/Lib/cookielib.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/cookielib.py')
-rw-r--r--Lib/cookielib.py99
1 files changed, 56 insertions, 43 deletions
diff --git a/Lib/cookielib.py b/Lib/cookielib.py
index e8fee0e..ce037b0 100644
--- a/Lib/cookielib.py
+++ b/Lib/cookielib.py
@@ -1316,26 +1316,28 @@ class CookieJar:
"""
_debug("add_cookie_header")
self._cookies_lock.acquire()
+ try:
- self._policy._now = self._now = int(time.time())
-
- cookies = self._cookies_for_request(request)
+ self._policy._now = self._now = int(time.time())
- attrs = self._cookie_attrs(cookies)
- if attrs:
- if not request.has_header("Cookie"):
- request.add_unredirected_header(
- "Cookie", "; ".join(attrs))
+ cookies = self._cookies_for_request(request)
- # if necessary, advertise that we know RFC 2965
- if (self._policy.rfc2965 and not self._policy.hide_cookie2 and
- not request.has_header("Cookie2")):
- for cookie in cookies:
- if cookie.version != 1:
- request.add_unredirected_header("Cookie2", '$Version="1"')
- break
+ attrs = self._cookie_attrs(cookies)
+ if attrs:
+ if not request.has_header("Cookie"):
+ request.add_unredirected_header(
+ "Cookie", "; ".join(attrs))
- self._cookies_lock.release()
+ # if necessary, advertise that we know RFC 2965
+ if (self._policy.rfc2965 and not self._policy.hide_cookie2 and
+ not request.has_header("Cookie2")):
+ for cookie in cookies:
+ if cookie.version != 1:
+ request.add_unredirected_header("Cookie2", '$Version="1"')
+ break
+
+ finally:
+ self._cookies_lock.release()
self.clear_expired_cookies()
@@ -1602,12 +1604,15 @@ class CookieJar:
def set_cookie_if_ok(self, cookie, request):
"""Set a cookie if policy says it's OK to do so."""
self._cookies_lock.acquire()
- self._policy._now = self._now = int(time.time())
+ try:
+ self._policy._now = self._now = int(time.time())
- if self._policy.set_ok(cookie, request):
- self.set_cookie(cookie)
+ if self._policy.set_ok(cookie, request):
+ self.set_cookie(cookie)
+
- self._cookies_lock.release()
+ finally:
+ self._cookies_lock.release()
def set_cookie(self, cookie):
"""Set a cookie, without checking whether or not it should be set."""
@@ -1626,13 +1631,15 @@ class CookieJar:
"""Extract cookies from response, where allowable given the request."""
_debug("extract_cookies: %s", response.info())
self._cookies_lock.acquire()
- self._policy._now = self._now = int(time.time())
+ try:
+ self._policy._now = self._now = int(time.time())
- for cookie in self.make_cookies(response, request):
- if self._policy.set_ok(cookie, request):
- _debug(" setting cookie: %s", cookie)
- self.set_cookie(cookie)
- self._cookies_lock.release()
+ for cookie in self.make_cookies(response, request):
+ if self._policy.set_ok(cookie, request):
+ _debug(" setting cookie: %s", cookie)
+ self.set_cookie(cookie)
+ finally:
+ self._cookies_lock.release()
def clear(self, domain=None, path=None, name=None):
"""Clear some cookies.
@@ -1669,10 +1676,12 @@ class CookieJar:
"""
self._cookies_lock.acquire()
- for cookie in self:
- if cookie.discard:
- self.clear(cookie.domain, cookie.path, cookie.name)
- self._cookies_lock.release()
+ try:
+ for cookie in self:
+ if cookie.discard:
+ self.clear(cookie.domain, cookie.path, cookie.name)
+ finally:
+ self._cookies_lock.release()
def clear_expired_cookies(self):
"""Discard all expired cookies.
@@ -1685,11 +1694,13 @@ class CookieJar:
"""
self._cookies_lock.acquire()
- now = time.time()
- for cookie in self:
- if cookie.is_expired(now):
- self.clear(cookie.domain, cookie.path, cookie.name)
- self._cookies_lock.release()
+ try:
+ now = time.time()
+ for cookie in self:
+ if cookie.is_expired(now):
+ self.clear(cookie.domain, cookie.path, cookie.name)
+ finally:
+ self._cookies_lock.release()
def __iter__(self):
return deepvalues(self._cookies)
@@ -1761,16 +1772,18 @@ class FileCookieJar(CookieJar):
else: raise ValueError(MISSING_FILENAME_TEXT)
self._cookies_lock.acquire()
-
- old_state = copy.deepcopy(self._cookies)
- self._cookies = {}
try:
- self.load(filename, ignore_discard, ignore_expires)
- except (LoadError, IOError):
- self._cookies = old_state
- raise
- self._cookies_lock.release()
+ old_state = copy.deepcopy(self._cookies)
+ self._cookies = {}
+ try:
+ self.load(filename, ignore_discard, ignore_expires)
+ except (LoadError, IOError):
+ self._cookies = old_state
+ raise
+
+ finally:
+ self._cookies_lock.release()
from _LWPCookieJar import LWPCookieJar, lwp_cookie_str
from _MozillaCookieJar import MozillaCookieJar