Project

Profile

Help

Is an error on calling a system function somehow handleable from the Python API?

Added by Martin Honnen almost 2 years ago

I wonder whether/how a dynamic error occurring during a call to a system function can be handled from the Python API.

Example:

from saxonche import *

with PySaxonProcessor(license=True) as proc:
    print(proc.version)
    parse_ietf_date = PyXdmFunctionItem().get_system_function(proc, "{http://www.w3.org/2005/xpath-functions}parse-ietf-date", 1)
    for date in ["Wed, 06 Jun 1994 07:29:35 GMT", "We, 06 Jun 1994 07:29:35 GMT"]:
        date_xdm_string = proc.make_string_value(date)
        result = parse_ietf_date.call(proc, [date_xdm_string])
        if proc.exception_occurred:
            print(proc.error_message)
        else:
            print(result)

Output (probably from System.err) shows the dynamic error but the code doesn't seem to notice on proc.exception occurred:

SaxonC-HE 12.0 from Saxonica
1994-06-06T07:29:35Z
None
Exception found in XdmFunctionItem.call Message= Invalid IETF date value We, 06 Jun 1994 07:29:35 GMT (String expected to begin with month name or day name (or day number))

I also tried with Python's try/except but it doesn't seem to catch that e.g.

from saxonche import *

with PySaxonProcessor(license=True) as proc:
    print(proc.version)
    parse_ietf_date = PyXdmFunctionItem().get_system_function(proc, "{http://www.w3.org/2005/xpath-functions}parse-ietf-date", 1)
    for date in ["Wed, 06 Jun 1994 07:29:35 GMT", "We, 06 Jun 1994 07:29:35 GMT"]:
        date_xdm_string = proc.make_string_value(date)
        try:
            result = parse_ietf_date.call(proc, [date_xdm_string])
            print(result)
        except Exception as err:
            print('Error during function call', err)

outputs

Exception found in XdmFunctionItem.call Message= Invalid IETF date value We, 06 Jun 1994 07:29:35 GMT (String expected to begin with month name or day name (or day number))
SaxonC-HE 12.0 from Saxonica
1994-06-06T07:29:35Z
None

Replies (2)

RE: Is an error on calling a system function somehow handleable from the Python API? - Added by O'Neil Delpratt almost 2 years ago

Thanks Martin for reporting this issue.

This is indeed a bug. In the C++ method call of the class XdmFunctionItem when we detect an exception from Java we incorrectly send the exception message to the System.err.

There is a gap here in the handing of exceptions for Xdm objects. Specifically for the dynamic function calls there maybe exceptions. With Graalvm we have more control in how we handle exceptions. Therefore I think there are two solutions: either we pass the information of the thrown exception back to the processor or we throw the exception from the C++ code, which Python should be able to pick up as a Python exception, which can be caught as in the code above.

I will file a bug issue and work on a solution.

    (1-2/2)

    Please register to reply