Project

Profile

Help

Any (cross platform) Python samples that show proper exception handling the SaxonC 12.1 API?

Added by Martin Honnen over 1 year ago

A new feature in 12.1 for SaxonC is the use of exceptions, as far as I understand it. The documentation https://www.saxonica.com/saxon-c/doc12/html/saxonc.html#PySaxonApiError has that PySaxonApiError so my first instinct was to replace any if ...exception_occurred with e.g. try: .. except PySaxonApiError as e: ... but additionally the section https://www.saxonica.com/saxon-c/documentation12/index.html#!changes/api has a note "For the Python API on Windows the PySaxonApiError is not available therefore we throw RuntimeError if there are any failures".

What does that mean for proper exception handling in cross platform Python code with SaxonC 12.1? I looked whether the samples have examples but I didn't find anything except in the test suite where however any Exception is caught.

What is the right way to handle exceptions thrown by Saxon?

try:
  ..
except RuntimeError as e:
  ..

?

Any thoughts appreciated.


Replies (3)

Please register to reply

RE: Any (cross platform) Python samples that show proper exception handling the SaxonC 12.1 API? - Added by O'Neil Delpratt over 1 year ago

For cross-platform Python code the RuntimeError will work. Alternatively following should work too:

try:
  ..
except Exception as e:

Any (cross platform) Python samples that show proper exception handling the SaxonC 12.1 API? - Added by Norm Tovey-Walsh over 1 year ago

What does that mean for proper exception handling in cross platform Python code with
SaxonC 12.1? I looked whether the samples have examples but I didn't find anything
except in the test suite where however any Exception is caught.

What is the right way to handle exceptions thrown by Saxon?

Switching to a RuntimeError on Windows was a last minute workaround for
compiler/linker issues on Windows. Attempting to throw the
SaxonApiException on Windows resulted in linking errors[1] that we
couldn’t diagnose. (Suggestions most gratefully accepted.)

I think the short-term solution for cross-platform exception handling
with 12.1 is to catch SaxonApiException and RuntimeError. Defensively,
you can check in the RuntimeError handler if the platform is Windows
and the consider what to do with the exception.

I hope that we can resolve this problem so that SaxonApiException is
thrown consistently on all the platforms. Releasing 12.1 with
different behavior on Windows seemed like the right compromise given
that we have no idea how long it might take to work out what is going on
with the Windows/linker/buildwheel problem.

Be seeing you,
norm

[1] https://github.com/Saxonica/BuildWheels/actions/runs/4468328530/jobs/7848941004

--
Norm Tovey-Walsh
Saxonica

RE: Any (cross platform) Python samples that show proper exception handling the SaxonC 12.1 API? - Added by O'Neil Delpratt over 1 year ago

I have created the following bug issue to track progress of this issue: #5936

    (1-3/3)

    Please register to reply