Skip to content

Commit

Permalink
autotest/pyscripts: check that python scripts don't emit the FutureWa…
Browse files Browse the repository at this point in the history
…rning about UseExceptions()
  • Loading branch information
rouault committed May 27, 2024
1 parent f079410 commit 9bdb598
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 21 deletions.
6 changes: 5 additions & 1 deletion autotest/pyscripts/test_gdal2tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,14 @@ def test_gdal2tiles_py_simple(script_path, tmp_path):
prev_wd = os.getcwd()
try:
os.chdir(tmp_path)
test_py_scripts.run_py_script(script_path, "gdal2tiles", f"-q {input_tif}")
_, err = test_py_scripts.run_py_script(
script_path, "gdal2tiles", f"-q {input_tif}", return_stderr=True
)
finally:
os.chdir(prev_wd)

assert "UseExceptions" not in err

_verify_raster_band_checksums(
f"{tmp_path}/out_gdal2tiles_smallworld/0/0/0.png",
expected_cs=[31420, 32522, 16314, 17849],
Expand Down
5 changes: 4 additions & 1 deletion autotest/pyscripts/test_gdal2xyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ def test_gdal2xyz_py_2(script_path, tmp_path):
arguments += " " + test_py_scripts.get_data_path("gcore") + "byte.tif "
arguments += out_xyz

test_py_scripts.run_py_script(script_path, "gdal2xyz", arguments)
_, err = test_py_scripts.run_py_script(
script_path, "gdal2xyz", arguments, return_stderr=True
)
assert "UseExceptions" not in err

assert os.path.exists(out_xyz)

Expand Down
9 changes: 7 additions & 2 deletions autotest/pyscripts/test_gdal_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,14 @@ def test_gdal_calc_py_1(script_path, tmp_path, stefan_full_rgba):
test_id, test_count = 1, 3
out = make_temp_filename_list(tmp_path, test_id, test_count)

test_py_scripts.run_py_script(
script_path, "gdal_calc", f"-A {infile} --calc=A --overwrite --outfile {out[0]}"
_, err = test_py_scripts.run_py_script(
script_path,
"gdal_calc",
f"-A {infile} --calc=A --overwrite --outfile {out[0]}",
return_stderr=True,
)
assert "UseExceptions" not in err

test_py_scripts.run_py_script(
script_path,
"gdal_calc",
Expand Down
4 changes: 3 additions & 1 deletion autotest/pyscripts/test_gdal_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_gdal_edit_py_1(script_path, tmp_path, read_only):
val_encoded = val

read_only_option = " -ro" if read_only else ""
test_py_scripts.run_py_script(
_, err = test_py_scripts.run_py_script(
script_path,
"gdal_edit",
filename
Expand All @@ -107,7 +107,9 @@ def test_gdal_edit_py_1(script_path, tmp_path, read_only):
+ val_encoded
+ "=UTF8"
+ read_only_option,
return_stderr=True,
)
assert "UseExceptions" not in err

ds = gdal.Open(filename)
wkt = ds.GetProjectionRef()
Expand Down
4 changes: 3 additions & 1 deletion autotest/pyscripts/test_gdal_fillnodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ def test_gdal_fillnodata_1(script_path, tmp_path):

result_tif = str(tmp_path / "test_gdal_fillnodata_1.tif")

test_py_scripts.run_py_script(
_, err = test_py_scripts.run_py_script(
script_path,
"gdal_fillnodata",
test_py_scripts.get_data_path("gcore") + f"byte.tif {result_tif}",
return_stderr=True,
)
assert "UseExceptions" not in err

ds = gdal.Open(result_tif)
assert ds.GetRasterBand(1).Checksum() == 4672
Expand Down
4 changes: 3 additions & 1 deletion autotest/pyscripts/test_gdal_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ def test_gdal_merge_1(script_path, tmp_path):

output_tif = str(tmp_path / "test_gdal_merge_1.tif")

test_py_scripts.run_py_script(
_, err = test_py_scripts.run_py_script(
script_path,
"gdal_merge",
f"-o {output_tif} " + test_py_scripts.get_data_path("gcore") + "byte.tif",
return_stderr=True,
)
assert "UseExceptions" not in err

ds = gdal.Open(output_tif)
assert ds.GetRasterBand(1).Checksum() == 4672
Expand Down
4 changes: 3 additions & 1 deletion autotest/pyscripts/test_gdal_pansharpen.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,16 @@ def test_gdal_pansharpen_1(script_path, tmp_path, small_world_pan_tif):

out_tif = str(tmp_path / "out.tif")

test_py_scripts.run_py_script(
_, err = test_py_scripts.run_py_script(
script_path,
"gdal_pansharpen",
f" {small_world_pan_tif} "
+ test_py_scripts.get_data_path("gdrivers")
+ "small_world.tif "
+ out_tif,
return_stderr=True,
)
assert "UseExceptions" not in err

with gdal.Open(out_tif) as ds:
cs = [ds.GetRasterBand(i + 1).Checksum() for i in range(ds.RasterCount)]
Expand Down
4 changes: 3 additions & 1 deletion autotest/pyscripts/test_gdal_polygonize.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ def test_gdal_polygonize_1(script_path, tmp_path):
shp_layer.CreateField(fd)

# run the algorithm.
test_py_scripts.run_py_script(
_, err = test_py_scripts.run_py_script(
script_path,
"gdal_polygonize",
test_py_scripts.get_data_path("alg") + f"polygonize_in.grd {tmp_path} poly DN",
return_stderr=True,
)
assert "UseExceptions" not in err

# Confirm we get the set of expected features in the output layer.

Expand Down
4 changes: 3 additions & 1 deletion autotest/pyscripts/test_gdal_proximity.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ def test_gdal_proximity_1(script_path, tmp_path):
dst_ds = drv.Create(output_tif, 25, 25, 1, gdal.GDT_Byte)
dst_ds = None

test_py_scripts.run_py_script(
_, err = test_py_scripts.run_py_script(
script_path,
"gdal_proximity",
test_py_scripts.get_data_path("alg") + f"pat.tif {output_tif}",
return_stderr=True,
)
assert "UseExceptions" not in err

dst_ds = gdal.Open(output_tif)
dst_band = dst_ds.GetRasterBand(1)
Expand Down
4 changes: 3 additions & 1 deletion autotest/pyscripts/test_gdal_retile.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,15 @@ def test_gdal_retile_2(script_path, tmp_path):
out_dir = tmp_path / "outretile2"
out_dir.mkdir()

test_py_scripts.run_py_script(
_, err = test_py_scripts.run_py_script(
script_path,
"gdal_retile",
f"-v -levels 2 -r bilinear -targetDir {out_dir} "
+ test_py_scripts.get_data_path("gcore")
+ "rgba.tif",
return_stderr=True,
)
assert "UseExceptions" not in err

ds = gdal.Open(f"{out_dir}/2/rgba_1_1.tif")
assert ds.GetRasterBand(1).Checksum() == 35, "wrong checksum for band 1"
Expand Down
4 changes: 3 additions & 1 deletion autotest/pyscripts/test_gdal_sieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ def test_gdal_sieve_1(script_path, tmp_path):
dst_ds = drv.Create(test_tif, 5, 7, 1, gdal.GDT_Byte)
dst_ds = None

test_py_scripts.run_py_script(
_, err = test_py_scripts.run_py_script(
script_path,
"gdal_sieve",
"-nomask -st 2 -4 "
+ test_py_scripts.get_data_path("alg")
+ f"sieve_src.grd {test_tif}",
return_stderr=True,
)
assert "UseExceptions" not in err

dst_ds = gdal.Open(test_tif)
dst_band = dst_ds.GetRasterBand(1)
Expand Down
4 changes: 3 additions & 1 deletion autotest/pyscripts/test_gdalattachpct.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ def test_gdalattachpct_basic(script_path, tmp_path, palette_file):

out_filename = str(tmp_path / "dst.tif")

test_py_scripts.run_py_script(
_, err = test_py_scripts.run_py_script(
script_path,
"gdalattachpct",
f" {palette_file} {src_filename} {out_filename}",
return_stderr=True,
)
assert "UseExceptions" not in err

ds = gdal.Open(out_filename)
assert ds.GetDriver().ShortName == "GTiff"
Expand Down
8 changes: 6 additions & 2 deletions autotest/pyscripts/test_gdalcompare.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ def test_gdalcompare_same(script_path, tmp_path):

source_filename = str(tmp_path / "src.tif")
shutil.copy("../gcore/data/byte.tif", source_filename)
ret = test_py_scripts.run_py_script(
script_path, "gdalcompare", f"{source_filename} {source_filename}"
ret, err = test_py_scripts.run_py_script(
script_path,
"gdalcompare",
f"{source_filename} {source_filename}",
return_stderr=True,
)
assert "UseExceptions" not in err
assert "Differences Found: 0" in ret


Expand Down
4 changes: 3 additions & 1 deletion autotest/pyscripts/test_gdalmove.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ def test_gdalmove_1(script_path, tmp_path):

shutil.copy(test_py_scripts.get_data_path("gcore") + "byte.tif", test_tif)

test_py_scripts.run_py_script(
_, err = test_py_scripts.run_py_script(
script_path,
"gdalmove",
f'-s_srs "+proj=utm +zone=11 +ellps=clrk66 +towgs84=0,0,0 +no_defs" -t_srs EPSG:32611 {test_tif} -et 1',
return_stderr=True,
)
assert "UseExceptions" not in err

ds = gdal.Open(test_tif)
got_gt = ds.GetGeoTransform()
Expand Down
4 changes: 3 additions & 1 deletion autotest/pyscripts/test_ogr_layer_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ def test_ogr_layer_algebra_intersection(script_path, tmp_path):
method_layer = None

# executing script
test_py_scripts.run_py_script(
_, err = test_py_scripts.run_py_script(
script_path,
"ogr_layer_algebra",
f"Intersection -input_ds {input_path} -output_ds {output_path} -method_ds {method_path}",
return_stderr=True,
)
assert "UseExceptions" not in err

driver = ogr.GetDriverByName("ESRI Shapefile")
dataSource = driver.Open(output_path, 0)
Expand Down
4 changes: 3 additions & 1 deletion autotest/pyscripts/test_ogrmerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,17 @@ def test_ogrmerge_1(script_path, tmp_path):

out_shp = str(tmp_path / "out.shp")

test_py_scripts.run_py_script(
_, err = test_py_scripts.run_py_script(
script_path,
"ogrmerge",
f"-single -o {out_shp} "
+ test_py_scripts.get_data_path("ogr")
+ "poly.shp "
+ test_py_scripts.get_data_path("ogr")
+ "poly.shp",
return_stderr=True,
)
assert "UseExceptions" not in err

ds = ogr.Open(out_shp)
lyr = ds.GetLayer(0)
Expand Down
9 changes: 6 additions & 3 deletions autotest/pyscripts/test_pct.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ def test_pct2rgb_1(script_path, tmp_path, rgb2pct1_tif):

output_tif = str(tmp_path / "test_pct2rgb_1.tif")

test_py_scripts.run_py_script(
script_path, "pct2rgb", f"{rgb2pct1_tif} {output_tif}"
_, err = test_py_scripts.run_py_script(
script_path, "pct2rgb", f"{rgb2pct1_tif} {output_tif}", return_stderr=True
)
assert "UseExceptions" not in err

ds = gdal.Open(output_tif)
assert ds.GetRasterBand(1).Checksum() == 20963
Expand Down Expand Up @@ -220,13 +221,15 @@ def test_rgb2pct_3(script_path, tmp_path, rgb2pct2_tif):

output_tif = str(tmp_path / "test_rgb2pct_3.tif")

test_py_scripts.run_py_script(
_, err = test_py_scripts.run_py_script(
script_path,
"rgb2pct",
f"-pct {rgb2pct2_tif} "
+ test_py_scripts.get_data_path("gcore")
+ f"rgbsmall.tif {output_tif}",
return_stderr=True,
)
assert "UseExceptions" not in err

ds = gdal.Open(output_tif)
assert ds.GetRasterBand(1).Checksum() == 16596
Expand Down

0 comments on commit 9bdb598

Please sign in to comment.