swig/python_exceptions.i: Add Context Manager for handling Python exception state #6637
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Allow the state of using exceptions to be set/reset locally, within a Python
with
statementThe Python gdal/ogr/osr/gnm modules each have a global state flag determining whether or not errors raise Exceptions. It defaults to not, and can be set or unset. However, because this is global, conflicts can arise in what a section of code requires, sometimes in code from other libraries. To avoid interfering with other code, one often needs to save current state, set desired state, perform some actions, then reset to saved state.
This PR adds a Python Context Manager class which handles this process, so that a Python
with
statement can have a desired state within that block, and not interact with the global state.It is added to the generic
python_exceptions.i
SWIG code, so it appears in each of the different GDAL modules.