Skip to content

Commit 68c151c

Browse files
barneygalepicnixz
authored andcommitted
pythonGH-126205: Fix conversion of UNC paths to file URIs (python#126208)
File URIs for Windows UNC paths should begin with two slashes, not four.
1 parent 4d78977 commit 68c151c

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

Lib/nturl2path.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,11 @@ def pathname2url(p):
5555
if p[:4] == '\\\\?\\':
5656
p = p[4:]
5757
if p[:4].upper() == 'UNC\\':
58-
p = '\\' + p[4:]
58+
p = '\\\\' + p[4:]
5959
elif p[1:2] != ':':
6060
raise OSError('Bad path: ' + p)
6161
if not ':' in p:
6262
# No drive specifier, just convert slashes and quote the name
63-
if p[:2] == '\\\\':
64-
# path is something like \\host\path\on\remote\host
65-
# convert this to ////host/path/on/remote/host
66-
# (notice doubling of slashes at the start of the path)
67-
p = '\\\\' + p
6863
components = p.split('\\')
6964
return urllib.parse.quote('/'.join(components))
7065
comp = p.split(':', maxsplit=2)

Lib/test/test_urllib.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ def test_pathname2url_win(self):
15241524
# Test special prefixes are correctly handled in pathname2url()
15251525
fn = urllib.request.pathname2url
15261526
self.assertEqual(fn('\\\\?\\C:\\dir'), '///C:/dir')
1527-
self.assertEqual(fn('\\\\?\\unc\\server\\share\\dir'), '/server/share/dir')
1527+
self.assertEqual(fn('\\\\?\\unc\\server\\share\\dir'), '//server/share/dir')
15281528
self.assertEqual(fn("C:"), '///C:')
15291529
self.assertEqual(fn("C:\\"), '///C:')
15301530
self.assertEqual(fn('C:\\a\\b.c'), '///C:/a/b.c')
@@ -1535,14 +1535,14 @@ def test_pathname2url_win(self):
15351535
self.assertRaises(IOError, fn, "XX:\\")
15361536
# No drive letter
15371537
self.assertEqual(fn("\\folder\\test\\"), '/folder/test/')
1538-
self.assertEqual(fn("\\\\folder\\test\\"), '////folder/test/')
1539-
self.assertEqual(fn("\\\\\\folder\\test\\"), '/////folder/test/')
1540-
self.assertEqual(fn('\\\\some\\share\\'), '////some/share/')
1541-
self.assertEqual(fn('\\\\some\\share\\a\\b.c'), '////some/share/a/b.c')
1542-
self.assertEqual(fn('\\\\some\\share\\a\\b%#c\xe9'), '////some/share/a/b%25%23c%C3%A9')
1538+
self.assertEqual(fn("\\\\folder\\test\\"), '//folder/test/')
1539+
self.assertEqual(fn("\\\\\\folder\\test\\"), '///folder/test/')
1540+
self.assertEqual(fn('\\\\some\\share\\'), '//some/share/')
1541+
self.assertEqual(fn('\\\\some\\share\\a\\b.c'), '//some/share/a/b.c')
1542+
self.assertEqual(fn('\\\\some\\share\\a\\b%#c\xe9'), '//some/share/a/b%25%23c%C3%A9')
15431543
# Round-tripping
15441544
urls = ['///C:',
1545-
'/////folder/test/',
1545+
'///folder/test/',
15461546
'///C:/foo/bar/spam.foo']
15471547
for url in urls:
15481548
self.assertEqual(fn(urllib.request.url2pathname(url)), url)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix issue where :func:`urllib.request.pathname2url` generated URLs beginning
2+
with four slashes (rather than two) when given a Windows UNC path.

0 commit comments

Comments
 (0)