Declare errorHandler() with gil
Declare errorHandler() with gil

I've run recently into an incompatibility between rasterio and osgeo.gdal

Since recently in GDAL master, osgeo.gdal bindings are generated with an option
that cause the GIL to be released before going to GDAL native code :
https://trac.osgeo.org/gdal/ticket/6649

I now observe crashes when running the following

import rasterio
from osgeo import gdal
gdal.OpenEx('i_dont_exist', gdal.OF_RASTER | gdal.OF_VERBOSE_ERROR)

The stack trace is
Program received signal SIGSEGV, Segmentation fault.
0x00007fffe1507401 in __Pyx_PyObject_Call (kw=0x0, arg=0x7fffe177f750, func=0x7ffff67b5cf8) at rasterio/_drivers.c:4620
4620        if (unlikely(Py_EnterRecursiveCall((char*)" while calling a Python object")))
(gdb) bt
    __pyx_v_msg=0xe6ea38 "i_dont_exist: No such file or directory") at rasterio/_drivers.c:1100
    papszOpenOptions=<optimized out>, papszSiblingFiles=<optimized out>) at gdaldataset.cpp:2886
    allowed_drivers=allowed_drivers@entry=0x0, open_options=open_options@entry=0x0, sibling_files=sibling_files@entry=0x0) at extensions/gdal_wrap.cpp:5887
    rtld_fini=<optimized out>, stack_end=0x7fffffffe398) at libc-start.c:287

The issue is that the rasterio errorHandler callback is called with the
GIL released, but it does use Python stuff.
So the GIL should be re-acquired in the callback. (which is done in osgeo.gdal
too with Python error and progress callbacks)

http://docs.cython.org/en/latest/src/userguide/external_C_code.html#acquiring-and-releasing-the-gil

brendan-ward/rasterio,brendan-ward/rasterio,brendan-ward/rasterio