From 48459fd857352daa73a45503fe8c6c1095deae12 Mon Sep 17 00:00:00 2001 From: Joe Guo Date: Mon, 26 Mar 2018 17:07:33 +1300 Subject: [PATCH] selftest/common: fix dsdb_Dn comparison for Python 3 In Python 3, the builtin `cmp` funtion was dropped. And the `__cmp__` magic method in object is no longer honored, which is replaced by 6 new methods: __eq__, __ne__, __lt__, __le__, __gt__, __ge__. This caused `tests.CommonTests` failed with `py3_compatiable=True`. Fixed by adding the above methods. Signed-off-by: Joe Guo --- python/samba/common.py | 27 +++++++++++++++++++++++++++ selftest/tests.py | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/python/samba/common.py b/python/samba/common.py index 1c410a4702d..66003993730 100644 --- a/python/samba/common.py +++ b/python/samba/common.py @@ -23,6 +23,14 @@ from samba.dcerpc import misc import binascii +from samba.compat import PY3 + + +if PY3: + # cmp() exists only in Python 2 + def cmp(a, b): + return (a > b) - (a < b) + def confirm(msg, forced=False, allow_all=False): """confirm an action with the user @@ -110,6 +118,25 @@ def __cmp__(self, other): v = cmp(dn1.binary, dn2.binary) return v + # In Python3, __cmp__ is replaced by these 6 methods + def __eq__(self, other): + return self.__cmp__(other) == 0 + + def __ne__(self, other): + return self.__cmp__(other) != 0 + + def __lt__(self, other): + return self.__cmp__(other) < 0 + + def __le__(self, other): + return self.__cmp__(other) <= 0 + + def __gt__(self, other): + return self.__cmp__(other) > 0 + + def __ge__(self, other): + return self.__cmp__(other) >= 0 + def get_binary_integer(self): '''return binary part of a dsdb_Dn as an integer, or None''' if self.prefix == '': diff --git a/selftest/tests.py b/selftest/tests.py index ccd184f60e1..031d7083580 100644 --- a/selftest/tests.py +++ b/selftest/tests.py @@ -64,7 +64,7 @@ planpythontestsuite("none", "samba.tests.param", py3_compatible=True) planpythontestsuite("none", "samba.tests.upgrade") planpythontestsuite("none", "samba.tests.core", py3_compatible=True) -planpythontestsuite("none", "samba.tests.common") +planpythontestsuite("none", "samba.tests.common", py3_compatible=True) planpythontestsuite("none", "samba.tests.provision") planpythontestsuite("none", "samba.tests.password_quality") planpythontestsuite("none", "samba.tests.samba3")