Gentoo Archives: gentoo-portage-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-portage-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-portage-dev] [PATCH 08/14] portage.checksum: Remove exception handling for missing hashlib
Date: Sun, 12 Mar 2017 19:02:47
Message-Id: 20170312190011.3234-9-mgorny@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCHES] portage.checksum hacking, pt. 4 by "Michał Górny"
1 Remove the try-except block for potential ImportError of hashlib.
2 The hashlib module should be available in all supported Python versions,
3 and we do not really test or support the case when it is not available.
4 ---
5 pym/portage/checksum.py | 55 ++++++++++++++++++++++---------------------------
6 1 file changed, 25 insertions(+), 30 deletions(-)
7
8 diff --git a/pym/portage/checksum.py b/pym/portage/checksum.py
9 index 7812791ad..68ed44fa9 100644
10 --- a/pym/portage/checksum.py
11 +++ b/pym/portage/checksum.py
12 @@ -9,6 +9,8 @@ from portage import os
13 from portage import _encodings
14 from portage import _unicode_decode, _unicode_encode
15 import errno
16 +import functools
17 +import hashlib
18 import stat
19 import sys
20 import subprocess
21 @@ -99,7 +101,7 @@ class _generate_hash_function(object):
22 # pycrypto. However, it might be the only accelerated implementation of
23 # WHIRLPOOL available.
24 try:
25 - import mhash, functools
26 + import mhash
27 for local_name, hash_name in (("rmd160", "ripemd160"), ("whirlpool", "whirlpool")):
28 if hasattr(mhash, 'MHASH_%s' % local_name.upper()):
29 globals()['%shash' % local_name] = \
30 @@ -124,7 +126,6 @@ except ImportError:
31 try:
32 # added in pycryptodome
33 from Crypto.Hash import BLAKE2b, BLAKE2s, SHA3_256, SHA3_512
34 - import functools
35
36 blake2bhash_ = getattr(BLAKE2b, 'new', None)
37 if blake2bhash_ is not None:
38 @@ -156,34 +157,28 @@ except ImportError:
39
40 # Use hashlib from python-2.5 if available and prefer it over pycrypto and internal fallbacks.
41 # Need special handling for RMD160/WHIRLPOOL as they may not always be provided by hashlib.
42 -try:
43 - import hashlib, functools
44 -
45 - md5hash = _generate_hash_function("MD5", hashlib.md5, origin="hashlib")
46 - sha1hash = _generate_hash_function("SHA1", hashlib.sha1, origin="hashlib")
47 - sha256hash = _generate_hash_function("SHA256", hashlib.sha256, origin="hashlib")
48 - sha512hash = _generate_hash_function("SHA512", hashlib.sha512, origin="hashlib")
49 - for local_name, hash_name in (
50 - ("rmd160", "ripemd160"),
51 - ("whirlpool", "whirlpool"),
52 - # available since Python 3.6
53 - ("BLAKE2B", "blake2b"),
54 - ("BLAKE2S", "blake2s"),
55 - ("SHA3_256", "sha3_256"),
56 - ("SHA3_512", "sha3_512"),
57 - ):
58 - try:
59 - hashlib.new(hash_name)
60 - except ValueError:
61 - pass
62 - else:
63 - globals()['%shash' % local_name] = \
64 - _generate_hash_function(local_name.upper(), \
65 - functools.partial(hashlib.new, hash_name), \
66 - origin='hashlib')
67 -
68 -except ImportError:
69 - pass
70 +md5hash = _generate_hash_function("MD5", hashlib.md5, origin="hashlib")
71 +sha1hash = _generate_hash_function("SHA1", hashlib.sha1, origin="hashlib")
72 +sha256hash = _generate_hash_function("SHA256", hashlib.sha256, origin="hashlib")
73 +sha512hash = _generate_hash_function("SHA512", hashlib.sha512, origin="hashlib")
74 +for local_name, hash_name in (
75 + ("rmd160", "ripemd160"),
76 + ("whirlpool", "whirlpool"),
77 + # available since Python 3.6
78 + ("BLAKE2B", "blake2b"),
79 + ("BLAKE2S", "blake2s"),
80 + ("SHA3_256", "sha3_256"),
81 + ("SHA3_512", "sha3_512"),
82 + ):
83 + try:
84 + hashlib.new(hash_name)
85 + except ValueError:
86 + pass
87 + else:
88 + globals()['%shash' % local_name] = \
89 + _generate_hash_function(local_name.upper(), \
90 + functools.partial(hashlib.new, hash_name), \
91 + origin='hashlib')
92
93 _whirlpool_unaccelerated = False
94 if "WHIRLPOOL" not in hashfunc_map:
95 --
96 2.12.0