summaryrefslogtreecommitdiffstats
path: root/Lib/nturl2path.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-11-12 19:52:30 (GMT)
committerGitHub <noreply@github.com>2024-11-12 19:52:30 (GMT)
commitbf224bd7cef5d24eaff35945ebe7ffe14df7710f (patch)
tree0fedcebce61343cc6061ad3f0ed53abea8aafc6c /Lib/nturl2path.py
parent7577307ebdaeef6702b639e22a896080e81aae4e (diff)
downloadcpython-bf224bd7cef5d24eaff35945ebe7ffe14df7710f.zip
cpython-bf224bd7cef5d24eaff35945ebe7ffe14df7710f.tar.gz
cpython-bf224bd7cef5d24eaff35945ebe7ffe14df7710f.tar.bz2
GH-120423: `pathname2url()`: handle forward slashes in Windows paths (#126593)
Adjust `urllib.request.pathname2url()` so that forward slashes in Windows paths are handled identically to backward slashes.
Diffstat (limited to 'Lib/nturl2path.py')
-rw-r--r--Lib/nturl2path.py13
1 files changed, 7 insertions, 6 deletions
diff --git a/Lib/nturl2path.py b/Lib/nturl2path.py
index 2f9fec7..9ecabff 100644
--- a/Lib/nturl2path.py
+++ b/Lib/nturl2path.py
@@ -44,20 +44,21 @@ def pathname2url(p):
import urllib.parse
# First, clean up some special forms. We are going to sacrifice
# the additional information anyway
- if p[:4] == '\\\\?\\':
+ p = p.replace('\\', '/')
+ if p[:4] == '//?/':
p = p[4:]
- if p[:4].upper() == 'UNC\\':
- p = '\\\\' + p[4:]
+ if p[:4].upper() == 'UNC/':
+ p = '//' + p[4:]
elif p[1:2] != ':':
raise OSError('Bad path: ' + p)
if not ':' in p:
- # No drive specifier, just convert slashes and quote the name
- return urllib.parse.quote(p.replace('\\', '/'))
+ # No DOS drive specified, just quote the pathname
+ return urllib.parse.quote(p)
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())
- tail = urllib.parse.quote(comp[1].replace('\\', '/'))
+ tail = urllib.parse.quote(comp[1])
return '///' + drive + ':' + tail