Commits

Anonymous committed 694c0a3

fixed #16 and #18: BadStatusLine and ValueError in package_index.urlopen

Comments (0)

Files changed (3)

 0.6.1
 -----
 
+setuptools
+==========
+
+* package_index.urlopen now catches BadStatusLine and malformed url errors.
+  This closes http://bitbucket.org/tarek/distribute/issue/16 and
+  http://bitbucket.org/tarek/distribute/issue/18.
+
 bootstraping
 ============
 

setuptools/package_index.py

 """PyPI and direct package downloading"""
 import sys, os.path, re, urlparse, urllib2, shutil, random, socket, cStringIO
+import httplib
 from pkg_resources import *
 from distutils import log
 from distutils.errors import DistutilsError
             return local_open(url)
         try:
             return open_with_auth(url)
+        except ValueError, v:
+            msg = ' '.join([str(arg) for arg in v.args])
+            if warning:
+                self.warn(warning, msg)
+            else:
+                raise DistutilsError('%s %s' % (url, msg))
         except urllib2.HTTPError, v:
             return v
         except urllib2.URLError, v:
-            if warning: self.warn(warning, v.reason)
+            if warning:
+                self.warn(warning, v.reason)
             else:
                 raise DistutilsError("Download error for %s: %s"
                                      % (url, v.reason))
+        except httplib.BadStatusLine, v:
+            if warning:
+                self.warn(warning, v.line)
+            else:
+                raise DistutilsError('%s returned a bad status line. '
+                                     'The server might be down, %s' % \
+                                             (url, v.line))
 
     def _download_url(self, scheme, url, tmpdir):
         # Determine download filename

setuptools/tests/test_packageindex.py

         else:
             self.assert_(isinstance(v,urllib2.HTTPError))
 
+        # issue 16
+        # easy_install inquant.contentmirror.plone breaks because of a typo
+        # in its home URL
+        index = setuptools.package_index.PackageIndex(
+            hosts=('www.example.com',)
+        )
+
+        url = 'url:%20https://svn.plone.org/svn/collective/inquant.contentmirror.plone/trunk'
+        try:
+            v = index.open_url(url)
+        except Exception, v:
+            self.assert_(url in str(v))
+        else:
+            self.assert_(isinstance(v, urllib2.HTTPError))
+
+        def _urlopen(*args):
+            import httplib
+            raise httplib.BadStatusLine('line')
+
+        old_urlopen = urllib2.urlopen
+        urllib2.urlopen = _urlopen
+        url = 'http://example.com'
+        try:
+            try:
+                v = index.open_url(url)
+            except Exception, v:
+                self.assert_('line' in str(v))
+            else:
+                raise AssertionError('Should have raise here!')
+        finally:
+            urllib2.urlopen = old_urlopen
+
+
     def test_url_ok(self):
         index = setuptools.package_index.PackageIndex(
             hosts=('www.example.com',)