return (x > y) - (x < y)
+def cmp_with_nones(x, y):
+ """This is like cmp(), but will cope if a value is None.
+
+ We sort Nones to the start.
+ """
+ # avoids
+ # TypeError: '>' not supported between instances of 'NoneType' and 'int'
+ if x == y:
+ return 0
+ if x is None:
+ return -1
+ if y is None:
+ return 1
+
+ return (x > y) - (x < y)
+
+
def confirm(msg, forced=False, allow_all=False):
"""confirm an action with the user
import samba
import samba.tests
-from samba.common import normalise_int32
+from samba.common import normalise_int32, cmp_with_nones
class CommonTests(samba.tests.TestCase):
self.assertRaises(ValueError, normalise_int32, 1 << 32)
self.assertRaises(ValueError, normalise_int32, 12345678901234567890)
self.assertRaises(ValueError, normalise_int32, -1000000000000)
+
+ def test_cmp_with_nones(self):
+ for pair, outcome in [
+ ((0, 0), 0),
+ ((2, 0), 1),
+ ((1, 2), -1),
+ ((None, 2), -1),
+ ((None, None), 0),
+ ((-6, None), 1),
+ (('a', None), 1),
+ ((object(), None), 1),
+ ((None, 1.2), -1),
+ ]:
+ self.assertEqual(cmp_with_nones(*pair), outcome)