diff --git a/gdal/swig/include/python/gdal_python.i b/gdal/swig/include/python/gdal_python.i index edc1d7789d05..1c82ae4f3a76 100644 --- a/gdal/swig/include/python/gdal_python.i +++ b/gdal/swig/include/python/gdal_python.i @@ -33,49 +33,9 @@ } %} -/* - * This was the cpl_exceptions.i code. But since python is the only one - * different (should support old method as well as new one) - * it was moved into this file. - */ -%{ -int bUseExceptions=0; - -void VeryQuiteErrorHandler(CPLErr eclass, int code, const char *msg ) { - /* If the error class is CE_Fatal, we want to have a message issued - because the CPL support code does an abort() before any exception - can be generated */ - if (eclass == CE_Fatal ) { - CPLDefaultErrorHandler(eclass, code, msg ); - } -} -%} - -%inline %{ -void UseExceptions() { - bUseExceptions = 1; - CPLSetErrorHandler( (CPLErrorHandler) VeryQuiteErrorHandler ); -} -void DontUseExceptions() { - bUseExceptions = 0; - CPLSetErrorHandler( CPLDefaultErrorHandler ); -} -%} - -%include exception.i - -%exception { - - $action - if ( bUseExceptions ) { - CPLErr eclass = CPLGetLastErrorType(); - if ( eclass == CE_Failure || eclass == CE_Fatal ) { - SWIG_exception( SWIG_RuntimeError, CPLGetLastErrorMsg() ); - } - } -} +%include "python_exceptions.i" %extend GDAL_GCP { %pythoncode { diff --git a/gdal/swig/include/python/ogr_python.i b/gdal/swig/include/python/ogr_python.i index 72ce0fdba8f0..64e29f3a5285 100644 --- a/gdal/swig/include/python/ogr_python.i +++ b/gdal/swig/include/python/ogr_python.i @@ -29,6 +29,8 @@ %rename (SetGenerate_DB2_V72_BYTE_ORDER) OGRSetGenerate_DB2_V72_BYTE_ORDER; %rename (RegisterAll) OGRRegisterAll(); +%include "python_exceptions.i" + %extend OGRDataSourceShadow { %pythoncode { def Destroy(self): diff --git a/gdal/swig/include/python/python_exceptions.i b/gdal/swig/include/python/python_exceptions.i new file mode 100644 index 000000000000..ddbd9ca6feb8 --- /dev/null +++ b/gdal/swig/include/python/python_exceptions.i @@ -0,0 +1,42 @@ +/* + * This was the cpl_exceptions.i code. But since python is the only one + * different (should support old method as well as new one) + * it was moved into this file. + */ +%{ +int bUseExceptions=0; + +void VeryQuiteErrorHandler(CPLErr eclass, int code, const char *msg ) { + /* If the error class is CE_Fatal, we want to have a message issued + because the CPL support code does an abort() before any exception + can be generated */ + if (eclass == CE_Fatal ) { + CPLDefaultErrorHandler(eclass, code, msg ); + } +} +%} + +%inline %{ +void UseExceptions() { + bUseExceptions = 1; + CPLSetErrorHandler( (CPLErrorHandler) VeryQuiteErrorHandler ); +} + +void DontUseExceptions() { + bUseExceptions = 0; + CPLSetErrorHandler( CPLDefaultErrorHandler ); +} +%} + +%include exception.i + +%exception { + + $action + if ( bUseExceptions ) { + CPLErr eclass = CPLGetLastErrorType(); + if ( eclass == CE_Failure || eclass == CE_Fatal ) { + SWIG_exception( SWIG_RuntimeError, CPLGetLastErrorMsg() ); + } + } +}