[Yum-devel] [UG] [PATCH] keepalive.py, python2.4 and HANDLE_ERRORS=0

Alexis S. L. Carvalho alexis at cecm.usp.br
Tue Apr 4 03:07:05 UTC 2006


Hi

I've noticed a small bug in keepalive.py's error handling when run with
python2.4 and HANDLE_ERRORS = 0.  You can see it by just running the
tests that are in the file itself.

With python 2.2 and 2.3, if you give it some URL that returns a 404,
you'll get:

"""
$ python2.2 keepalive.py 1 http://www.google.com/does/not/exist
checking error hander (do this on a non-200)
  fancy error handling off (HANDLE_ERRORS = 0)
  status = 404, reason = Not Found
  fancy error handling on (HANDLE_ERRORS = 1)
  EXCEPTION: HTTP Error 404: Not Found
exiting - exception will prevent further tests
"""

With python 2.4, you get:

"""
$ python2.4 keepalive.py 1 http://www.google.com/does/not/exist
checking error hander (do this on a non-200)
  fancy error handling off (HANDLE_ERRORS = 0)
  EXCEPTION: HTTP Error 404: Content-Type: text/html
Server: GWS/2.1
Content-Length: 1362
Date: Tue, 04 Apr 2006 02:39:20 GMT

exiting - exception will prevent further tests
"""

Notice how, instead of "Not Found", it prints the headers that the
server sent back.

This is because in python2.4, urllib2.HTTPErrorProcessor wants to access
the "Not Found" (or equivalent) string through the "msg" attribute of
the object passed to it.

The attached patch fixes this by creating a small wrapper around the
HTTPResponse object that HTTPHandler.do_open returns.

Alexis
-------------- next part --------------
diff -r 6acca4cd7fd1 -r 2f69d75a7298 urlgrabber/keepalive.py
--- a/urlgrabber/keepalive.py	Mon Apr 03 23:32:46 2006 -0300
+++ b/urlgrabber/keepalive.py	Mon Apr 03 23:33:18 2006 -0300
@@ -171,6 +171,16 @@ class ConnectionManager:
             return list(self._hostmap.get(host, []))
         else:
             return dict(self._hostmap)
+
+# In python 2.4, HTTPErrorProcessor wants to access the english 
+# translation of the status as the "msg" attribute
+class _ResponseWrapper(object):
+    def __init__(self, obj):
+        self.obj = obj
+        self.msg = obj.reason
+
+    def __getattr__(self, name):
+        return getattr(self.obj, name)
 
 class HTTPHandler(urllib2.HTTPHandler):
     def __init__(self):
@@ -251,7 +261,7 @@ class HTTPHandler(urllib2.HTTPHandler):
         r.code = r.status
         
         if r.status == 200 or not HANDLE_ERRORS:
-            return r
+            return _ResponseWrapper(r)
         else:
             return self.parent.error('http', req, r, r.status, r.reason, r.msg)
 


More information about the Yum-devel mailing list