Skip to content

Commit e53e849

Browse files
authored
Merge pull request #274 from azmeuk/comparison
Allow inequalities with newstr and newbytes.
2 parents aaf2f33 + 9b51ec5 commit e53e849

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

src/future/types/newbytes.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -348,24 +348,24 @@ def __ne__(self, other):
348348
unorderable_err = 'unorderable types: bytes() and {0}'
349349

350350
def __lt__(self, other):
351-
if not isbytes(other):
352-
raise TypeError(self.unorderable_err.format(type(other)))
353-
return super(newbytes, self).__lt__(other)
351+
if isinstance(other, _builtin_bytes):
352+
return super(newbytes, self).__lt__(other)
353+
raise TypeError(self.unorderable_err.format(type(other)))
354354

355355
def __le__(self, other):
356-
if not isbytes(other):
357-
raise TypeError(self.unorderable_err.format(type(other)))
358-
return super(newbytes, self).__le__(other)
356+
if isinstance(other, _builtin_bytes):
357+
return super(newbytes, self).__le__(other)
358+
raise TypeError(self.unorderable_err.format(type(other)))
359359

360360
def __gt__(self, other):
361-
if not isbytes(other):
362-
raise TypeError(self.unorderable_err.format(type(other)))
363-
return super(newbytes, self).__gt__(other)
361+
if isinstance(other, _builtin_bytes):
362+
return super(newbytes, self).__gt__(other)
363+
raise TypeError(self.unorderable_err.format(type(other)))
364364

365365
def __ge__(self, other):
366-
if not isbytes(other):
367-
raise TypeError(self.unorderable_err.format(type(other)))
368-
return super(newbytes, self).__ge__(other)
366+
if isinstance(other, _builtin_bytes):
367+
return super(newbytes, self).__ge__(other)
368+
raise TypeError(self.unorderable_err.format(type(other)))
369369

370370
def __native__(self):
371371
# We can't just feed a newbytes object into str(), because

src/future/types/newstr.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,24 +302,28 @@ def __ne__(self, other):
302302
unorderable_err = 'unorderable types: str() and {0}'
303303

304304
def __lt__(self, other):
305-
if not istext(other):
306-
raise TypeError(self.unorderable_err.format(type(other)))
307-
return super(newstr, self).__lt__(other)
305+
if (isinstance(other, unicode) or
306+
isinstance(other, bytes) and not isnewbytes(other)):
307+
return super(newstr, self).__lt__(other)
308+
raise TypeError(self.unorderable_err.format(type(other)))
308309

309310
def __le__(self, other):
310-
if not istext(other):
311-
raise TypeError(self.unorderable_err.format(type(other)))
312-
return super(newstr, self).__le__(other)
311+
if (isinstance(other, unicode) or
312+
isinstance(other, bytes) and not isnewbytes(other)):
313+
return super(newstr, self).__le__(other)
314+
raise TypeError(self.unorderable_err.format(type(other)))
313315

314316
def __gt__(self, other):
315-
if not istext(other):
316-
raise TypeError(self.unorderable_err.format(type(other)))
317-
return super(newstr, self).__gt__(other)
317+
if (isinstance(other, unicode) or
318+
isinstance(other, bytes) and not isnewbytes(other)):
319+
return super(newstr, self).__gt__(other)
320+
raise TypeError(self.unorderable_err.format(type(other)))
318321

319322
def __ge__(self, other):
320-
if not istext(other):
321-
raise TypeError(self.unorderable_err.format(type(other)))
322-
return super(newstr, self).__ge__(other)
323+
if (isinstance(other, unicode) or
324+
isinstance(other, bytes) and not isnewbytes(other)):
325+
return super(newstr, self).__ge__(other)
326+
raise TypeError(self.unorderable_err.format(type(other)))
323327

324328
def __getattribute__(self, name):
325329
"""

tests/test_future/test_str.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,6 @@ def test_cmp(self):
382382
s > 3
383383
with self.assertRaises(TypeError):
384384
s < 1000
385-
with self.assertRaises(TypeError):
386-
s > b'XYZ'
387-
with self.assertRaises(TypeError):
388-
s < b'XYZ'
389385
with self.assertRaises(TypeError):
390386
s <= 3
391387
with self.assertRaises(TypeError):

0 commit comments

Comments
 (0)