From 66e0101bd0b5e2cf655afa2a9cfc397a0d378e01 Mon Sep 17 00:00:00 2001 From: Howard Butler Date: Mon, 14 May 2007 20:23:07 +0000 Subject: [PATCH] consolidate exception stuff for Python so both GDAL and OGR can use it (https://trac.osgeo.org/gdal/ticket/1638) git-svn-id: https://svn.osgeo.org/gdal/trunk@11517 f0d54148-0727-0410-94bb-9a71ac55c965 --- gdal/swig/include/python/gdal_python.i | 42 +------------------- gdal/swig/include/python/ogr_python.i | 2 + gdal/swig/include/python/python_exceptions.i | 42 ++++++++++++++++++++ 3 files changed, 45 insertions(+), 41 deletions(-) create mode 100644 gdal/swig/include/python/python_exceptions.i 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() ); + } + } +}