]> granicus.if.org Git - python/commitdiff
packaging: don't use locale encoding to compute MD5 checksums
authorVictor Stinner <victor.stinner@haypocalc.com>
Thu, 19 May 2011 13:09:57 +0000 (15:09 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Thu, 19 May 2011 13:09:57 +0000 (15:09 +0200)
Open the file in binary mode or use UTF-8 encoding.

Lib/packaging/command/install_distinfo.py
Lib/packaging/create.py
Lib/packaging/tests/test_command_install_distinfo.py

index 41fe73459f2033b173338f5fc5c1b57a96b1f0d9..a25a15a2bbcac1c79a3e702942f12226eae1f282 100644 (file)
@@ -133,9 +133,9 @@ class install_distinfo(Command):
                             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))
 
index 837d0b6b88135045f7c0196850b652e8ba8ea8b4..0676cf188f7d074dc021313776f9e445deecf292 100644 (file)
@@ -400,10 +400,10 @@ class MainProgram:
                                  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
index 3d33691de6926622c957b6214acac05a34fe7e2b..6d40f66d92ab4b7800765a62cc53ec9f6296db90 100644 (file)
@@ -168,8 +168,8 @@ class InstallDistinfoTestCase(support.TempdirManager,
             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)])