Ticket #9448: layer_reference_ds.diff

File layer_reference_ds.diff, 2.8 KB (added by jbronn, 7 years ago)

Patch that creates a reference to the DataSource inside each Layer

  • django/contrib/gis/tests/test_gdal_ds.py

     
    130130            control_vals = source.field_values[fld_name][sl]
    131131            self.assertEqual(control_vals, test_vals)
    132132
     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
    133150    def test04_features(self):
    134151        "Testing Data Source Features."
    135152        for source in ds_list:
  • django/contrib/gis/gdal/datasource.py

     
    111111            l = get_layer(self._ptr, index)
    112112        else:
    113113            raise TypeError('Invalid index type: %s' % type(index))
    114         return Layer(l)
     114        return Layer(l, self)
    115115       
    116116    def __len__(self):
    117117        "Returns the number of layers within the data source."
  • django/contrib/gis/gdal/layer.py

     
    2525    "A class that wraps an OGR Layer, needs to be instantiated from a DataSource object."
    2626
    2727    #### 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 and the `DataSource` object that
     31        owns this layer.  The `DataSource` object is required so that
     32        a reference to it is kept with this Layer.  This prevents the
     33        garbage collection of the `DataSource` while this `Layer` object
     34        is still active.
     35        """
    3036        self._ptr = None # Initially NULL
    3137        if not layer_ptr:
    3238            raise OGRException('Cannot create Layer, invalid pointer given')
    3339        self._ptr = layer_ptr
     40        self._ds = ds
    3441        self._ldefn = get_layer_defn(self._ptr)
    3542        # Does the Layer support random reading?
    3643        self._random_read = self.test_capability('RandomRead')
Back to Top