From d63c995b48aea983ebb6ce29d292b2d825d1ae38 Mon Sep 17 00:00:00 2001 From: Yuming Zhu Date: Jun 06 2019 11:34:03 +0000 Subject: [PATCH 1/2] make sure args of md5 and sha1 constructor are bytes on py3 --- diff --git a/koji/util.py b/koji/util.py index 4b2a3a7..6f9a9df 100644 --- a/koji/util.py +++ b/koji/util.py @@ -45,13 +45,32 @@ from six.moves import zip # imported from kojiweb and kojihub try: - from hashlib import md5 as md5_constructor + from hashlib import md5 as _md5_constructor except ImportError: # pragma: no cover - from md5 import new as md5_constructor + from md5 import new as _md5_constructor try: - from hashlib import sha1 as sha1_constructor + from hashlib import sha1 as _sha1_constructor except ImportError: # pragma: no cover - from sha import new as sha1_constructor + from sha import new as _sha1_constructor + + +def md5_constructor(*args): + """Construct MD5 hash object""" + if six.PY3: + args = _to_bytes_list(args) + return _md5_constructor(*args) + + +def sha1_constructor(*args): + """Construct SHA1 hash object""" + if six.PY3: + args = _to_bytes_list(args) + return _sha1_constructor(*args) + + +def _to_bytes_list(str_list): + """Translate string list to bytes list""" + return [bytes(s, 'utf-8') if isinstance(s, str) else s for s in str_list] def deprecated(message): From 4a63ea361c56fd98ee1c5876733d7205a86a5d2b Mon Sep 17 00:00:00 2001 From: Yu Ming Zhu Date: Jun 06 2019 12:07:19 +0000 Subject: [PATCH 2/2] also makesure arg is bytes in constr.update --- diff --git a/koji/util.py b/koji/util.py index 6f9a9df..23ca48c 100644 --- a/koji/util.py +++ b/koji/util.py @@ -54,18 +54,36 @@ except ImportError: # pragma: no cover from sha import new as _sha1_constructor -def md5_constructor(*args): - """Construct MD5 hash object""" - if six.PY3: - args = _to_bytes_list(args) - return _md5_constructor(*args) - - -def sha1_constructor(*args): - """Construct SHA1 hash object""" - if six.PY3: - args = _to_bytes_list(args) - return _sha1_constructor(*args) +class md5_constructor(object): + """wrapper of md5 constructor for python3 bytes/str support""" + def __init__(self, *args): + if six.PY3: + args = _to_bytes_list(args) + self.constr = _md5_constructor(*args) + + def __getattr__(self, attr): + return self.constr.__getattribute__(attr) + + def update(self, arg): + if six.PY3 and isinstance(arg, str): + arg = bytes(arg, 'utf-8') + return self.constr.update(arg) + + +class sha1_constructor(object): + """wrapper of sha1 constructor for python3 bytes/str support""" + def __init__(self, *args): + if six.PY3: + args = _to_bytes_list(args) + self.constr = _sha1_constructor(*args) + + def __getattr__(self, attr): + return self.constr.__getattribute__(attr) + + def update(self, arg): + if six.PY3 and isinstance(arg, str): + arg = bytes(arg, 'utf-8') + return self.constr.update(arg) def _to_bytes_list(str_list):