Martin von Löwis avatar Martin von Löwis committed 1249ce2

Work around missing getcode in 2.5.

Comments (0)

Files changed (1)


         return char
     bytes_from_ints = bytes
+if sys.version_info < (2,6):
+    # Add getcode into 2.5
+    class _addinfourl(urllib.addinfourl):
+        def __init__(self, fp, headers, url, code=None):
+            urllib.addinfourl.__init__(self, fp, headers, url)
+            self.code = code
+        def getcode(self):
+            return self.code
+    class FancyURLopener(urllib.FancyURLopener):
+        def http_error_default(self, url, fp, errcode, errmsg, headers):
+            return _addinfourl(fp, headers, "http:" + url, errcode)
+    FancyURLopener = urllib.FancyURLopener
 class NotAuthenticated(Exception):
         if data['openid.session_type'] == "no-encryption":
             data['openid.session_type'] = ''
         del data['openid.ns']
-    res = urllib.urlopen(url, b(urllib.urlencode(data)))
-    if res.getcode() != 200:
+    res = FancyURLopener().open(url, b(urllib.urlencode(data)))
+    if hasattr(res, 'getcode') and res.getcode() != 200:
         raise ValueError, "OpenID provider refuses connection with status %d" % res.getcode()
     data = parse_response(
     if 'error' in data:
     # Exact copies of all fields from the authentication response, except for
     # "openid.mode"
     request.extend((k, v) for k, (v,) in response.items() if 'openid.mode' != k)
-    res = urllib.urlopen(op_endpoint, urllib.urlencode(request))
-    if 200 != res.getcode():
+    res = FancyURLopener().open(op_endpoint, urllib.urlencode(request))
+    if hasattr(res, 'getcode') and 200 != res.getcode():
         raise NotAuthenticated(NotAuthenticated.CONNECTION_REFUSED, res.getcode())
     response = parse_response(
     if 'true' != response['is_valid']:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.