[Python-checkins] r53073 - python/trunk/Lib/cookielib.py
andrew.kuchling
python-checkins at python.org
Tue Dec 19 16:43:10 CET 2006
Author: andrew.kuchling
Date: Tue Dec 19 16:43:10 2006
New Revision: 53073
Modified:
python/trunk/Lib/cookielib.py
Log:
[Patch #1587139 by kxroberto] Protect lock acquisition/release with
try...finally to ensure the lock is always released. This could use
the 'with' statement, but the patch uses 'finally'.
2.5 backport candidate.
Modified: python/trunk/Lib/cookielib.py
==============================================================================
--- python/trunk/Lib/cookielib.py (original)
+++ python/trunk/Lib/cookielib.py Tue Dec 19 16:43:10 2006
@@ -1316,26 +1316,28 @@
"""
_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))
-
- # 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
+ cookies = self._cookies_for_request(request)
- self._cookies_lock.release()
+ attrs = self._cookie_attrs(cookies)
+ if attrs:
+ if not request.has_header("Cookie"):
+ request.add_unredirected_header(
+ "Cookie", "; ".join(attrs))
+
+ # 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 @@
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 @@
"""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 @@
"""
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 @@
"""
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 @@
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
More information about the Python-checkins
mailing list