Range objects cannot be ordered

This commit is contained in:
Daniele Varrazzo 2012-09-23 22:54:50 +01:00
parent 25ef540aa3
commit 841ddaba87
2 changed files with 12 additions and 0 deletions

View File

@ -147,6 +147,11 @@ class Range(object):
def __hash__(self): def __hash__(self):
return hash((self._lower, self._upper, self._bounds)) return hash((self._lower, self._upper, self._bounds))
def __lt__(self, other):
raise TypeError('Range objects are not ordered')
__le__ = __gt__ = __ge__ = __lt__
def register_range(pgrange, pyrange, conn_or_curs, globally=False): def register_range(pgrange, pyrange, conn_or_curs, globally=False):
"""Register a typecaster and an adapter for range a range type. """Register a typecaster and an adapter for range a range type.

View File

@ -915,6 +915,13 @@ class RangeTestCase(unittest.TestCase):
assert_not_equal(Range(10, 20), Range(11, 20)) assert_not_equal(Range(10, 20), Range(11, 20))
assert_not_equal(Range(10, 20, '[)'), Range(10, 20, '[]')) assert_not_equal(Range(10, 20, '[)'), Range(10, 20, '[]'))
def test_not_ordered(self):
from psycopg2.extras import Range
self.assertRaises(TypeError, lambda: Range(empty=True) < Range(0,4))
self.assertRaises(TypeError, lambda: Range(1,2) > Range(0,4))
self.assertRaises(TypeError, lambda: Range(1,2) <= Range())
self.assertRaises(TypeError, lambda: Range(1,2) >= Range())
def skip_if_no_range(f): def skip_if_no_range(f):
def skip_if_no_range_(self): def skip_if_no_range_(self):