Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python utilities: avoid UseExceptions() related deprecation warning when launched from launcher shell scripts #10039

Merged
merged 11 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions autotest/gcore/basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,22 @@ def test_basic_test_11():
with pytest.raises(Exception):
gdal.OpenEx("non existing")

try:
with gdal.ExceptionMgr(useExceptions=True):
try:
gdal.OpenEx("non existing")
except Exception:
pass
except Exception:
pytest.fails("Exception thrown whereas it should not have")

with gdal.ExceptionMgr(useExceptions=True):
try:
gdal.OpenEx("non existing")
except Exception:
pass
gdal.Open("data/byte.tif")


###############################################################################
# Test GDAL layer API
Expand Down
25 changes: 19 additions & 6 deletions autotest/pymod/test_py_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,33 @@ def run_py_script(
concatenated_argv: str,
run_as_script: bool = True,
run_as_module: bool = False,
return_stderr: bool = False,
):
result = None
if run_as_module:
try:
module = importlib.import_module("osgeo_utils." + script_name)
except ImportError:
module = importlib.import_module("osgeo_utils.samples." + script_name)
argv = [module.__file__] + shlex.split(concatenated_argv)
result = module.main(argv)
return module.main(argv)

if run_as_script:
result = run_py_script_as_external_script(
script_path, script_name, concatenated_argv
return run_py_script_as_external_script(
script_path, script_name, concatenated_argv, return_stderr=return_stderr
)
return result

raise Exception("either run_as_script or run_as_module should be specified")


###############################################################################
# Runs a Python script in a new process
#
def run_py_script_as_external_script(
script_path, script_name, concatenated_argv, display_live_on_parent_stdout=False
script_path,
script_name,
concatenated_argv,
display_live_on_parent_stdout=False,
return_stderr: bool = False,
):

script_file_path = os.path.join(script_path, script_name + ".py")
Expand All @@ -109,6 +115,13 @@ def run_py_script_as_external_script(
python_exe = python_exe.replace("\\", "/")
script_file_path = script_file_path.replace("\\", "/")

if return_stderr:
assert (
not display_live_on_parent_stdout
), "display_live_on_parent_stdout=True and return_stderr=True aren't supported at the same time"
return gdaltest.runexternal_out_and_err(
python_exe + ' "' + script_file_path + '" ' + concatenated_argv
)
return gdaltest.runexternal(
python_exe + ' "' + script_file_path + '" ' + concatenated_argv,
display_live_on_parent_stdout=display_live_on_parent_stdout,
Expand Down
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
9 changes: 8 additions & 1 deletion autotest/pyscripts/test_gdal2xyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import os

import gdaltest
import pytest
import test_py_scripts

Expand Down Expand Up @@ -64,6 +65,9 @@ def script_path():

def test_gdal2xyz_help(script_path):

if gdaltest.is_travis_branch("sanitize"):
pytest.skip("fails on sanitize for unknown reason")

assert "ERROR" not in test_py_scripts.run_py_script(
script_path, "gdal2xyz", "--help"
)
Expand Down Expand Up @@ -143,7 +147,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
Loading
Loading