Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9448 -- `Layer` objects now carry a reference to their parent …

…`DataSource`. Thanks, Matthew D. Hancher for the bug report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9284 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0b6d214f481e5412da0edb6c940db86e520b04c3 1 parent cbeea83
Justin Bronn authored October 26, 2008
2  django/contrib/gis/gdal/datasource.py
@@ -111,7 +111,7 @@ def __getitem__(self, index):
111 111
             l = get_layer(self._ptr, index)
112 112
         else:
113 113
             raise TypeError('Invalid index type: %s' % type(index))
114  
-        return Layer(l)
  114
+        return Layer(l, self)
115 115
         
116 116
     def __len__(self):
117 117
         "Returns the number of layers within the data source."
10  django/contrib/gis/gdal/layer.py
@@ -25,12 +25,18 @@ class Layer(object):
25 25
     "A class that wraps an OGR Layer, needs to be instantiated from a DataSource object."
26 26
 
27 27
     #### Python 'magic' routines ####
28  
-    def __init__(self, layer_ptr):
29  
-        "Needs a C pointer (Python/ctypes integer) in order to initialize."
  28
+    def __init__(self, layer_ptr, ds):
  29
+        """
  30
+        Initializes on an OGR C pointer to the Layer and the `DataSource` object
  31
+        that owns this layer.  The `DataSource` object is required so that a 
  32
+        reference to it is kept with this Layer.  This prevents garbage 
  33
+        collection of the `DataSource` while this Layer is still active.
  34
+        """
30 35
         self._ptr = None # Initially NULL
31 36
         if not layer_ptr:
32 37
             raise OGRException('Cannot create Layer, invalid pointer given')
33 38
         self._ptr = layer_ptr
  39
+        self._ds = ds
34 40
         self._ldefn = get_layer_defn(self._ptr)
35 41
         # Does the Layer support random reading?
36 42
         self._random_read = self.test_capability('RandomRead')
17  django/contrib/gis/tests/test_gdal_ds.py
@@ -130,6 +130,23 @@ def test03b_layer_slice(self):
130 130
             control_vals = source.field_values[fld_name][sl]
131 131
             self.assertEqual(control_vals, test_vals)
132 132
 
  133
+    def test03c_layer_references(self):
  134
+        "Test to make sure Layer access is still available without the DataSource."
  135
+        source = ds_list[0]
  136
+
  137
+        # See ticket #9448.
  138
+        def get_layer():
  139
+            # This DataSource object is not accessible outside this
  140
+            # scope.  However, a reference should still be kept alive
  141
+            # on the `Layer` returned.
  142
+            ds = DataSource(source.ds)
  143
+            return ds[0]
  144
+
  145
+        # Making sure we can call OGR routines on the Layer returned.
  146
+        lyr = get_layer()
  147
+        self.assertEqual(source.nfeat, len(lyr))
  148
+        self.assertEqual(source.gtype, lyr.geom_type.num)        
  149
+
133 150
     def test04_features(self):
134 151
         "Testing Data Source Features."
135 152
         for source in ds_list:

0 notes on commit 0b6d214

Please sign in to comment.
Something went wrong with that request. Please try again.