Fixed comparison of Range with non-range objects

Fixes ticket #164.

Patch from Chris Withers on master.
This commit is contained in:
Daniele Varrazzo 2013-06-18 14:16:06 +01:00
parent 8fd228dd28
commit eb36e75b89
3 changed files with 17 additions and 0 deletions

2
NEWS
View File

@ -3,6 +3,8 @@ What's new in psycopg 2.5.1
- Fixed build on Solaris 10 and 11 where the round() function is already
declared (:ticket:`#146`).
- Fixed comparison of `Range` with non-range objects (:ticket:`#164`).
Thanks to Chris Withers for the patch.
What's new in psycopg 2.5

View File

@ -121,6 +121,8 @@ class Range(object):
return self._bounds is not None
def __eq__(self, other):
if not isinstance(other, Range):
return False
return (self._lower == other._lower
and self._upper == other._upper
and self._bounds == other._bounds)

View File

@ -1212,6 +1212,19 @@ class RangeTestCase(unittest.TestCase):
assert_not_equal(Range(10, 20), Range(11, 20))
assert_not_equal(Range(10, 20, '[)'), Range(10, 20, '[]'))
def test_eq_wrong_type(self):
from psycopg2.extras import Range
self.assertNotEqual(Range(10, 20), ())
def test_eq_subclass(self):
from psycopg2.extras import Range, NumericRange
class IntRange(NumericRange): pass
class PositiveIntRange(IntRange): pass
self.assertEqual(Range(10, 20), IntRange(10, 20))
self.assertEqual(PositiveIntRange(10, 20), IntRange(10, 20))
def test_not_ordered(self):
from psycopg2.extras import Range
self.assertRaises(TypeError, lambda: Range(empty=True) < Range(0,4))