Skip to content

Commit

Permalink
Python bindings: remove recently added [Dont]UseExceptionsAllModules(…
Browse files Browse the repository at this point in the history
…) and make existing [Dont]UseExceptions() affect all modules
  • Loading branch information
rouault committed Mar 22, 2023
1 parent 1924c1a commit a1df6dc
Show file tree
Hide file tree
Showing 25 changed files with 133 additions and 95 deletions.
2 changes: 1 addition & 1 deletion autotest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# Explicitly enable exceptions since autotest/ now assumes them to be
# enabled
gdal.UseExceptionsAllModules()
gdal.UseExceptions()

# Put the pymod dir on the path, so modules can `import gdaltest`
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "pymod"))
Expand Down
27 changes: 23 additions & 4 deletions autotest/gcore/basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,27 +719,46 @@ def test_quiet_errors():
gdal.Error(gdal.CE_Failure, gdal.CPLE_AppDefined, "you will never see me")


def test_basic_test_UseExceptionsAllModules():
def test_basic_test_UseExceptions():

python_exe = sys.executable
cmd = '%s -c "from osgeo import gdal;' % python_exe + (
"gdal.UseExceptionsAllModules();" "gdal.Open('non_existing.tif');" ' " '
"gdal.UseExceptions();" "gdal.Open('non_existing.tif');" ' " '
)
try:
(_, err) = gdaltest.runexternal_out_and_err(cmd, encoding="UTF-8")
except Exception as e:
pytest.skip("got exception %s" % str(e))
assert "RuntimeError: " in err
assert "FutureWarning: Neither gdal.UseExceptions()" not in err
assert "FutureWarning: Neither ogr.UseExceptions()" not in err


def test_basic_test_DontUseExceptionsAllModules():
def test_basic_test_UseExceptions_ogr_open():

python_exe = sys.executable
cmd = '%s -c "from osgeo import gdal, ogr;' % python_exe + (
"gdal.UseExceptions();" "ogr.Open('non_existing.tif');" ' " '
)
try:
(_, err) = gdaltest.runexternal_out_and_err(cmd, encoding="UTF-8")
except Exception as e:
pytest.skip("got exception %s" % str(e))
assert "RuntimeError: " in err
assert "FutureWarning: Neither gdal.UseExceptions()" not in err
assert "FutureWarning: Neither ogr.UseExceptions()" not in err


def test_basic_test_DontUseExceptions():

python_exe = sys.executable
cmd = '%s -c "from osgeo import gdal;' % python_exe + (
"gdal.DontUseExceptionsAllModules();" "gdal.Open('non_existing.tif');" ' " '
"gdal.DontUseExceptions();" "gdal.Open('non_existing.tif');" ' " '
)
try:
(_, err) = gdaltest.runexternal_out_and_err(cmd, encoding="UTF-8")
except Exception as e:
pytest.skip("got exception %s" % str(e))
assert "ERROR " in err
assert "FutureWarning: Neither gdal.UseExceptions()" not in err
assert "FutureWarning: Neither ogr.UseExceptions()" not in err
41 changes: 4 additions & 37 deletions swig/include/python/gdal_python.i
Original file line number Diff line number Diff line change
Expand Up @@ -1379,17 +1379,15 @@ CPLErr ReadRaster1( double xoff, double yoff, double xsize, double ysize,
// be impacted when exceptions are enabled by default

%pythoncode %{
hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions = False

def _WarnIfUserHasNotSpecifiedIfUsingExceptions():
global hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions
if not hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions and not _UserHasSpecifiedIfUsingExceptions():
hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions = True
from . import gdal
if not hasattr(gdal, "hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions") and not _UserHasSpecifiedIfUsingExceptions():
gdal.hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions = True
import warnings
warnings.warn(
"Neither gdal.UseExceptions() nor gdal.DontUseExceptions() has been explicitly called. " +
"In GDAL 4.0, exceptions will be enabled by default. " +
"You may also call gdal.UseExceptionsAllModules() to enable exceptions in all GDAL related modules.", FutureWarning)
"In GDAL 4.0, exceptions will be enabled by default.", FutureWarning)

def _WarnIfUserHasNotSpecifiedIfUsingOgrExceptions():
from . import ogr
Expand Down Expand Up @@ -1450,37 +1448,6 @@ def _WarnIfUserHasNotSpecifiedIfUsingOgrExceptions():

%pythoncode %{

def UseExceptionsAllModules():
""" Enable exceptions in all GDAL related modules (osgeo.gdal, osgeo.ogr, osgeo.osr, osgeo.gnm).
Equivalent to calling UseExceptions() on all those modules"""

from . import gdal, ogr, osr
gdal.UseExceptions()
ogr.UseExceptions()
osr.UseExceptions()
try:
from . import gnm
gnm.UseExceptions()
except ImportError:
pass

def DontUseExceptionsAllModules():
""" Disable exceptions in all GDAL related modules (osgeo.gdal, osgeo.ogr, osgeo.osr, osgeo.gnm).
Equivalent to calling DontUseExceptions() on all those modules"""

from . import gdal, ogr, osr
gdal.DontUseExceptions()
ogr.DontUseExceptions()
osr.DontUseExceptions()
try:
from . import gnm
gnm.DontUseExceptions()
except ImportError:
pass


def InfoOptions(options=None, format='text', deserialize=True,
computeMinMax=False, reportHistograms=False, reportProj4=False,
stats=False, approxStats=False, computeChecksum=False,
Expand Down
9 changes: 4 additions & 5 deletions swig/include/python/gnm_python.i
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@
hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions = False

def _WarnIfUserHasNotSpecifiedIfUsingExceptions():
global hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions
if not hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions and not _UserHasSpecifiedIfUsingExceptions():
hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions = True
from . import gdal
if not hasattr(gdal, "hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions") and not _UserHasSpecifiedIfUsingExceptions():
gdal.hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions = True
import warnings
warnings.warn(
"Neither gnm.UseExceptions() nor gnm.DontUseExceptions() has been explicitly called. " +
"In GDAL 4.0, exceptions will be enabled by default. " +
"You may also call gdal.UseExceptionsAllModules() to enable exceptions in all GDAL related modules.", FutureWarning)
"In GDAL 4.0, exceptions will be enabled by default.", FutureWarning)
%}

%pythonprepend CastToNetwork %{
Expand Down
9 changes: 4 additions & 5 deletions swig/include/python/ogr_python.i
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@
hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions = False

def _WarnIfUserHasNotSpecifiedIfUsingExceptions():
global hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions
if not hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions and not _UserHasSpecifiedIfUsingExceptions():
hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions = True
from . import gdal
if not hasattr(gdal, "hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions") and not _UserHasSpecifiedIfUsingExceptions():
gdal.hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions = True
import warnings
warnings.warn(
"Neither ogr.UseExceptions() nor ogr.DontUseExceptions() has been explicitly called. " +
"In GDAL 4.0, exceptions will be enabled by default. " +
"You may also call gdal.UseExceptionsAllModules() to enable exceptions in all GDAL related modules.", FutureWarning)
"In GDAL 4.0, exceptions will be enabled by default.", FutureWarning)
%}

%pythonprepend Open %{
Expand Down
9 changes: 4 additions & 5 deletions swig/include/python/osr_python.i
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@
hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions = False

def _WarnIfUserHasNotSpecifiedIfUsingExceptions():
global hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions
if not hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions and not _UserHasSpecifiedIfUsingExceptions():
hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions = True
from . import gdal
if not hasattr(gdal, "hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions") and not _UserHasSpecifiedIfUsingExceptions():
gdal.hasWarnedAboutUserHasNotSpecifiedIfUsingExceptions = True
import warnings
warnings.warn(
"Neither osr.UseExceptions() nor osr.DontUseExceptions() has been explicitly called. " +
"In GDAL 4.0, exceptions will be enabled by default. " +
"You may also call gdal.UseExceptionsAllModules() to enable exceptions in all GDAL related modules.", FutureWarning)
"In GDAL 4.0, exceptions will be enabled by default.", FutureWarning)
%}

// End: to be removed in GDAL 4.0
Expand Down
59 changes: 57 additions & 2 deletions swig/include/python/python_exceptions.i
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static void _SetExceptionsLocal(int bVal)
}

static
void UseExceptions() {
void _UseExceptions() {
CPLErrorReset();
bUserHasSpecifiedIfUsingExceptions = TRUE;
if( !bUseExceptions )
Expand All @@ -99,7 +99,7 @@ void UseExceptions() {
}

static
void DontUseExceptions() {
void _DontUseExceptions() {
CPLErrorReset();
bUserHasSpecifiedIfUsingExceptions = TRUE;
if( bUseExceptions )
Expand Down Expand Up @@ -320,3 +320,58 @@ static void popErrorHandler()
"""
_SetExceptionsLocal(self.currentUseExceptions)
%}


%pythoncode %{

def UseExceptions():
""" Enable exceptions in all GDAL related modules (osgeo.gdal, osgeo.ogr, osgeo.osr, osgeo.gnm).
Note: prior to GDAL 3.7, this only affected the calling modue"""

try:
from . import gdal
gdal._UseExceptions()
except ImportError:
pass
try:
from . import ogr
ogr._UseExceptions()
except ImportError:
pass
try:
from . import osr
osr._UseExceptions()
except ImportError:
pass
try:
from . import gnm
gnm._UseExceptions()
except ImportError:
pass

def DontUseExceptions():
""" Disable exceptions in all GDAL related modules (osgeo.gdal, osgeo.ogr, osgeo.osr, osgeo.gnm).
Note: prior to GDAL 3.7, this only affected the calling modue"""

try:
from . import gdal
gdal._DontUseExceptions()
except ImportError:
pass
try:
from . import ogr
ogr._DontUseExceptions()
except ImportError:
pass
try:
from . import osr
osr._DontUseExceptions()
except ImportError:
pass
try:
from . import gnm
gnm._DontUseExceptions()
except ImportError:
pass

%}
4 changes: 2 additions & 2 deletions swig/python/gdal-utils/scripts/gdal2tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# Running main() must be protected that way due to use of multiprocessing on Windows:
# https://docs.python.org/3/library/multiprocessing.html#the-spawn-and-forkserver-start-methods
if __name__ == "__main__":
from osgeo.gdal import UseExceptionsAllModules, deprecation_warn
from osgeo.gdal import UseExceptions, deprecation_warn

UseExceptionsAllModules()
UseExceptions()

# import osgeo_utils.gdal2tiles as a convenience to use as a script
from osgeo_utils.gdal2tiles import * # noqa
Expand Down
4 changes: 2 additions & 2 deletions swig/python/gdal-utils/scripts/gdal2xyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import sys

from osgeo.gdal import UseExceptionsAllModules, deprecation_warn
from osgeo.gdal import UseExceptions, deprecation_warn

# import osgeo_utils.gdal2xyz as a convenience to use as a script
from osgeo_utils.gdal2xyz import * # noqa
from osgeo_utils.gdal2xyz import main

UseExceptionsAllModules()
UseExceptions()

deprecation_warn("gdal2xyz")
sys.exit(main(sys.argv))
4 changes: 2 additions & 2 deletions swig/python/gdal-utils/scripts/gdal_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import sys

from osgeo.gdal import UseExceptionsAllModules, deprecation_warn
from osgeo.gdal import UseExceptions, deprecation_warn

# import osgeo_utils.gdal_calc as a convenience to use as a script
from osgeo_utils.gdal_calc import * # noqa
from osgeo_utils.gdal_calc import main

UseExceptionsAllModules()
UseExceptions()

deprecation_warn("gdal_calc")
sys.exit(main(sys.argv))
4 changes: 2 additions & 2 deletions swig/python/gdal-utils/scripts/gdal_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import sys

from osgeo.gdal import UseExceptionsAllModules, deprecation_warn
from osgeo.gdal import UseExceptions, deprecation_warn

# import osgeo_utils.gdal_edit as a convenience to use as a script
from osgeo_utils.gdal_edit import * # noqa
from osgeo_utils.gdal_edit import main

UseExceptionsAllModules()
UseExceptions()

deprecation_warn("gdal_edit")
sys.exit(main(sys.argv))
4 changes: 2 additions & 2 deletions swig/python/gdal-utils/scripts/gdal_fillnodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import sys

from osgeo.gdal import UseExceptionsAllModules, deprecation_warn
from osgeo.gdal import UseExceptions, deprecation_warn

# import osgeo_utils.gdal_fillnodata as a convenience to use as a script
from osgeo_utils.gdal_fillnodata import * # noqa
from osgeo_utils.gdal_fillnodata import main

UseExceptionsAllModules()
UseExceptions()

deprecation_warn("gdal_fillnodata")
sys.exit(main(sys.argv))
4 changes: 2 additions & 2 deletions swig/python/gdal-utils/scripts/gdal_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import sys

from osgeo.gdal import UseExceptionsAllModules, deprecation_warn
from osgeo.gdal import UseExceptions, deprecation_warn

# import osgeo_utils.gdal_merge as a convenience to use as a script
from osgeo_utils.gdal_merge import * # noqa
from osgeo_utils.gdal_merge import main

UseExceptionsAllModules()
UseExceptions()

deprecation_warn("gdal_merge")
sys.exit(main(sys.argv))
4 changes: 2 additions & 2 deletions swig/python/gdal-utils/scripts/gdal_pansharpen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import sys

from osgeo.gdal import UseExceptionsAllModules, deprecation_warn
from osgeo.gdal import UseExceptions, deprecation_warn

# import osgeo_utils.gdal_pansharpen as a convenience to use as a script
from osgeo_utils.gdal_pansharpen import * # noqa
from osgeo_utils.gdal_pansharpen import main

UseExceptionsAllModules()
UseExceptions()

deprecation_warn("gdal_pansharpen")
sys.exit(main(sys.argv))
4 changes: 2 additions & 2 deletions swig/python/gdal-utils/scripts/gdal_polygonize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import sys

from osgeo.gdal import UseExceptionsAllModules, deprecation_warn
from osgeo.gdal import UseExceptions, deprecation_warn

# import osgeo_utils.gdal_polygonize as a convenience to use as a script
from osgeo_utils.gdal_polygonize import * # noqa
from osgeo_utils.gdal_polygonize import main

UseExceptionsAllModules()
UseExceptions()

deprecation_warn("gdal_polygonize")
sys.exit(main(sys.argv))
4 changes: 2 additions & 2 deletions swig/python/gdal-utils/scripts/gdal_proximity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import sys

from osgeo.gdal import UseExceptionsAllModules, deprecation_warn
from osgeo.gdal import UseExceptions, deprecation_warn

# import osgeo_utils.gdal_proximity as a convenience to use as a script
from osgeo_utils.gdal_proximity import * # noqa
from osgeo_utils.gdal_proximity import main

UseExceptionsAllModules()
UseExceptions()

deprecation_warn("gdal_proximity")
sys.exit(main(sys.argv))
Loading

0 comments on commit a1df6dc

Please sign in to comment.