Code

Ticket #19366: 19366-4.diff

File 19366-4.diff, 4.4 KB (added by claudep, 20 months ago)
Line 
1diff --git a/django/contrib/gis/geos/mutable_list.py b/django/contrib/gis/geos/mutable_list.py
2index 820cdfa..0418282 100644
3--- a/django/contrib/gis/geos/mutable_list.py
4+++ b/django/contrib/gis/geos/mutable_list.py
5@@ -149,27 +149,30 @@ class ListMixin(object):
6         return self
7 
8     def __eq__(self, other):
9-        for i in range(len(self)):
10+        olen = len(other)
11+        for i in range(olen):
12             try:
13                 c = self[i] == other[i]
14-            except IndexError:
15-                # must be other is shorter
16+            except self._IndexError:
17+                # self must be shorter
18                 return False
19             if not c:
20                 return False
21-        return True
22+        return len(self) == olen
23 
24     def __lt__(self, other):
25-        slen = len(self)
26-        for i in range(slen):
27+        olen = len(other)
28+        for i in range(olen):
29             try:
30                 c = self[i] < other[i]
31-            except IndexError:
32-                # must be other is shorter
33-                return False
34+            except self._IndexError:
35+                # self must be shorter
36+                return True
37             if c:
38                 return c
39-        return slen < len(other)
40+            elif other[i] < self[i]:
41+                return False
42+        return len(self) < olen
43 
44     ### Public list interface Methods ###
45     ## Non-mutating ##
46diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py
47index 283daa4..ec320f9 100644
48--- a/django/contrib/gis/geos/tests/test_geos.py
49+++ b/django/contrib/gis/geos/tests/test_geos.py
50@@ -451,6 +451,21 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
51             self.assertEqual(poly.wkt, Polygon(*tuple(r for r in poly)).wkt)
52             self.assertEqual(poly.wkt, Polygon(*tuple(LinearRing(r.tuple) for r in poly)).wkt)
53 
54+    def test_polygon_comparison(self):
55+        p1 = Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
56+        p2 = Polygon(((0, 0), (0, 1), (1, 0), (0, 0)))
57+        self.assertTrue(p1 > p2)
58+        self.assertFalse(p1 < p2)
59+        self.assertFalse(p2 > p1)
60+        self.assertTrue(p2 < p1)
61+
62+        p3 = Polygon(((0, 0), (0, 1), (1, 1), (2, 0), (0, 0)))
63+        p4 = Polygon(((0, 0), (0, 1), (2, 2), (1, 0), (0, 0)))
64+        self.assertFalse(p4 < p3)
65+        self.assertTrue(p3 < p4)
66+        self.assertTrue(p4 > p3)
67+        self.assertFalse(p3 > p4)
68+
69     def test_multipolygons(self):
70         "Testing MultiPolygon objects."
71         prev = fromstr('POINT (0 0)')
72diff --git a/django/contrib/gis/geos/tests/test_mutable_list.py b/django/contrib/gis/geos/tests/test_mutable_list.py
73index 675505f..988d841 100644
74--- a/django/contrib/gis/geos/tests/test_mutable_list.py
75+++ b/django/contrib/gis/geos/tests/test_mutable_list.py
76@@ -363,6 +363,7 @@ class ListMixinTest(unittest.TestCase):
77 
78         pl, ul = self.lists_of_len()
79         self.assertEqual(pl, ul, 'cmp for equal')
80+        self.assertFalse(ul == pl + [2], 'cmp for not equal')
81         self.assertTrue(pl >= ul, 'cmp for gte self')
82         self.assertTrue(pl <= ul, 'cmp for lte self')
83         self.assertTrue(ul >= pl, 'cmp for self gte')
84@@ -377,6 +378,14 @@ class ListMixinTest(unittest.TestCase):
85         self.assertTrue(ul < pl + [2], 'cmp')
86         self.assertTrue(ul <= pl + [2], 'cmp')
87 
88+        # Also works with a custom IndexError
89+        ul_longer = ul + [2]
90+        ul_longer._IndexError = TypeError
91+        ul._IndexError = TypeError
92+        self.assertFalse(ul_longer == pl)
93+        self.assertFalse(ul == ul_longer)
94+        self.assertTrue(ul_longer > ul)
95+
96         pl[1] = 20
97         self.assertTrue(pl > ul, 'cmp for gt self')
98         self.assertTrue(ul < pl, 'cmp for self lt')
99diff --git a/docs/ref/contrib/gis/geos.txt b/docs/ref/contrib/gis/geos.txt
100index eb20b1f..7d7c327 100644
101--- a/docs/ref/contrib/gis/geos.txt
102+++ b/docs/ref/contrib/gis/geos.txt
103@@ -656,6 +656,17 @@ is returned instead.
104 
105    Returns the number of interior rings in this geometry.
106 
107+.. admonition:: Comparing Polygons
108+
109+    Note that it is possible to compare ``Polygon`` objects directly with ``<``
110+    or ``>``, but as the comparison is made through Polygon's
111+    :class:`LineString`, it does not mean much (but is consistent and quick).
112+    You can always force the comparison with the :attr:`~GEOSGeometry.area`
113+    property::
114+
115+        >>> if poly_1.area > poly_2.area:
116+        >>>     pass
117+
118 Geometry Collections
119 ====================
120