Skip to content

Commit

Permalink
VRT Python: fix PyObject leak and a crash in error code paths
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.osgeo.org/gdal/trunk@35408 f0d54148-0727-0410-94bb-9a71ac55c965
  • Loading branch information
rouault committed Sep 12, 2016
1 parent 8764e95 commit a371c15
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
48 changes: 46 additions & 2 deletions autotest/gdrivers/vrtderived.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,30 @@ def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize
print(gdal.GetLastErrorMsg())
return 'fail'

# Error at run time
# Error at run time (in global code)
ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
<VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
<ColorInterp>Gray</ColorInterp>
<PixelFunctionType>identity</PixelFunctionType>
<PixelFunctionLanguage>Python</PixelFunctionLanguage>
<PixelFunctionCode><![CDATA[
runtime_error
def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs):
pass
]]>
</PixelFunctionCode>
</VRTRasterBand>
</VRTDataset>
""")
with gdaltest.error_handler():
cs = ds.GetRasterBand(1).Checksum()
if cs != 0:
gdaltest.post_reason( 'invalid checksum' )
print(cs)
print(gdal.GetLastErrorMsg())
return 'fail'

# Error at run time (in pixel function)
ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
<VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
<ColorInterp>Gray</ColorInterp>
Expand Down Expand Up @@ -438,6 +461,27 @@ def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize
</PixelFunctionCode>
</VRTRasterBand>
</VRTDataset>
""")
with gdaltest.error_handler():
cs = ds.GetRasterBand(1).Checksum()
if cs != 0:
gdaltest.post_reason( 'invalid checksum' )
print(cs)
print(gdal.GetLastErrorMsg())
return 'fail'

# uncallable object
ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
<VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
<ColorInterp>Gray</ColorInterp>
<PixelFunctionType>uncallable_object</PixelFunctionType>
<PixelFunctionLanguage>Python</PixelFunctionLanguage>
<PixelFunctionCode><![CDATA[
uncallable_object = True
]]>
</PixelFunctionCode>
</VRTRasterBand>
</VRTDataset>
""")
with gdaltest.error_handler():
cs = ds.GetRasterBand(1).Checksum()
Expand Down Expand Up @@ -513,7 +557,7 @@ def my_func(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize,
ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
<VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
<ColorInterp>Gray</ColorInterp>
<PixelFunctionType>vrtderived.unknown_function</PixelFunctionType>
<PixelFunctionType>unknown_module.unknown_function</PixelFunctionType>
<PixelFunctionLanguage>Python</PixelFunctionLanguage>
</VRTRasterBand>
</VRTDataset>
Expand Down
6 changes: 4 additions & 2 deletions gdal/frmts/vrt/vrtderivedrasterband.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,7 @@ bool VRTDerivedRasterBand::InitializePython()
{
CPLError(CE_Failure, CPLE_AppDefined,
"%s", GetPyExceptionString().c_str());
Py_DecRef(poModule);
return false;
}
m_poPrivate->m_poUserFunction = PyObject_GetAttrString(poUserModule,
Expand All @@ -1018,8 +1019,8 @@ bool VRTDerivedRasterBand::InitializePython()
}
if( !PyCallable_Check(m_poPrivate->m_poUserFunction) )
{
CPLError(CE_Failure, CPLE_AppDefined,
"%s", GetPyExceptionString().c_str());
CPLError(CE_Failure, CPLE_AppDefined, "Object '%s' is not callable",
osPythonFunction.c_str());
Py_DecRef(poModule);
return false;
}
Expand All @@ -1029,6 +1030,7 @@ bool VRTDerivedRasterBand::InitializePython()
PyObject_GetAttrString(poModule, "GDALCreateNumpyArray" );
if (m_poPrivate->m_poGDALCreateNumpyArray == NULL || PyErr_Occurred())
{
// Shouldn't happen normally...
CPLError(CE_Failure, CPLE_AppDefined,
"%s", GetPyExceptionString().c_str());
Py_DecRef(poModule);
Expand Down

0 comments on commit a371c15

Please sign in to comment.