Skip to content

Commit 2f55359

Browse files
committed
Fix _url_tools.url_quote for Python ≥ 3.14
Starting in Python 3.14 (python/cpython#127194), urllib.request.pathname2url returns URLs with a leading '//' for absolute input paths on POSIX. For compatibility, restore the traditional behavior. All callers prepend their own scheme including the '//'. This fixes these tests by resolving string-equality AssertionErrors: - tests/test_osfs.py::TestOSFS::test_complex_geturl - tests/test_subfs.py::TestOSFS::test_complex_geturl - tests/test_subfs.py::TestSubFS::test_complex_geturl - tests/test_tarfs.py::TestReadTarFS::test_geturl_for_fs - tests/test_tarfs.py::TestReadTarFSMem::test_geturl_for_fs - tests/test_tempfs.py::TestOSFS::test_complex_geturl - tests/test_tempfs.py::TestTempFS::test_complex_geturl - tests/test_zipfs.py::TestReadZipFS::test_geturl_for_fs - tests/test_zipfs.py::TestReadZipFSMem::test_geturl_for_fs
1 parent 77a8562 commit 2f55359

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

‎fs/_url_tools.py‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import typing
22

3+
import os.path
34
import platform
45
import re
56
import six
7+
import sys
68

79
if typing.TYPE_CHECKING:
810
from typing import Text
@@ -27,12 +29,21 @@ def url_quote(path_snippet):
2729
if six.PY2:
2830
path = path.encode("utf-8")
2931
path = six.moves.urllib.request.pathname2url(path)
30-
path_snippet = "{}:{}".format(drive_letter, path)
32+
path = "{}:{}".format(drive_letter, path)
3133
else:
3234
if six.PY2:
3335
path_snippet = path_snippet.encode("utf-8")
34-
path_snippet = six.moves.urllib.request.pathname2url(path_snippet)
35-
return path_snippet
36+
path = six.moves.urllib.request.pathname2url(path_snippet)
37+
if sys.version_info >= (3, 14):
38+
# For compatibility, restore the traditional behavior from before
39+
# https://github.com/python/cpython/pull/127194. All callers prepend
40+
# their own scheme including the '//'.
41+
_, root, _ = os.path.splitroot(path_snippet)
42+
if root:
43+
assert path.startswith("//")
44+
path = path[2:]
45+
46+
return path
3647

3748

3849
def _has_drive_letter(path_snippet):

0 commit comments

Comments
 (0)