[Python-checkins] distutils2: PyPiDistribution raise an exception if hashname is invalid.

tarek.ziade python-checkins at python.org
Sun Jul 4 11:48:40 CEST 2010


tarek.ziade pushed 5a22adb09675 to distutils2:

http://hg.python.org/distutils2/rev/5a22adb09675
changeset:   329:5a22adb09675
user:        Alexis Metaireau <ametaireau at gmail.com>
date:        Thu Jul 01 12:59:32 2010 +0200
summary:     PyPiDistribution raise an exception if hashname is invalid.
files:       src/distutils2/pypi/dist.py, src/distutils2/pypi/errors.py, src/distutils2/tests/test_pypi_dist.py

diff --git a/src/distutils2/pypi/dist.py b/src/distutils2/pypi/dist.py
--- a/src/distutils2/pypi/dist.py
+++ b/src/distutils2/pypi/dist.py
@@ -11,8 +11,9 @@
     import hashlib
 except ImportError:
     from distutils2._backport import hashlib
+
 from distutils2.version import suggest_normalized_version
-from distutils2.pypi.errors import HashDoesNotMatch
+from distutils2.pypi.errors import HashDoesNotMatch, UnsupportedHashName
 
 EXTENSIONS = ".tar.gz .tar.bz2 .tar .zip .tgz .egg".split()
 MD5_HASH = re.compile(r'^.*#md5=([a-f0-9]+)$')
@@ -92,6 +93,12 @@
     
     def add_url(self, url, hashname=None, hashval=None, is_external=True):
         """Add a new url to the list of urls"""
+        if hashname is not None:
+            try:
+                hashlib.new(hashname)
+            except ValueError:
+                raise UnsupportedHashName(hashname)
+
         self._urls.append({
             'url': url,
             'hashname': hashname,
diff --git a/src/distutils2/pypi/errors.py b/src/distutils2/pypi/errors.py
--- a/src/distutils2/pypi/errors.py
+++ b/src/distutils2/pypi/errors.py
@@ -22,7 +22,11 @@
 
 
 class HashDoesNotMatch(DownloadError):
-    """Compared MD5 hashes does not match"""
+    """Compared hashes does not match"""
+
+
+class UnsupportedHashName(PyPIError):
+    """A unsupported hashname has been used"""
 
 
 class UnableToDownload(PyPIError):
diff --git a/src/distutils2/tests/test_pypi_dist.py b/src/distutils2/tests/test_pypi_dist.py
--- a/src/distutils2/tests/test_pypi_dist.py
+++ b/src/distutils2/tests/test_pypi_dist.py
@@ -7,7 +7,7 @@
 from distutils2.tests.pypi_server import use_pypi_server
 from distutils2.tests.support import unittest
 from distutils2.version import VersionPredicate
-from distutils2.pypi.errors import HashDoesNotMatch
+from distutils2.pypi.errors import HashDoesNotMatch, UnsupportedHashName
 from distutils2.pypi.dist import (PyPIDistribution as Dist,
                                   PyPIDistributions as Dists,
                                   split_archive_name)
@@ -140,6 +140,14 @@
         shutil.rmtree(path1)
         shutil.rmtree(os.path.dirname(path2))
 
+    def test_hashname(self):
+        """Invalid hashnames raises an exception on assignation"""
+        # should be ok
+        Dist("FooBar", "0.1", hashname="md5", hashval="value")
+
+        self.assertRaises(UnsupportedHashName, Dist, "FooBar", "0.1", 
+                          hashname="invalid_hashname", hashval="value")
+
 
 class TestPyPIDistributions(unittest.TestCase):
     """test the pypi.distr.PyPIDistributions class"""

--
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list