Project

Profile

Help

Bug #5944

closed

set_cwd on PySchemaValidator seems to be ignored when registering schema with relative xsd_file name argument or validating file_name with relative file name

Added by Martin Honnen about 1 year ago. Updated 12 months ago.

Status:
Closed
Priority:
Normal
Category:
Python API
Start date:
2023-03-29
Due date:
% Done:

100%

Estimated time:
Found in version:
12.1
Fixed in version:
12.2
Platforms:

Description

Using SaxonC EE 12.1 under both Windows and Linux it seems using set_cwd on an PySchemaValidator doesn't have to be any effect on the subsequent use of e.g. validator.register_schema(xsd_file='schema2.xsd') or validator.validate(file_name= as the error message I then get suggest the file is looked for in the current working directory the Python file is called from but not the one set with set_cwd.

Examples: my failing PHP code does e.g.

from pathlib import Path
from saxoncee import *

with PySaxonProcessor(license=True) as saxon_processor:

    print(saxon_processor.version)

    validator = saxon_processor.new_schema_validator()

    input_path = Path.cwd() / '..' / 'test-schemas'

    input_path = str(input_path.resolve())

    print(input_path)

    validator.set_cwd(input_path)

    try:
        validator.register_schema(xsd_file='schema2.xsd')
        validator.validate(file_name='sample2-well-formed-invalid.xml')
        print('XML is valid.')
    except RuntimeError as e:
        print(f'Validation failed: {e}')

and the output then is e.g.

(saxonc-12.1-validatation-test1) mh@LibertyDell:~/saxonc-12.1-validatation-test1$ pwd
/home/mh/saxonc-12.1-validatation-test1
(saxonc-12.1-validatation-test1) mh@LibertyDell:~/saxonc-12.1-validatation-test1$ python test-invalid-sample2.py
SaxonC-EE 12.1 from Saxonica
/home/mh/test-schemas
Validation failed: java.io.FileNotFoundException: /home/mh/saxonc-12.1-validatation-test1/schema2.xsd (No such file or directory). Line number: -1

so while I set cwd to e.g. /home/mh/test-schemas the file schema2.xsd is looked for in the working directory /home/mh/saxonc-12.1-validatation-test1 the python file is called in.

Putting the schema in the working directory the Python file is called from works as it does work to not rely on set_cwd and have explicit paths on the individual arguments e.g.

        validator.register_schema(xsd_file=str(Path.cwd() / '..' / 'test-schemas' / 'schema2.xsd'))
        validator.validate(file_name=str(Path.cwd() / '..' / 'test-schemas' / 'sample2-well-formed-invalid.xml'))

but I wonder what is the point of having the set_cwd method.


Files

validate-set-cwd-failure.zip (1.26 KB) validate-set-cwd-failure.zip Martin Honnen, 2023-03-29 15:30
Actions #1

Updated by Martin Honnen about 1 year ago

I am attaching a zip with the relevant file/directory structure (i.e. the Python file in one directory and the sample schema and sample XML in another directory).

Actions #2

Updated by O'Neil Delpratt about 1 year ago

  • Status changed from New to In Progress

Thanks Martin for reporting this issue. I have managed to reproduce it. Investigating it further.

Actions #3

Updated by O'Neil Delpratt about 1 year ago

  • Status changed from In Progress to Resolved
  • % Done changed from 0 to 100
  • Found in version set to 12.1

Bug fixed in the C++ code.

As a workaround you can set the cwd on the PySaxonProcessor before creating the PySchemaValidator, which works for me:

saxon_processor.set_cwd(input_path)
Actions #4

Updated by Martin Honnen about 1 year ago

So the workaround would be like this?

from pathlib import Path
from saxoncee import *

with PySaxonProcessor(license=True) as saxon_processor:

    print(saxon_processor.version)

    input_path = Path.cwd() / '..' / 'test-schemas'

    input_path = str(input_path.resolve())

    print(input_path)

    saxon_processor.set_cwd(input_path)

    validator = saxon_processor.new_schema_validator()

    try:
        validator.register_schema(xsd_file='schema2.xsd')
        validator.validate(file_name='sample2-well-formed-invalid.xml')
        print('XML is valid.')
    except RuntimeError as e:
        print(f'Validation failed: {e}')

For me that doesn't avoid the error:

(saxonc-12.1-validatation-test1) mh@LibertyDell:~/saxonc-12.1-validatation-test1$ pwd
/home/mh/saxonc-12.1-validatation-test1
(saxonc-12.1-validatation-test1) mh@LibertyDell:~/saxonc-12.1-validatation-test1$ cat test-invalid-sample4.py
from pathlib import Path
from saxoncee import *

with PySaxonProcessor(license=True) as saxon_processor:

    print(saxon_processor.version)

    input_path = Path.cwd() / '..' / 'test-schemas'

    input_path = str(input_path.resolve())

    print(input_path)

    saxon_processor.set_cwd(input_path)

    validator = saxon_processor.new_schema_validator()

    try:
        validator.register_schema(xsd_file='schema2.xsd')
        validator.validate(file_name='sample2-well-formed-invalid.xml')
        print('XML is valid.')
    except RuntimeError as e:
        print(f'Validation failed: {e}')
(saxonc-12.1-validatation-test1) mh@LibertyDell:~/saxonc-12.1-validatation-test1$ python test-invalid-sample4.py
SaxonC-EE 12.1 from Saxonica
/home/mh/test-schemas
Validation failed: java.io.FileNotFoundException: /home/mh/saxonc-12.1-validatation-test1/schema2.xsd (No such file or directory). Line number: -1
Actions #5

Updated by O'Neil Delpratt about 1 year ago

Yes I reproduced that error too with SaxonC 12.1, but the SaxonC 12.2 (pre-release) works as expected. This might be related to a resolving bug.

Thats a shame, you may have to wait for the next maintenance release.

Actions #6

Updated by O'Neil Delpratt 12 months ago

  • Status changed from Resolved to Closed
  • Fixed in version set to 12.2

Bug fix applied in the SaxonC 12.2 maintenance release.

Please register to edit this issue

Also available in: Atom PDF