Skip to content

Commit

Permalink
Fixed comparison of Range with non-range objects
Browse files Browse the repository at this point in the history
Fixes ticket psycopg#164.

Patch from Chris Withers on master.
  • Loading branch information
dvarrazzo committed Jun 18, 2013
1 parent 8fd228d commit eb36e75
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def __nonzero__(self):
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)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_types_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,19 @@ def assert_not_equal(r1, r2):
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))
Expand Down

0 comments on commit eb36e75

Please sign in to comment.