[Python-checkins] cpython: packaging: don't use locale encoding to compute MD5 checksums

victor.stinner python-checkins at python.org
Thu May 19 15:10:27 CEST 2011


http://hg.python.org/cpython/rev/47c0ab9ab3aa
changeset:   70198:47c0ab9ab3aa
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Thu May 19 15:09:57 2011 +0200
summary:
  packaging: don't use locale encoding to compute MD5 checksums

Open the file in binary mode or use UTF-8 encoding.

files:
  Lib/packaging/command/install_distinfo.py            |  4 ++--
  Lib/packaging/create.py                              |  6 +++---
  Lib/packaging/tests/test_command_install_distinfo.py |  4 ++--
  3 files changed, 7 insertions(+), 7 deletions(-)


diff --git a/Lib/packaging/command/install_distinfo.py b/Lib/packaging/command/install_distinfo.py
--- a/Lib/packaging/command/install_distinfo.py
+++ b/Lib/packaging/command/install_distinfo.py
@@ -133,9 +133,9 @@
                             writer.writerow((fpath, '', ''))
                         else:
                             size = os.path.getsize(fpath)
-                            with open(fpath, 'r') as fp:
+                            with open(fpath, 'rb') as fp:
                                 hash = hashlib.md5()
-                                hash.update(fp.read().encode())
+                                hash.update(fp.read())
                             md5sum = hash.hexdigest()
                             writer.writerow((fpath, md5sum, size))
 
diff --git a/Lib/packaging/create.py b/Lib/packaging/create.py
--- a/Lib/packaging/create.py
+++ b/Lib/packaging/create.py
@@ -400,10 +400,10 @@
                                  self.data['description']).lower().encode())
                 ref = ref.digest()
                 for readme in glob.glob('README*'):
-                    with open(readme) as fp:
+                    with open(readme, encoding='utf-8') as fp:
                         contents = fp.read()
-                    val = md5(re.sub('\s', '',
-                                     contents.lower()).encode()).digest()
+                    contents = re.sub('\s', '', contents.lower()).encode()
+                    val = md5(contents).digest()
                     if val == ref:
                         del data['description']
                         data['description-file'] = readme
diff --git a/Lib/packaging/tests/test_command_install_distinfo.py b/Lib/packaging/tests/test_command_install_distinfo.py
--- a/Lib/packaging/tests/test_command_install_distinfo.py
+++ b/Lib/packaging/tests/test_command_install_distinfo.py
@@ -168,8 +168,8 @@
             else:
                 size = os.path.getsize(f)
                 md5 = hashlib.md5()
-                with open(f) as fp:
-                    md5.update(fp.read().encode())
+                with open(f, 'rb') as fp:
+                    md5.update(fp.read())
                 hash = md5.hexdigest()
                 expected.append([f, hash, str(size)])
 

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


More information about the Python-checkins mailing list