From: Zac Medico Date: Sun, 2 Oct 2011 05:17:52 +0000 (-0700) Subject: Convert _generate_hash_function into a class. X-Git-Tag: v2.2.0_alpha61~28 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1ae379c59a5cdc2d729f2b33807e70548afcbfb4;p=portage.git Convert _generate_hash_function into a class. --- diff --git a/pym/portage/checksum.py b/pym/portage/checksum.py index 9e7e455d1..52e45d388 100644 --- a/pym/portage/checksum.py +++ b/pym/portage/checksum.py @@ -16,8 +16,16 @@ import tempfile hashfunc_map = {} hashorigin_map = {} -def _generate_hash_function(hashtype, hashobject, origin="unknown"): - def pyhash(filename): +class _generate_hash_function(object): + + __slots__ = ("_hashobject",) + + def __init__(self, hashtype, hashobject, origin="unknown"): + self._hashobject = hashobject + hashfunc_map[hashtype] = self + hashorigin_map[hashtype] = origin + + def __call__(self, filename): """ Run a checksum against a file. @@ -41,7 +49,7 @@ def _generate_hash_function(hashtype, hashobject, origin="unknown"): blocksize = HASHING_BLOCKSIZE data = f.read(blocksize) size = 0 - checksum = hashobject() + checksum = self._hashobject() while data: checksum.update(data) size = size + len(data) @@ -49,9 +57,6 @@ def _generate_hash_function(hashtype, hashobject, origin="unknown"): f.close() return (checksum.hexdigest(), size) - hashfunc_map[hashtype] = pyhash - hashorigin_map[hashtype] = origin - return pyhash # Define hash functions, try to use the best module available. Later definitions # override earlier ones