Be more flexible when looking for "timed out" exceptions.
authorAdam Sampson <ats@offog.org>
Mon, 15 Sep 2014 22:09:29 +0000 (23:09 +0100)
committerAdam Sampson <ats@offog.org>
Mon, 15 Sep 2014 22:09:29 +0000 (23:09 +0100)
Debian sid's current Python package produces slightly different
exceptions from stock Python, which caused this check to fail. I've
moved the check out to a function, which'll now match anything with
"timeout", "time out" or "timed out" in it.

NEWS
rawdoglib/rawdog.py

diff --git a/NEWS b/NEWS
index 870215e359949356fecb418ed3f7da50d772a281..a578f8420bcf6c30b046a28f3524e16b244d0c8f 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,11 @@ Add a test for the maxage option (suggested by joelmo).
 
 Add a rule to style.css to scale down large images.
 
+When looking for SSL timeout exceptions, match any form of "timeout",
+"time out" or "timed out" in the message. This makes rawdog detect SSL
+connection timeouts correctly with Debian's Python 2.7.8 package
+(reported by David Suárez).
+
 - rawdog 2.19
 
 Make test-rawdog not depend on having a host it can test connection
index 9947def280253d88c64892d812fa0a7e13f98f44..fe1bc23b52c065cebd993b1b551eb6fa3fa5681b 100644 (file)
@@ -325,6 +325,25 @@ def ensure_unicode(value, encoding):
        else:
                return value
 
+timeout_re = re.compile(r'timed? ?out', re.I)
+def is_timeout_exception(exc):
+       """Return True if the given exception object suggests that a timeout
+       occurred, else return False."""
+
+       # Since urlopen throws away the original exception object,
+       # we have to look at the stringified form to tell if it was a timeout.
+       # (We're in reasonable company here, since test_ssl.py in the Python
+       # distribution does the same thing!)
+       #
+       # The message we're looking for is something like:
+       # Stock Python 2.7.7 and 2.7.8:
+       #   <urlopen error _ssl.c:495: The handshake operation timed out>
+       # Debian python 2.7.3-4+deb7u1:
+       #   <urlopen error _ssl.c:489: The handshake operation timed out>
+       # Debian python 2.7.8-1:
+       #   <urlopen error ('_ssl.c:563: The handshake operation timed out',)>
+       return (timeout_re.search(str(exc)) is not None)
+
 class BasicAuthProcessor(urllib2.BaseHandler):
        """urllib2 handler that does HTTP basic authentication
        or proxy authentication with a fixed username and password.
@@ -516,7 +535,7 @@ class Feed:
                if got_urlerror or got_timeout:
                        # urllib2 reported an error when fetching the feed.
                        # Check to see if it was a timeout.
-                       if not (got_timeout or str(bozo_exception).endswith("timed out>")):
+                       if not (got_timeout or is_timeout_exception(bozo_exception)):
                                errors.append("Error while fetching feed:")
                                errors.append(str(bozo_exception))
                                errors.append("")