summaryrefslogtreecommitdiffstats
path: root/Lib/nturl2path.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-11-08 16:47:51 (GMT)
committerGitHub <noreply@github.com>2024-11-08 16:47:51 (GMT)
commit54c63a32d06cb5f07a66245c375eac7d7efb964a (patch)
treeec77472893e2d8bb0b900d44d8e9fffcb26ccede /Lib/nturl2path.py
parentfa4092259763ffad45a5bb9ef55f515dc6a69ad2 (diff)
downloadcpython-54c63a32d06cb5f07a66245c375eac7d7efb964a.zip
cpython-54c63a32d06cb5f07a66245c375eac7d7efb964a.tar.gz
cpython-54c63a32d06cb5f07a66245c375eac7d7efb964a.tar.bz2
GH-126212: Fix removal of slashes in file URIs on Windows (#126214)
Adjust `urllib.request.pathname2url()` and `url2pathname()` so that they don't remove slashes from Windows DOS drive paths and URLs. There was no basis for this behaviour, and it conflicts with how UNC and POSIX paths are handled.
Diffstat (limited to 'Lib/nturl2path.py')
-rw-r--r--Lib/nturl2path.py25
1 files changed, 6 insertions, 19 deletions
diff --git a/Lib/nturl2path.py b/Lib/nturl2path.py
index 6453f20..2f9fec7 100644
--- a/Lib/nturl2path.py
+++ b/Lib/nturl2path.py
@@ -24,23 +24,15 @@ def url2pathname(url):
# convert this to \\host\path\on\remote\host
# (notice halving of slashes at the start of the path)
url = url[2:]
- components = url.split('/')
# make sure not to convert quoted slashes :-)
- return urllib.parse.unquote('\\'.join(components))
+ return urllib.parse.unquote(url.replace('/', '\\'))
comp = url.split('|')
if len(comp) != 2 or comp[0][-1] not in string.ascii_letters:
error = 'Bad URL: ' + url
raise OSError(error)
drive = comp[0][-1].upper()
- components = comp[1].split('/')
- path = drive + ':'
- for comp in components:
- if comp:
- path = path + '\\' + urllib.parse.unquote(comp)
- # Issue #11474 - handing url such as |c/|
- if path.endswith(':') and url.endswith('/'):
- path += '\\'
- return path
+ tail = urllib.parse.unquote(comp[1].replace('/', '\\'))
+ return drive + ':' + tail
def pathname2url(p):
"""OS-specific conversion from a file system path to a relative URL
@@ -60,17 +52,12 @@ def pathname2url(p):
raise OSError('Bad path: ' + p)
if not ':' in p:
# No drive specifier, just convert slashes and quote the name
- components = p.split('\\')
- return urllib.parse.quote('/'.join(components))
+ return urllib.parse.quote(p.replace('\\', '/'))
comp = p.split(':', maxsplit=2)
if len(comp) != 2 or len(comp[0]) > 1:
error = 'Bad path: ' + p
raise OSError(error)
drive = urllib.parse.quote(comp[0].upper())
- components = comp[1].split('\\')
- path = '///' + drive + ':'
- for comp in components:
- if comp:
- path = path + '/' + urllib.parse.quote(comp)
- return path
+ tail = urllib.parse.quote(comp[1].replace('\\', '/'))
+ return '///' + drive + ':' + tail