Skip to content

Commit 00a8f1c

Browse files
Merge pull request #447 from emlys/task/446
Move `GDALUseExceptions` and `gdal_use_exceptions` into new utils module
2 parents 27ccf7a + a2356cf commit 00a8f1c

11 files changed

Lines changed: 44 additions & 41 deletions

File tree

src/pygeoprocessing/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
from .geoprocessing_core import calculate_slope
5151
from .geoprocessing_core import raster_band_percentile
5252
from .slurm_utils import log_warning_if_gdal_will_exhaust_slurm_memory
53+
from .utils import GDALUseExceptions
54+
from .utils import gdal_use_exceptions
5355

5456
try:
5557
__version__ = version('pygeoprocessing')

src/pygeoprocessing/geoprocessing.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@
3636
from .geoprocessing_core import DEFAULT_CREATION_OPTIONS
3737
from .geoprocessing_core import DEFAULT_GTIFF_CREATION_TUPLE_OPTIONS
3838
from .geoprocessing_core import DEFAULT_OSR_AXIS_MAPPING_STRATEGY
39-
from .geoprocessing_core import gdal_use_exceptions
40-
from .geoprocessing_core import GDALUseExceptions
4139
from .geoprocessing_core import INT8_CREATION_OPTIONS
40+
from .utils import GDALUseExceptions, gdal_use_exceptions
4241

4342
# This is used to efficiently pass data to the raster stats worker if available
4443
if sys.version_info >= (3, 8):

src/pygeoprocessing/geoprocessing_core.pyx

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ from osgeo import osr
2828
import numpy
2929
import pygeoprocessing
3030
from .extensions cimport FastFileIterator, FastFileIteratorCompare, is_close
31+
from .utils import gdal_use_exceptions
3132

3233

3334
BLOCK_BITS = 8
@@ -50,38 +51,6 @@ DEFAULT_OSR_AXIS_MAPPING_STRATEGY = osr.OAMS_TRADITIONAL_GIS_ORDER
5051

5152
LOGGER = logging.getLogger('pygeoprocessing.geoprocessing_core')
5253

53-
54-
class GDALUseExceptions:
55-
"""Context manager that enables GDAL exceptions and restores state after."""
56-
57-
def __init__(self):
58-
pass
59-
60-
def __enter__(self):
61-
self.currentUseExceptions = gdal.GetUseExceptions()
62-
gdal.UseExceptions()
63-
64-
def __exit__(self, exc_type, exc_val, exc_tb):
65-
if self.currentUseExceptions == 0:
66-
gdal.DontUseExceptions()
67-
68-
69-
def gdal_use_exceptions(func):
70-
"""Decorator that enables GDAL exceptions and restores state after.
71-
72-
Args:
73-
func (callable): function to call with GDAL exceptions enabled
74-
75-
Returns:
76-
Wrapper function that calls ``func`` with GDAL exceptions enabled
77-
"""
78-
@functools.wraps(func)
79-
def wrapper(*args, **kwargs):
80-
with GDALUseExceptions():
81-
return func(*args, **kwargs)
82-
return wrapper
83-
84-
8554
cdef float _NODATA = -1.0
8655

8756
# This resolves an issue on Mac OS X Catalina where cimporting ``push_heap``

src/pygeoprocessing/kernels.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from numpy.typing import ArrayLike
4242
from osgeo import gdal
4343

44-
from .geoprocessing_core import gdal_use_exceptions
44+
from .utils import gdal_use_exceptions
4545

4646
FLOAT32_NODATA = float(numpy.finfo(numpy.float32).min)
4747
LOGGER = logging.getLogger(__name__)

src/pygeoprocessing/multiprocessing/raster_calculator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from ..geoprocessing import iterblocks
2323
from ..geoprocessing import LOGGER
2424
from ..geoprocessing_core import DEFAULT_GTIFF_CREATION_TUPLE_OPTIONS
25-
from ..geoprocessing_core import GDALUseExceptions
25+
from ..utils import GDALUseExceptions
2626

2727
if sys.version_info >= (3, 8):
2828
import multiprocessing.shared_memory

src/pygeoprocessing/routing/helper_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from ..geoprocessing import get_raster_info
55
from ..geoprocessing import raster_calculator
66
from ..geoprocessing_core import DEFAULT_GTIFF_CREATION_TUPLE_OPTIONS
7-
from ..geoprocessing_core import gdal_use_exceptions
7+
from ..utils import gdal_use_exceptions
88

99

1010
@gdal_use_exceptions

src/pygeoprocessing/routing/routing.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ import shapely.ops
6161
import scipy.stats
6262

6363
from ..geoprocessing_core import DEFAULT_OSR_AXIS_MAPPING_STRATEGY
64-
from ..geoprocessing_core import gdal_use_exceptions, GDALUseExceptions
6564
from ..geoprocessing_core import DEFAULT_GTIFF_CREATION_TUPLE_OPTIONS
6665
from ..geoprocessing_core import SPARSE_CREATION_OPTIONS, BLOCK_BITS
6766
from ..extensions cimport ManagedRaster, is_close
6867
from ..extensions cimport COL_OFFSETS, ROW_OFFSETS, INFLOW_OFFSETS
68+
from ..utils import gdal_use_exceptions, GDALUseExceptions
6969
import pygeoprocessing
7070

7171
LOGGER = logging.getLogger(__name__)

src/pygeoprocessing/routing/watershed.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import shapely.prepared
2626
import shapely.wkb
2727

2828
import pygeoprocessing
29-
from ..geoprocessing_core import gdal_use_exceptions, GDALUseExceptions
3029
from ..geoprocessing_core import SPARSE_CREATION_OPTIONS
3130
from ..extensions cimport ManagedRaster
31+
from ..utils import gdal_use_exceptions, GDALUseExceptions
3232

3333
LOGGER = logging.getLogger(__name__)
3434

src/pygeoprocessing/slurm_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from osgeo import gdal
66

7-
from .geoprocessing_core import gdal_use_exceptions
7+
from .utils import gdal_use_exceptions
88

99
LOGGER = logging.getLogger(__name__)
1010

src/pygeoprocessing/utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import functools
2+
3+
from osgeo import gdal
4+
5+
class GDALUseExceptions:
6+
"""Context manager that enables GDAL exceptions and restores state after."""
7+
8+
def __init__(self):
9+
pass
10+
11+
def __enter__(self):
12+
self.currentUseExceptions = gdal.GetUseExceptions()
13+
gdal.UseExceptions()
14+
15+
def __exit__(self, exc_type, exc_val, exc_tb):
16+
if self.currentUseExceptions == 0:
17+
gdal.DontUseExceptions()
18+
19+
20+
def gdal_use_exceptions(func):
21+
"""Decorator that enables GDAL exceptions and restores state after.
22+
23+
Args:
24+
func (callable): function to call with GDAL exceptions enabled
25+
26+
Returns:
27+
Wrapper function that calls ``func`` with GDAL exceptions enabled
28+
"""
29+
@functools.wraps(func)
30+
def wrapper(*args, **kwargs):
31+
with GDALUseExceptions():
32+
return func(*args, **kwargs)
33+
return wrapper

0 commit comments

Comments
 (0)