فهرست منبع

Simplified index bounds checking in GDAL objects.

Nick Pope 7 سال پیش
والد
کامیت
adc07e8f90
3فایلهای تغییر یافته به همراه13 افزوده شده و 12 حذف شده
  1. 3 2
      django/contrib/gis/gdal/datasource.py
  2. 3 3
      django/contrib/gis/gdal/feature.py
  3. 7 7
      django/contrib/gis/gdal/geometries.py

+ 3 - 2
django/contrib/gis/gdal/datasource.py

@@ -96,9 +96,10 @@ class DataSource(GDALBase):
             if not layer:
                 raise OGRIndexError('invalid OGR Layer name given: "%s"' % index)
         elif isinstance(index, int):
-            if index < 0 or index >= self.layer_count:
+            if 0 <= index < self.layer_count:
+                layer = capi.get_layer(self._ptr, index)
+            else:
                 raise OGRIndexError('index out of range')
-            layer = capi.get_layer(self._ptr, index)
         else:
             raise TypeError('Invalid index type: %s' % type(index))
         return Layer(layer, self)

+ 3 - 3
django/contrib/gis/gdal/feature.py

@@ -35,10 +35,10 @@ class Feature(GDALBase):
         """
         if isinstance(index, str):
             i = self.index(index)
-        else:
-            if index < 0 or index > self.num_fields:
-                raise OGRIndexError('index out of range')
+        elif 0 <= index < self.num_fields:
             i = index
+        else:
+            raise OGRIndexError('index out of range')
         return Field(self, i)
 
     def __iter__(self):

+ 7 - 7
django/contrib/gis/gdal/geometries.py

@@ -555,7 +555,7 @@ class LineString(OGRGeometry):
 
     def __getitem__(self, index):
         "Return the Point at the given index."
-        if index >= 0 and index < self.point_count:
+        if 0 <= index < self.point_count:
             x, y, z = c_double(), c_double(), c_double()
             capi.get_point(self.ptr, index, byref(x), byref(y), byref(z))
             dim = self.coord_dim
@@ -625,10 +625,10 @@ class Polygon(OGRGeometry):
 
     def __getitem__(self, index):
         "Get the ring at the specified index."
-        if index < 0 or index >= self.geom_count:
-            raise OGRIndexError('index out of range: %s' % index)
-        else:
+        if 0 <= index < self.geom_count:
             return OGRGeometry(capi.clone_geom(capi.get_geom_ref(self.ptr, index)), self.srs)
+        else:
+            raise OGRIndexError('index out of range: %s' % index)
 
     # Polygon Properties
     @property
@@ -664,10 +664,10 @@ class GeometryCollection(OGRGeometry):
 
     def __getitem__(self, index):
         "Get the Geometry at the specified index."
-        if index < 0 or index >= self.geom_count:
-            raise OGRIndexError('index out of range: %s' % index)
-        else:
+        if 0 <= index < self.geom_count:
             return OGRGeometry(capi.clone_geom(capi.get_geom_ref(self.ptr, index)), self.srs)
+        else:
+            raise OGRIndexError('index out of range: %s' % index)
 
     def __iter__(self):
         "Iterate over each Geometry."