diff --git a/lib/_range.py b/lib/_range.py index 5a794103..47a3e0e4 100644 --- a/lib/_range.py +++ b/lib/_range.py @@ -139,15 +139,35 @@ class Range(object): def __lt__(self, other): if not isinstance(other, Range): - return False - return ((self._lower, self._upper, self._bounds) < - (other._lower, other._upper, other._bounds)) + return NotImplemented + for attr in self.__slots__: + self_value = getattr(self, attr) + other_value = getattr(other, attr) + if self_value == other_value: + pass + elif self_value is None: + return True + elif other_value is None: + return False + else: + return self_value < other_value + return False def __le__(self, other): if not isinstance(other, Range): - return False - return ((self._lower, self._upper, self._bounds) <= - (other._lower, other._upper, other._bounds)) + return NotImplemented + for attr in self.__slots__: + self_value = getattr(self, attr) + other_value = getattr(other, attr) + if self_value == other_value: + pass + elif self_value is None: + return True + elif other_value is None: + return False + else: + return self_value <= other_value + return True def register_range(pgrange, pyrange, conn_or_curs, globally=False): diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index a7d9da28..de3bebe9 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -13,6 +13,7 @@ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # License for more details. +from __future__ import with_statement import re import sys @@ -22,6 +23,7 @@ from functools import wraps from testutils import unittest, skip_if_no_uuid, skip_before_postgres from testutils import ConnectingTestCase, decorate_all_tests +from testutils import py3_raises_typeerror import psycopg2 import psycopg2.extras @@ -1240,8 +1242,10 @@ class RangeTestCase(unittest.TestCase): self.assert_(not Range() < Range()) self.assert_(not Range(empty=True) < Range(empty=True)) self.assert_(not Range(1, 2) < Range(1, 2)) - self.assert_(1 < Range(1, 2)) - self.assert_(not Range(1, 2) < 1) + with py3_raises_typeerror(): + self.assert_(1 < Range(1, 2)) + with py3_raises_typeerror(): + self.assert_(not Range(1, 2) < 1) def test_gt_ordering(self): from psycopg2.extras import Range @@ -1254,8 +1258,10 @@ class RangeTestCase(unittest.TestCase): self.assert_(not Range() > Range()) self.assert_(not Range(empty=True) > Range(empty=True)) self.assert_(not Range(1, 2) > Range(1, 2)) - self.assert_(not 1 > Range(1, 2)) - self.assert_(Range(1, 2) > 1) + with py3_raises_typeerror(): + self.assert_(not 1 > Range(1, 2)) + with py3_raises_typeerror(): + self.assert_(Range(1, 2) > 1) def test_le_ordering(self): from psycopg2.extras import Range @@ -1268,8 +1274,10 @@ class RangeTestCase(unittest.TestCase): self.assert_(Range() <= Range()) self.assert_(Range(empty=True) <= Range(empty=True)) self.assert_(Range(1, 2) <= Range(1, 2)) - self.assert_(1 <= Range(1, 2)) - self.assert_(not Range(1, 2) <= 1) + with py3_raises_typeerror(): + self.assert_(1 <= Range(1, 2)) + with py3_raises_typeerror(): + self.assert_(not Range(1, 2) <= 1) def test_ge_ordering(self): from psycopg2.extras import Range @@ -1282,8 +1290,10 @@ class RangeTestCase(unittest.TestCase): self.assert_(Range() >= Range()) self.assert_(Range(empty=True) >= Range(empty=True)) self.assert_(Range(1, 2) >= Range(1, 2)) - self.assert_(not 1 >= Range(1, 2)) - self.assert_(Range(1, 2) >= 1) + with py3_raises_typeerror(): + self.assert_(not 1 >= Range(1, 2)) + with py3_raises_typeerror(): + self.assert_(Range(1, 2) >= 1) def skip_if_no_range(f): diff --git a/tests/testutils.py b/tests/testutils.py index 708dd224..0569edef 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -329,3 +329,13 @@ def script_to_py3(script): f2.close() os.remove(filename) +class py3_raises_typeerror(object): + + def __enter__(self): + pass + + def __exit__(self, type, exc, tb): + if sys.version_info[0] >= 3: + assert type is TypeError + return True +