From 9b51ec531198b8ee0b805c3526b02bb3bc57130d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89loi=20Rivard?= Date: Tue, 7 Mar 2017 09:38:32 +0100 Subject: [PATCH] Allow inequalities with newstr and newbytes. --- src/future/types/newbytes.py | 24 ++++++++++++------------ src/future/types/newstr.py | 28 ++++++++++++++++------------ tests/test_future/test_str.py | 4 ---- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/future/types/newbytes.py b/src/future/types/newbytes.py index 85e6501c..24ff5f90 100644 --- a/src/future/types/newbytes.py +++ b/src/future/types/newbytes.py @@ -348,24 +348,24 @@ def __ne__(self, other): unorderable_err = 'unorderable types: bytes() and {0}' def __lt__(self, other): - if not isbytes(other): - raise TypeError(self.unorderable_err.format(type(other))) - return super(newbytes, self).__lt__(other) + if isinstance(other, _builtin_bytes): + return super(newbytes, self).__lt__(other) + raise TypeError(self.unorderable_err.format(type(other))) def __le__(self, other): - if not isbytes(other): - raise TypeError(self.unorderable_err.format(type(other))) - return super(newbytes, self).__le__(other) + if isinstance(other, _builtin_bytes): + return super(newbytes, self).__le__(other) + raise TypeError(self.unorderable_err.format(type(other))) def __gt__(self, other): - if not isbytes(other): - raise TypeError(self.unorderable_err.format(type(other))) - return super(newbytes, self).__gt__(other) + if isinstance(other, _builtin_bytes): + return super(newbytes, self).__gt__(other) + raise TypeError(self.unorderable_err.format(type(other))) def __ge__(self, other): - if not isbytes(other): - raise TypeError(self.unorderable_err.format(type(other))) - return super(newbytes, self).__ge__(other) + if isinstance(other, _builtin_bytes): + return super(newbytes, self).__ge__(other) + raise TypeError(self.unorderable_err.format(type(other))) def __native__(self): # We can't just feed a newbytes object into str(), because diff --git a/src/future/types/newstr.py b/src/future/types/newstr.py index fd8615af..7bb1d915 100644 --- a/src/future/types/newstr.py +++ b/src/future/types/newstr.py @@ -302,24 +302,28 @@ def __ne__(self, other): unorderable_err = 'unorderable types: str() and {0}' def __lt__(self, other): - if not istext(other): - raise TypeError(self.unorderable_err.format(type(other))) - return super(newstr, self).__lt__(other) + if (isinstance(other, unicode) or + isinstance(other, bytes) and not isnewbytes(other)): + return super(newstr, self).__lt__(other) + raise TypeError(self.unorderable_err.format(type(other))) def __le__(self, other): - if not istext(other): - raise TypeError(self.unorderable_err.format(type(other))) - return super(newstr, self).__le__(other) + if (isinstance(other, unicode) or + isinstance(other, bytes) and not isnewbytes(other)): + return super(newstr, self).__le__(other) + raise TypeError(self.unorderable_err.format(type(other))) def __gt__(self, other): - if not istext(other): - raise TypeError(self.unorderable_err.format(type(other))) - return super(newstr, self).__gt__(other) + if (isinstance(other, unicode) or + isinstance(other, bytes) and not isnewbytes(other)): + return super(newstr, self).__gt__(other) + raise TypeError(self.unorderable_err.format(type(other))) def __ge__(self, other): - if not istext(other): - raise TypeError(self.unorderable_err.format(type(other))) - return super(newstr, self).__ge__(other) + if (isinstance(other, unicode) or + isinstance(other, bytes) and not isnewbytes(other)): + return super(newstr, self).__ge__(other) + raise TypeError(self.unorderable_err.format(type(other))) def __getattribute__(self, name): """ diff --git a/tests/test_future/test_str.py b/tests/test_future/test_str.py index 7e37a62f..d8da4d0e 100644 --- a/tests/test_future/test_str.py +++ b/tests/test_future/test_str.py @@ -382,10 +382,6 @@ def test_cmp(self): s > 3 with self.assertRaises(TypeError): s < 1000 - with self.assertRaises(TypeError): - s > b'XYZ' - with self.assertRaises(TypeError): - s < b'XYZ' with self.assertRaises(TypeError): s <= 3 with self.assertRaises(TypeError):