From 8d0b52c94acd5664856ad063e64084558f559633 Mon Sep 17 00:00:00 2001 From: Sean Gillies Date: Thu, 29 Feb 2024 17:18:27 -0700 Subject: [PATCH] Address weird Windows path parsing --- fiona/_path.py | 14 ++++++++++---- tests/test__path.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 tests/test__path.py diff --git a/fiona/_path.py b/fiona/_path.py index 045a62a0..b3cc744a 100644 --- a/fiona/_path.py +++ b/fiona/_path.py @@ -65,7 +65,13 @@ class _ParsedPath(_Path): @classmethod def from_uri(cls, uri): parts = urlparse(uri) - path = pathlib.Path(parts.path).as_posix() if parts.path else parts.path + if sys.platform == "win32" and re.match(r"^[a-zA-Z]\:", parts.netloc) and not parts.path: + parsed_path = parts.netloc + parsed_netloc = None + else: + parsed_path = parts.path + parsed_netloc = parts.netloc + path = pathlib.Path(parsed_path).as_posix() if parsed_path else parsed_path scheme = parts.scheme or None if parts.query: @@ -78,11 +84,11 @@ def from_uri(cls, uri): else: archive = None - if parts.scheme and parts.netloc: + if scheme and parsed_netloc: if archive: - archive = parts.netloc + archive + archive = parsed_netloc + archive else: - path = parts.netloc + path + path = parsed_netloc + path return _ParsedPath(path, archive, scheme) diff --git a/tests/test__path.py b/tests/test__path.py new file mode 100644 index 00000000..bf610d00 --- /dev/null +++ b/tests/test__path.py @@ -0,0 +1,13 @@ +"""_path tests.""" + +import sys + +from fiona._path import _parse_path, _vsi_path + + +def test_parse_zip_windows(monkeypatch): + """Parse a zip+ Windows path.""" + monkeypatch.setattr(sys, "platform", "win32") + path = _parse_path("zip://D:\\a\\Fiona\\Fiona\\tests\\data\\coutwildrnp.zip!coutwildrnp.shp") + vsi_path = _vsi_path(path) + assert vsi_path == "/vsizip/D:\\a\\Fiona\\Fiona\\tests\\data\\coutwildrnp.zip/coutwildrnp.shp"