[gdal-dev] Should a custom error handler (in Python) clear the last error?

Daniel Evans Daniel.Evans at jbarisk.com
Fri Nov 27 07:13:14 PST 2020


Hello,

In the Python bindings, after calling `gdal.UseExceptions()` trying something like an out-of-range raster read will raise an error like:

[ ... traceback ... ]
  File "/home/jbanorthwest.co.uk/danielevans/venvs/farmcat3/lib64/python3.6/site-packages/osgeo/gdal_array.py", line 246, in _RaiseException
    raise RuntimeError(gdal.GetLastErrorMsg())
RuntimeErrorRuntimeError: /data/gfm/maps/FLRF_Q100.vrt, band 1: Access window out of range in RasterIO().  Requested
(1,123456789) of size 1x1 on raster of 1296001x507215.


It is possible to set a custom error handler to determine how log messages should be recorded. For example, to silence all logging:

gdal.SetErrorHandler('CPLQuietErrorHandler')

Or to point to a custom Python error handling function:

gdal.SetErrorHandler(self.log)



This does not affect the act exceptions being raised. However, GDAL no longer shows the last error message:

  File "/home/jbanorthwest.co.uk/danielevans/venvs/farmcat3/lib64/python3.6/site-packages/osgeo/gdal_array.py", line 246, in _RaiseException
    raise RuntimeError(gdal.GetLastErrorMsg())
RuntimeError

This implies that `gdal.GetLastErrorMsg()` is returning nothing, i.e. that the last error has been cleared. However, this seems to be at odds with the C++ documentation for the CPLError() function, which states that:


Regardless of how application error handlers or the default error handler choose to handle an error, the error number, and message will be stored for recovery with CPLGetLastErrorNo()<https://gdal.org/doxygen/cpl__error_8h.html#a4a583fe987b8f1a7b2468805578fa8d4> and CPLGetLastErrorMsg()<https://gdal.org/doxygen/cpl__error_8h.html#a7f71ade3bb0a0e9e45802975ec59ff5e>.
https://gdal.org/doxygen/cpl__error_8h.html#afda4d86428c1c533449ae6a69cdf430d


Is the error message being cleared incorrectly?


Dr Daniel Evans
Software Developer


[JBA COVID-19 statement]<https://www.jbagroup.co.uk/sites/www.jbagroup.co.uk/files/documents/15-030%20JBA%20Business%20Continuity%20Briefing%20-%20Latest.pdf>

T +44 (0) 1756 799919
www.jbarisk.com<http://www.jbarisk.com>

[Visit our website]<http://www.jbarisk.com> [http://www.jbagroup.co.uk/imgstore/JBA-Email-Sig-Icons-LINKEDIN.png]  [Follow us on Twitter] <https://twitter.com/jbarisk>

Find out more about us here: www.jbarisk.com<http://www.jbarisk.com/> and follow us on Twitter @JBARisk<http://twitter.com/JBARisk> and LinkedIn<https://www.linkedin.com/company/2370847?trk=tyah&trkInfo=clickedVertical%3Acompany%2CclickedEntityId%3A2370847%2Cidx%3A2-1-2%2CtarId%3A1447414259786%2Ctas%3AJBA%20RISK%20MANAGEMENT>

The JBA Group supports the JBA Trust.

All JBA Risk Management's email messages contain confidential information and are intended only for the individual(s) named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail.
Please notify the sender immediately by email if you have received this email by mistake and delete this email from your system.


JBA Risk Management Limited is registered in England, company number 07732946, 1 Broughton Park, Old Lane North, Broughton, Skipton, North Yorkshire, BD23 3FD, Telephone: +441756799919


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20201127/88dff911/attachment.html>


More information about the gdal-dev mailing list