Skip to content

Commit 8ad3eea

Browse files
authored
Merge pull request #13444 from rouault/python_gdal_alg_remove_objects
Python bindings: avoid gdal.alg.X public symbols to have non relevant suggestions
2 parents 4780542 + b549034 commit 8ad3eea

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

autotest/gcore/algorithm.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ def test_gdal_alg_module(tmp_vsimem):
472472
assert set(["dataset", "mdim", "raster", "vector", "vsi"]).issubset(
473473
set(gdal.alg.__dict__.keys())
474474
)
475+
assert "gdal" not in gdal.alg.__dict__.keys()
476+
assert "os" not in gdal.alg.__dict__.keys()
477+
assert "Optional" not in gdal.alg.__dict__.keys()
475478

476479
gdal.FileFromMemBuffer(tmp_vsimem / "a", "a")
477480

swig/include/python/generate_gdal_alg_methods.i

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
def _generate_gdal_alg_methods():
66
"""Dynamically generates gdal.alg.{X}.{Y}.{func} methods from GDAL algorithms"""
77

8+
import copy
89
import os
910
import types
1011
import typing
@@ -147,14 +148,16 @@ def {name_sanitized}({args}):
147148
kwargs = {{k: v for k, v in kwargs.items() if v is not None}}
148149
return gdal.Run({new_path}, **kwargs)
149150
"""
150-
g = parent_module.__dict__
151+
func_globals = copy.copy(parent_module.__dict__)
151152
extra = {"gdal": gdal_module, "os": os, "List": List, "Union": Union, "Optional": Optional, "Callable": Callable}
152153
for k,v in extra.items():
153-
g[k] = v
154-
exec(func_code, g)
154+
func_globals[k] = v
155+
exec(func_code, func_globals)
156+
157+
parent_module.__dict__[name_sanitized] = func_globals[name_sanitized]
155158

156159
# Register doc string
157-
g[name_sanitized].__doc__ = f"""{alg.GetDescription()}
160+
parent_module.__dict__[name_sanitized].__doc__ = f"""{alg.GetDescription()}
158161
159162
Consult {alg.GetHelpFullURL()} for more details.
160163

0 commit comments

Comments
 (0)