Push and pop error handler within Env context

Closes #876
Closes #902
This commit is contained in:
Sean Gillies 2016-10-03 17:43:29 +02:00
parent 51c6e6294d
commit 2f1ee243ba
4 changed files with 16 additions and 7 deletions

View File

@ -9,9 +9,9 @@ import sys
from rasterio.compat import string_types
from rasterio._gdal cimport (
CPLSetConfigOption, CPLSetErrorHandler, GDALAllRegister, GDALGetDriver,
CPLSetConfigOption, GDALAllRegister, GDALGetDriver,
GDALGetDriverCount, GDALGetDriverLongName, GDALGetDriverShortName,
OGRGetDriverCount, OGRRegisterAll)
OGRGetDriverCount, OGRRegisterAll, CPLPopErrorHandler, CPLPushErrorHandler)
include "gdal.pxi"
@ -131,7 +131,9 @@ cdef class GDALEnv(ConfigEnv):
def start(self):
GDALAllRegister()
OGRRegisterAll()
CPLSetErrorHandler(<CPLErrorHandler>errorHandler)
CPLPushErrorHandler(<CPLErrorHandler>errorHandler)
log.debug("Error handler pushed")
if driver_count() == 0:
raise ValueError("Drivers not registered")
@ -153,6 +155,8 @@ cdef class GDALEnv(ConfigEnv):
# NB: do not restore the CPL error handler to its default
# state here. If you do, log messages will be written to stderr
# by GDAL instead of being sent to Python's logging module.
CPLPopErrorHandler()
log.debug("Error handler popped")
log.debug("Env %r has been stopped", self)
def drivers(self):

View File

@ -29,7 +29,8 @@ cdef extern from "cpl_error.h" nogil:
int CPLGetLastErrorNo()
const char* CPLGetLastErrorMsg()
CPLErr CPLGetLastErrorType()
void CPLSetErrorHandler(CPLErrorHandler handler)
void CPLPushErrorHandler(CPLErrorHandler handler)
void CPLPopErrorHandler()
cdef extern from "ogr_srs_api.h" nogil:

View File

@ -1,5 +1,6 @@
"""Rasterio's GDAL/AWS environment"""
from functools import wraps
import logging
from rasterio._drivers import (
@ -197,5 +198,8 @@ def delenv():
def ensure_env(f):
"""A decorator that ensures an env exists before a function
calls any GDAL C functions."""
defenv()
return f
@wraps(f)
def wrapper(*args, **kwds):
with Env():
return f(*args, **kwds)
return wrapper

View File

@ -72,7 +72,7 @@ def test_ensure_env_decorator(gdalenv):
def f(x):
return x
wrapper = ensure_env(f)
assert wrapper == f
assert wrapper.func_name == f.func_name
def test_no_aws_gdal_config(gdalenv):