From: Noel Power Date: Fri, 15 Jun 2018 12:00:15 +0000 (+0100) Subject: python/samba: Add cmp_fn and cmp_to_key_fn functions for py2/py3 X-Git-Tag: ldb-1.5.0~289 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0e930dfb477b121b69351cc9a011dfb71bcd10c0;p=thirdparty%2Fsamba.git python/samba: Add cmp_fn and cmp_to_key_fn functions for py2/py3 the cmp function and the cmp paramater (e.g. to sort functions) no longer exist in python3. cmp_fn is provides the missing functionality of the py2 cmp builtin function. cmp_to_key_fn allows the key paramater (e.g. for sort) to use the old py2 cmp function for sorting. Note: the cmp_to_key is present in since 2.7 (hence the inclusion of the source code for this function pre that version) Signed-off-by: Noel Power Reviewed-by: Douglas Bagnall Reviewed-by: Andreas Schneider Signed-off-by: Joe Guo --- diff --git a/python/samba/compat.py b/python/samba/compat.py index 042fc86a440..3fdeec886a6 100644 --- a/python/samba/compat.py +++ b/python/samba/compat.py @@ -22,9 +22,20 @@ import sys PY3 = sys.version_info[0] == 3 if PY3: + def cmp_fn(x, y): + """ + Replacement for built-in function cmp that was removed in Python 3 + + Compare the two objects x and y and return an integer according to + the outcome. The return value is negative if x < y, zero if x == y + and strictly positive if x > y. + """ + + return (x > y) - (x < y) # compat functions from urllib.parse import quote as urllib_quote from urllib.request import urlopen as urllib_urlopen + from functools import cmp_to_key as cmp_to_key_fn # compat types integer_types = int, @@ -36,6 +47,40 @@ if PY3: import io StringIO = io.StringIO else: + + if sys.version_info < (2, 7): + def cmp_to_key_fn(mycmp): + + """Convert a cmp= function into a key= function""" + class K(object): + __slots__ = ['obj'] + + def __init__(self, obj, *args): + self.obj = obj + + def __lt__(self, other): + return mycmp(self.obj, other.obj) < 0 + + def __gt__(self, other): + return mycmp(self.obj, other.obj) > 0 + + def __eq__(self, other): + return mycmp(self.obj, other.obj) == 0 + + def __le__(self, other): + return mycmp(self.obj, other.obj) <= 0 + + def __ge__(self, other): + return mycmp(self.obj, other.obj) >= 0 + + def __ne__(self, other): + return mycmp(self.obj, other.obj) != 0 + + def __hash__(self): + raise TypeError('hash not implemented') + return K + else: + from functools import cmp_to_key as cmp_to_key_fn # compat functions from urllib import quote as urllib_quote from urllib import urlopen as urllib_urlopen @@ -49,3 +94,4 @@ else: # alias import StringIO StringIO = StringIO.StringIO + cmp_fn = cmp