pumazi/wsgi_intercept2 — Issue #3: 3 - wsgi_intercept's urlopen fails under Python 2.5 due to wsgi_intercept/urllib2_intercept/wsgi_urllib2.py 
Author: pumazi
Created: 2011-12-22 19:25:01+00:00
State: open

Under Python 2.5, calls to urlopen using wsgi_intercept fail with the
following stacktrace:

Traceback (most recent call last):
File "/Users/me/Projects/project/httpactions.py", line 42, in __call__
response = urlopen(request)
File
"/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py",
line 121, in urlopen
return _opener.open(url, data)
File
"/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py",
line 374, in open
response = self._open(req, data)
File
"/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py",
line 392, in _open
'_open', req)
File
"/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py",
line 353, in _call_chain
result = func(*args)
File
"/Users/me/Projects/project/eggs/wsgi_intercept-0.3.dev-py2.5.egg/wsgi_intercept/urllib2/wsgi_urllib2.py",
line 31, in http_open
return self.do_open(WSGI_HTTP, req)
File
"/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py",
line 1072, in do_open
h.request(req.get_method(), req.get_selector(), req.data, headers)
AttributeError: WSGI_HTTP instance has no attribute 'request'

This appears to be due to a change introduced in Python 2.4 and workaround
code in wsgi_intercept/urllib2_intercept/wsgi_urllib2.py that has an if
2.4/else clause, rather than an if 2.3/else clause. The following diff
resolves this issue:

--- wsgi_intercept/urllib2_intercept/wsgi_urllib2.py (revision 26)
+++ wsgi_intercept/urllib2_intercept/wsgi_urllib2.py (working copy)
@@ -9,26 +9,26 @@
# ugh, version dependence.
#

-if sys.version_info[:2] == (2, 4,):
+if sys.version_info[:2] == (2, 3):
+ class WSGI_HTTP(HTTP):
+ _connection_class = WSGI_HTTPConnection
+
class WSGI_HTTPHandler(HTTPHandler):
"""
Override the default HTTPHandler class with one that uses the
WSGI_HTTPConnection class to open HTTP URLs.
"""
def http_open(self, req):
- return self.do_open(WSGI_HTTPConnection, req)
+ return self.do_open(WSGI_HTTP, req)

else:
- class WSGI_HTTP(HTTP):
- _connection_class = WSGI_HTTPConnection
-
class WSGI_HTTPHandler(HTTPHandler):
"""
Override the default HTTPHandler class with one that uses the
WSGI_HTTPConnection class to open HTTP URLs.
"""
def http_open(self, req):
- return self.do_open(WSGI_HTTP, req)
+ return self.do_open(WSGI_HTTPConnection, req)

def install_opener():
handler = WSGI_HTTPHandler()

Original link: http://code.google.com/p/wsgi-intercept/issues/detail?id=3