Skip to content

Commit

Permalink
autotest/alg: make it work with exceptions on by default
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Mar 18, 2023
1 parent a061ad0 commit 1ef2258
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 71 deletions.
11 changes: 5 additions & 6 deletions autotest/alg/applyverticalshiftgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def test_applyverticalshiftgrid_1():
# Error cases


@gdaltest.disable_exceptions()
def test_applyverticalshiftgrid_2():

sr = osr.SpatialReference()
Expand Down Expand Up @@ -246,9 +247,8 @@ def test_applyverticalshiftgrid_4():
out_ds = gdal.ApplyVerticalShiftGrid(
src_ds, grid_ds, options=["ERROR_ON_MISSING_VERT_SHIFT=YES"]
)
with gdaltest.error_handler():
data = out_ds.GetRasterBand(1).ReadRaster()
assert data is None
with pytest.raises(Exception):
out_ds.GetRasterBand(1).ReadRaster()

# ERROR_ON_MISSING_VERT_SHIFT due to nodata in grid
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
Expand All @@ -262,9 +262,8 @@ def test_applyverticalshiftgrid_4():
out_ds = gdal.ApplyVerticalShiftGrid(
src_ds, grid_ds, options=["ERROR_ON_MISSING_VERT_SHIFT=YES"]
)
with gdaltest.error_handler():
data = out_ds.GetRasterBand(1).ReadRaster()
assert data is None
with pytest.raises(Exception):
out_ds.GetRasterBand(1).ReadRaster()


###############################################################################
Expand Down
43 changes: 17 additions & 26 deletions autotest/alg/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,30 +393,24 @@ def test_contour_too_many_levels():

with gdaltest.tempfile("/vsimem/test.asc", content):
ds = gdal.Open("/vsimem/test.asc")
with gdaltest.error_handler():
assert (
gdal.ContourGenerateEx(
ds.GetRasterBand(1),
ogr_lyr,
options=["LEVEL_INTERVAL=1", "ID_FIELD=0"],
)
!= 0
with pytest.raises(Exception):
gdal.ContourGenerateEx(
ds.GetRasterBand(1),
ogr_lyr,
options=["LEVEL_INTERVAL=1", "ID_FIELD=0"],
)

with gdaltest.tempfile("/vsimem/test.asc", content):
ds = gdal.Open("/vsimem/test.asc")
with gdaltest.error_handler():
assert (
gdal.ContourGenerateEx(
ds.GetRasterBand(1),
ogr_lyr,
options=[
"LEVEL_INTERVAL=1",
"LEVEL_EXP_BASE=1.0001",
"ID_FIELD=0",
],
)
!= 0
with pytest.raises(Exception):
gdal.ContourGenerateEx(
ds.GetRasterBand(1),
ogr_lyr,
options=[
"LEVEL_INTERVAL=1",
"LEVEL_EXP_BASE=1.0001",
"ID_FIELD=0",
],
)

ogr_ds = None
Expand All @@ -434,12 +428,9 @@ def test_contour_raster_acquisition_error():
ogr_lyr.CreateField(field_defn)
ds = gdal.Open("../gcore/data/byte_truncated.tif")

with gdaltest.error_handler():
assert (
gdal.ContourGenerateEx(
ds.GetRasterBand(1), ogr_lyr, options=["LEVEL_INTERVAL=1", "ID_FIELD=0"]
)
!= 0
with pytest.raises(Exception):
gdal.ContourGenerateEx(
ds.GetRasterBand(1), ogr_lyr, options=["LEVEL_INTERVAL=1", "ID_FIELD=0"]
)


Expand Down
66 changes: 27 additions & 39 deletions autotest/alg/warp.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,40 +951,31 @@ def test_warp_30():
cbk = warp_30_progress_callback
cbk_user_data = None # value for last parameter of above warp_27_progress_callback

gdal.PushErrorHandler("CPLQuietErrorHandler")
ret = gdal.ReprojectImage(
src_ds,
dst_ds,
None, # src_wkt : left to default value --> will use the one from source \
None, # dst_wkt : left to default value --> will use the one from destination \
resampling,
0, # WarpMemoryLimit : left to default value \
error_threshold,
cbk, # Progress callback : could be left to None or unspecified for silent progress
cbk_user_data,
) # Progress callback user data
gdal.PopErrorHandler()

assert ret != 0

old_val = gdal.GetConfigOption("GDAL_NUM_THREADS")
gdal.SetConfigOption("GDAL_NUM_THREADS", "2")
gdal.PushErrorHandler("CPLQuietErrorHandler")
ret = gdal.ReprojectImage(
src_ds,
dst_ds,
None, # src_wkt : left to default value --> will use the one from source \
None, # dst_wkt : left to default value --> will use the one from destination \
resampling,
0, # WarpMemoryLimit : left to default value \
error_threshold,
cbk, # Progress callback : could be left to None or unspecified for silent progress
cbk_user_data,
) # Progress callback user data
gdal.PopErrorHandler()
gdal.SetConfigOption("GDAL_NUM_THREADS", old_val)

assert ret != 0
with pytest.raises(Exception):
gdal.ReprojectImage(
src_ds,
dst_ds,
None, # src_wkt : left to default value --> will use the one from source \
None, # dst_wkt : left to default value --> will use the one from destination \
resampling,
0, # WarpMemoryLimit : left to default value \
error_threshold,
cbk, # Progress callback : could be left to None or unspecified for silent progress
cbk_user_data,
) # Progress callback user data

with gdal.config_option("GDAL_NUM_THREADS", "2"), pytest.raises(Exception):
gdal.ReprojectImage(
src_ds,
dst_ds,
None, # src_wkt : left to default value --> will use the one from source \
None, # dst_wkt : left to default value --> will use the one from destination \
resampling,
0, # WarpMemoryLimit : left to default value \
error_threshold,
cbk, # Progress callback : could be left to None or unspecified for silent progress
cbk_user_data,
) # Progress callback user data

gdal.Unlink("/vsimem/warp_30.tif")

Expand Down Expand Up @@ -1097,11 +1088,8 @@ def test_warp_37():
)
dst_wkt = sr.ExportToWkt()

gdal.PushErrorHandler("CPLQuietErrorHandler")
tmp_ds = gdal.AutoCreateWarpedVRT(src_ds, None, dst_wkt)
gdal.PopErrorHandler()
gdal.ErrorReset()
assert tmp_ds is None
with pytest.raises(Exception):
gdal.AutoCreateWarpedVRT(src_ds, None, dst_wkt)


###############################################################################
Expand Down

0 comments on commit 1ef2258

Please sign in to comment.