selftest: enable py3 for samba.tests.common
authorJoe Guo <joeg@catalyst.net.nz>
Mon, 26 Mar 2018 04:07:33 +0000 (17:07 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 5 Apr 2018 06:59:10 +0000 (08:59 +0200)
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 <joeg@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
python/samba/common.py
selftest/tests.py

index 1c410a4702d48ec44af10766b2e6d1710826f8a6..66003993730220655ddb82cc8b8e3dbb7fe838aa 100644 (file)
@@ -23,6 +23,14 @@ from samba.ndr import ndr_pack
 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 @@ class dsdb_Dn(object):
         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 == '':
index a2ccee771eef3c0081c98cb01e4fdec69dc6cccd..24ed0c6819d35f034e86c1c7e84af9826e666034 100644 (file)
@@ -64,7 +64,7 @@ planpythontestsuite("none", "samba.tests.dcerpc.integer")
 planpythontestsuite("none", "samba.tests.param", py3_compatible=True)
 planpythontestsuite("none", "samba.tests.upgrade", py3_compatible=True)
 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", py3_compatible=True)
 planpythontestsuite("none", "samba.tests.password_quality", py3_compatible=True)
 planpythontestsuite("none", "samba.tests.strings")