Skip to content

Commit bf224bd

Browse files
authored
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.
1 parent 7577307 commit bf224bd

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

Lib/nturl2path.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,21 @@ def pathname2url(p):
4444
import urllib.parse
4545
# First, clean up some special forms. We are going to sacrifice
4646
# the additional information anyway
47-
if p[:4] == '\\\\?\\':
47+
p = p.replace('\\', '/')
48+
if p[:4] == '//?/':
4849
p = p[4:]
49-
if p[:4].upper() == 'UNC\\':
50-
p = '\\\\' + p[4:]
50+
if p[:4].upper() == 'UNC/':
51+
p = '//' + p[4:]
5152
elif p[1:2] != ':':
5253
raise OSError('Bad path: ' + p)
5354
if not ':' in p:
54-
# No drive specifier, just convert slashes and quote the name
55-
return urllib.parse.quote(p.replace('\\', '/'))
55+
# No DOS drive specified, just quote the pathname
56+
return urllib.parse.quote(p)
5657
comp = p.split(':', maxsplit=2)
5758
if len(comp) != 2 or len(comp[0]) > 1:
5859
error = 'Bad path: ' + p
5960
raise OSError(error)
6061

6162
drive = urllib.parse.quote(comp[0].upper())
62-
tail = urllib.parse.quote(comp[1].replace('\\', '/'))
63+
tail = urllib.parse.quote(comp[1])
6364
return '///' + drive + ':' + tail

Lib/test/test_urllib.py

+5
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,11 @@ def test_pathname2url_win(self):
15421542
self.assertEqual(fn('\\\\some\\share\\'), '//some/share/')
15431543
self.assertEqual(fn('\\\\some\\share\\a\\b.c'), '//some/share/a/b.c')
15441544
self.assertEqual(fn('\\\\some\\share\\a\\b%#c\xe9'), '//some/share/a/b%25%23c%C3%A9')
1545+
# Alternate path separator
1546+
self.assertEqual(fn('C:/a/b.c'), '///C:/a/b.c')
1547+
self.assertEqual(fn('//some/share/a/b.c'), '//some/share/a/b.c')
1548+
self.assertEqual(fn('//?/C:/dir'), '///C:/dir')
1549+
self.assertEqual(fn('//?/unc/server/share/dir'), '//server/share/dir')
15451550
# Round-tripping
15461551
urls = ['///C:',
15471552
'///folder/test/',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix issue where :func:`urllib.request.pathname2url` mishandled Windows paths
2+
with embedded forward slashes.

0 commit comments

Comments
 (0)