From 335468e02a671e325295c07b6c2e7e3ec77d7948 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Fri, 17 Mar 2023 21:02:25 +0100 Subject: [PATCH] Python bindings: turn on exceptions by default (refs #7452) --- swig/include/gdal_array.i | 22 ---------------------- swig/include/python/gdal_python.i | 1 + swig/include/python/gnm_python.i | 1 + swig/include/python/ogr_python.i | 1 + swig/include/python/osr_python.i | 4 ++++ 5 files changed, 7 insertions(+), 22 deletions(-) diff --git a/swig/include/gdal_array.i b/swig/include/gdal_array.i index 21c32f545b04..156079bf6124 100644 --- a/swig/include/gdal_array.i +++ b/swig/include/gdal_array.i @@ -66,27 +66,6 @@ typedef int GDALRIOResampleAlg; %include "python_strings.i" -%{ -#include "cpl_conv.h" -%} - -%inline %{ -// Note: copied&pasted from python_exceptions.i -static void _StoreLastException() -{ - const char* pszLastErrorMessage = - CPLGetThreadLocalConfigOption("__last_error_message", NULL); - const char* pszLastErrorCode = - CPLGetThreadLocalConfigOption("__last_error_code", NULL); - if( pszLastErrorMessage != NULL && pszLastErrorCode != NULL ) - { - CPLErrorSetState( CE_Failure, - static_cast(atoi(pszLastErrorCode)), - pszLastErrorMessage); - } -} -%} - %{ #include #include "gdal_priv.h" @@ -2087,7 +2066,6 @@ def GDALTypeCodeToNumericTypeCode(gdal_code): def _RaiseException(): if gdal.GetUseExceptions(): - _StoreLastException() raise RuntimeError(gdal.GetLastErrorMsg()) def LoadFile(filename, xoff=0, yoff=0, xsize=None, ysize=None, diff --git a/swig/include/python/gdal_python.i b/swig/include/python/gdal_python.i index f8c22d1e99ad..f93ba27afec0 100644 --- a/swig/include/python/gdal_python.i +++ b/swig/include/python/gdal_python.i @@ -11,6 +11,7 @@ /* gdal_python.i %init code */ if ( GDALGetDriverCount() == 0 ) { GDALAllRegister(); + UseExceptions(); } %} diff --git a/swig/include/python/gnm_python.i b/swig/include/python/gnm_python.i index b0c2f01a70db..7c1208b8c208 100644 --- a/swig/include/python/gnm_python.i +++ b/swig/include/python/gnm_python.i @@ -11,6 +11,7 @@ if ( OGRGetDriverCount() == 0 ) { OGRRegisterAll(); + UseExceptions(); } %} diff --git a/swig/include/python/ogr_python.i b/swig/include/python/ogr_python.i index f2adbd1c37c3..253f923f298e 100644 --- a/swig/include/python/ogr_python.i +++ b/swig/include/python/ogr_python.i @@ -11,6 +11,7 @@ if ( OGRGetDriverCount() == 0 ) { OGRRegisterAll(); + UseExceptions(); } %} diff --git a/swig/include/python/osr_python.i b/swig/include/python/osr_python.i index 60aa88b98651..702daea6909d 100644 --- a/swig/include/python/osr_python.i +++ b/swig/include/python/osr_python.i @@ -6,6 +6,10 @@ %feature("autodoc"); +%init %{ + UseExceptions(); +%} + #ifndef FROM_GDAL_I %{ #define MODULE_NAME "osr"