Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#2688 closed defect (fixed)

On Windows, after calling ogr.UseExceptions(), Python crashes when OGR error occurs

Reported by: jjr8 Owned by: hobu
Priority: high Milestone: 1.5.4
Component: PythonBindings Version: 1.5.2
Severity: normal Keywords: exceptions
Cc: warmerdam


As I understand it, Python programs should be able to call ogr.UseExceptions() to instruct OGR to raise exceptions on error rather than writing error messages to stderr / stdout stream (I'm not sure which it uses). But on Windows, when you call ogr.UseExceptions() and then provoke an error, the python.exe process crashes with the "python.exe has encountered a problem and needs to close" message. The exception code is 0xC0000005 (access violation).

Repro steps:

  1. Install Python 2.5.2 to normal location (C:\Python25).
  1. Install GDAL for Python, following instructions on
    1. Download and unzip to C:\gdalwin32-1.5
    2. Add C:\gdalwin32-1.5\bin to the PATH and set GDAL_DATA to C:\gdalwin32-1.5\data
    3. Download and run
    4. From a CMD shell, run C:\Python25\Scripts\easy_install gdal.
  1. From a CMD shell, run C:\Python25\python.exe and run this:
>>> from osgeo import ogr
>>> driver = ogr.GetDriverByName("ESRI Shapefile")
>>> ds = driver.CreateDataSource("C:\\Test")
>>> ds.ExecuteSQL('asdf')
ERROR 1: SQL: Missing keyword SELECT
>>> ogr.UseExceptions()
>>> ds.ExecuteSQL('asdf')       # This causes python.exe to crash

In this example, I provoked an error by specifying invalid syntax to ExecuteSQL(). But the problem is not with the ExecuteSQL() statement specifically. You can provoke errors with different OGR APIs and the same problem will happen: python.exe will crash.

The expected result is that a Python exception will be raised. Please advise me if this is not correct.

If exceptions are not supposed to be supported on Python, can you say how it is that the program can obtain error messages, short of trying to hijack the stderr stream and capture the output? For GUI applications, the user cannot even see the output streams so the program must be able to provide the error message some other way.

Thanks for looking at this. I picked the 1.6.0 milestone for this ticket but I do not presume to know your priorities. Please prioritize appropriately.

Change History (4)

comment:1 Changed 11 years ago by warmerdam

Cc: warmerdam added
Keywords: exceptions added
Priority: normalhigh

I have confirmed this with OSGeo4W (1.5 package and gdal-dev package).

This problem does not occur on linux.

Somehow I think this is an FAQ - that we have run into it before. Leaving to hobu to follow up.

Howard, if you won't have time to investigate before RC1 please let me know so I can dig into it.

comment:2 Changed 11 years ago by warmerdam

Resolution: fixed
Status: newclosed

As observed by Even, the problem was the missing CPL_STDCALL attribute on VeryQuietErrorHandler?.

I have corrected this in trunk (r15822) and 1.5 branch (r15823) in time for the 1.6 release and 1.5.4 releases.

comment:3 Changed 11 years ago by Even Rouault


comment:4 Changed 11 years ago by jjr8

I confirm this is fixed with the final build of GDAL 1.6.0, with Python bindings installed using easy_install on 12-Dec-2008 10:30 EST, on Win32 for both Python 2.4 and 2.5:

>>> from osgeo import ogr
>>> driver = ogr.GetDriverByName("ESRI Shapefile")
>>> ds = driver.CreateDataSource("C:\\Test4")
>>> ds.ExecuteSQL('asdf')
ERROR 1: SQL: Missing keyword SELECT
>>> ogr.UseExceptions()
>>> ds.ExecuteSQL('asdf')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\python25\lib\site-packages\GDAL-1.6.0-py2.5-win32.egg\osgeo\", line 508, in ExecuteSQL
    return _ogr.DataSource_ExecuteSQL(*args, **kwargs)
RuntimeError: SQL: Missing keyword SELECT

Note: See TracTickets for help on using tickets.