xmlschema icon indicating copy to clipboard operation
xmlschema copied to clipboard

Import errors with UNC paths on Windows (regression in 3.4.2)

Open nrusch opened this issue 2 months ago • 0 comments

Hello,

We run a set of Python dependencies (including xmlschema) from a UNC network path on Windows. Recently, we attempted an upgrade from xmlschema 2.5.1 to something newer, but found that we started getting errors at import time that appear to be due to some improper handling of UNC paths.

After some basic bisecting, I found that xmlschema 3.4.2 appears to have introduced a regression in UNC path handling (possibly similar to https://github.com/sissaschool/xmlschema/issues/268). Based on the errors, it appears that at some point UNC paths are being identified as non-absolute and concatenated with the root of the PWD in an attempt to make them absolute, thus resulting in a nonexistent/nonsensical path.

To Reproduce

NOTE: Using the Windows py launcher for simplicity.

  1. Install xmlschema>=3.4.2 to a UNC-based prefix:
    • py -3.11 -m pip install --prefix \\path\to\some\UNC\folder xmlschema==3.4.2
  2. Add the prefix's site-packages folder to PYTHONPATH
    • e.g. in PowerShell: $env:PYTHONPATH = "\\path\to\some\UNC\folder\Lib\site-packages"
  3. Run Python and attempt to import xmlschema
    • py -3.11 -c "import xmlschema"

The error

Traceback (most recent call last):
  File "C:\Python311\Lib\urllib\request.py", line 1505, in open_local_file
    stats = os.stat(localfile)
            ^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\path\\to\\some\\UNC\\folder\\Lib\\site-packages\\xmlschema\\schemas\\XML\\xml_minimal.xsd'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "\\path\to\some\UNC\folder\Lib\site-packages\xmlschema\__init__.py", line 22, in <module>
    from .dataobjects import DataElement, DataElementConverter, DataBindingConverter
  File "\\path\to\some\UNC\folder\Lib\site-packages\xmlschema\dataobjects.py", line 25, in <module>
    from . import validators
  File "\\path\to\some\UNC\folder\Lib\site-packages\xmlschema\validators\__init__.py", line 38, in <module>
    from .schemas import XMLSchemaMeta, XMLSchemaBase, XMLSchema, XMLSchema10, XMLSchema11
  File "\\path\to\some\UNC\folder\Lib\site-packages\xmlschema\validators\schemas.py", line 2262, in <module>
    class XMLSchema10(XMLSchemaBase):
  File "\\path\to\some\UNC\folder\Lib\site-packages\xmlschema\validators\schemas.py", line 136, in __new__
    meta_schema = meta_schema_class.create_meta_schema(meta_schema_file)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "\\path\to\some\UNC\folder\Lib\site-packages\xmlschema\validators\schemas.py", line 830, in create_meta_schema
    meta_schema.import_schema(namespace=ns, location=location)
  File "\\path\to\some\UNC\folder\Lib\site-packages\xmlschema\validators\schemas.py", line 1428, in import_schema
    schema = type(self)(
             ^^^^^^^^^^^
  File "\\path\to\some\UNC\folder\Lib\site-packages\xmlschema\validators\schemas.py", line 349, in __init__
    self.source = XMLResource(
                  ^^^^^^^^^^^^
  File "\\path\to\some\UNC\folder\Lib\site-packages\xmlschema\resources.py", line 258, in __init__
    self.parse(source, lazy)
  File "\\path\to\some\UNC\folder\Lib\site-packages\xmlschema\resources.py", line 613, in parse
    with urlopen(url, timeout=self._timeout) as resource:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 519, in open
    response = self._open(req, data)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 536, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 496, in _call_chain
    result = func(*args)
             ^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 1483, in file_open
    return self.open_local_file(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 1522, in open_local_file
    raise URLError(exp)
urllib.error.URLError: <urlopen error [WinError 3] The system cannot find the path specified: 'C:\\path\\to\\some\\UNC\\folder\\Lib\\site-packages\\xmlschema\\schemas\\XML\\xml_minimal.xsd'>

Notes

  • I have reproduced this issue with Python 3.10 and 3.11, but it does not occur with 3.12, so it seems possible that the code was refactored in 3.4.2 based on some newer behavior of a standard library module like pathlib, but not validated in older (supported) Python versions.
  • The error is reproducible with the latest version of xmlschema as well (at this time, 4.2.0), though the exact error changes. It still shows improper UNC path handling, but it relates to the loading of XSD_1.1/XMLSchema.xsd instead of XML/xml_minimal.xsd.
  • The drive letter that's being prepended to the mangled UNC path is the root of the PWD when the test command is run.
    • If you change your working directory to another drive before running the command, the path in the error will change accordingly.
    • Similarly, if you change your working directory to a UNC path, the mangled path will instead have the PWD's UNC root (e.g. \\\\path\\to\\path\\to\\some\\UNC\\folder\\Lib\\site-packages\\xmlschema\\schemas\\XML\\xml_minimal.xsd)

nrusch avatar Oct 28 '25 20:10 nrusch