title: Python crashes when adding a field to a shapefile with ogr
body: <p>When adding a field to a shapefile using the ogr package in python, the program crashes at the last step which is "SetFeature". This problem has been reported before, but I have not been able to resolve it based on other posts. </p>
  
  <p>Here is a sample code where the dummy value '100' is added as a new field for all features in the shapefile, followed by related questions and things I tried. </p>
  
  <p>Any idea what I could try ? Thank you for your time.</p>
  
  <p>SDB</p>
  
  <p>*EDIT : There was a conflict between this code and a module I wrote. It is hard to reproduce, so I'm settling for aworkaround : import the vexating module after the code below is used. Thank you all for your insight and time ! </p>
  
  <pre><code># Load shapefile
  shapefile = ogr.Open(shapefile_path, 1)
  
  layer = shapefile.GetLayer()
  layer_defn = layer.GetLayerDefn()
  
  
  new_field_defn = ogr.FieldDefn("New_field", ogr.OFTReal)
  new_field_defn.SetWidth(50)
  new_field_defn.SetPrecision(11)
  layer.CreateField(new_field_defn)
  
  
  # Walk through shapefile, setting new field for each feature
  for feature in layer :
      geometry = feature.GetGeometryRef()             
  
      band_value = 100
      feature.SetField("New_field",np.double(band_value))
  
      # This is the line that crashes the program
      layer.SetFeature(feature)
  
  shapefile.Destroy()
  </code></pre>
  
  <p>Using this solution did not work :
  <a href="https://gis.stackexchange.com/questions/109194/setfeature-creates-infinite-loop-when-updating-sqlite-feature-using-ogr">https://gis.stackexchange.com/questions/109194/setfeature-creates-infinite-loop-when-updating-sqlite-feature-using-ogr</a></p>
  
  <p>I believe my code follows this example : <a href="http://www.digital-geography.com/create-and-edit-shapefiles-with-python-only/#.V_0YtPnhCUk" rel="nofollow noreferrer">http://www.digital-geography.com/create-and-edit-shapefiles-with-python-only/#.V_0YtPnhCUk</a></p>
  
  <p>I think I've avoided the related Gotcha <a href="http://trac.osgeo.org/gdal/wiki/PythonGotchas#PythonbindingsdonotraiseexceptionsunlessyouexplicitlycallUseExceptions" rel="nofollow noreferrer">http://trac.osgeo.org/gdal/wiki/PythonGotchas#PythonbindingsdonotraiseexceptionsunlessyouexplicitlycallUseExceptions</a></p>