Skip to content

Commit

Permalink
python_exceptions.i: make it thread friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Mar 18, 2023
1 parent 7d6c214 commit e90d65e
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions swig/include/python/python_exceptions.i
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/
%{
static int bUseExceptions=0;
static CPLErrorHandler pfnPreviousHandler = CPLDefaultErrorHandler;
static thread_local int bUseExceptionsLocal = -1;
static thread_local CPLErrorHandler pfnPreviousHandler = CPLDefaultErrorHandler;

static void CPL_STDCALL
PythonBindingErrorHandler(CPLErr eclass, int code, const char *msg )
Expand Down Expand Up @@ -45,11 +46,37 @@ PythonBindingErrorHandler(CPLErr eclass, int code, const char *msg )
result = GetUseExceptions();
}

%exception _GetExceptionsLocal
{
%#ifdef SED_HACKS
if( bUseExceptions ) bLocalUseExceptionsCode = FALSE;
%#endif
$action
}

%exception _SetExceptionsLocal
{
%#ifdef SED_HACKS
if( bUseExceptions ) bLocalUseExceptionsCode = FALSE;
%#endif
$action
}

%inline %{

static
int GetUseExceptions() {
return bUseExceptions;
return bUseExceptionsLocal >= 0 ? bUseExceptionsLocal : bUseExceptions;
}

static int _GetExceptionsLocal()
{
return bUseExceptionsLocal;
}

static void _SetExceptionsLocal(int bVal)
{
bUseExceptionsLocal = bVal;
}

static
Expand Down Expand Up @@ -136,7 +163,7 @@ static void popErrorHandler()
%include exception.i

%exception {
const int bLocalUseExceptions = bUseExceptions;
const int bLocalUseExceptions = GetUseExceptions();
if ( bLocalUseExceptions ) {
pushErrorHandler();
}
Expand All @@ -155,7 +182,7 @@ static void popErrorHandler()
}

%feature("except") Open {
const int bLocalUseExceptions = bUseExceptions;
const int bLocalUseExceptions = GetUseExceptions();
if ( bLocalUseExceptions ) {
pushErrorHandler();
}
Expand All @@ -180,7 +207,7 @@ static void popErrorHandler()
}

%feature("except") OpenShared {
const int bLocalUseExceptions = bUseExceptions;
const int bLocalUseExceptions = GetUseExceptions();
if ( bLocalUseExceptions ) {
pushErrorHandler();
}
Expand All @@ -205,7 +232,7 @@ static void popErrorHandler()
}

%feature("except") OpenEx {
const int bLocalUseExceptions = bUseExceptions;
const int bLocalUseExceptions = GetUseExceptions();
if ( bLocalUseExceptions ) {
pushErrorHandler();
}
Expand Down Expand Up @@ -266,20 +293,13 @@ static void popErrorHandler()
set it to the state requested for the context
"""
self.currentUseExceptions = (GetUseExceptions() != 0)

if self.requestedUseExceptions:
UseExceptions()
else:
DontUseExceptions()
self.currentUseExceptions = _GetExceptionsLocal()
_SetExceptionsLocal(self.requestedUseExceptions)

def __exit__(self, exc_type, exc_val, exc_tb):
"""
On exit, restore the GDAL/OGR/OSR/GNM exception state which was
current on entry to the context
"""
if self.currentUseExceptions:
UseExceptions()
else:
DontUseExceptions()
_SetExceptionsLocal(self.currentUseExceptions)
%}

0 comments on commit e90d65e

Please sign in to comment.