File tree 4 files changed +15
-5
lines changed
4 files changed +15
-5
lines changed Original file line number Diff line number Diff line change @@ -15,14 +15,17 @@ def url2pathname(url):
15
15
# become
16
16
# C:\foo\bar\spam.foo
17
17
import string , urllib .parse
18
+ if url [:3 ] == '///' :
19
+ # URL has an empty authority section, so the path begins on the third
20
+ # character.
21
+ url = url [2 :]
22
+ elif url [:12 ] == '//localhost/' :
23
+ # Skip past 'localhost' authority.
24
+ url = url [11 :]
18
25
# Windows itself uses ":" even in URLs.
19
26
url = url .replace (':' , '|' )
20
27
if not '|' in url :
21
28
# No drive specifier, just convert slashes
22
- if url [:3 ] == '///' :
23
- # URL has an empty authority section, so the path begins on the
24
- # third character.
25
- url = url [2 :]
26
29
# make sure not to convert quoted slashes :-)
27
30
return urllib .parse .unquote (url .replace ('/' , '\\ ' ))
28
31
comp = url .split ('|' )
Original file line number Diff line number Diff line change @@ -1604,6 +1604,8 @@ def test_url2pathname_win(self):
1604
1604
# Localhost paths
1605
1605
self .assertEqual (fn ('//localhost/C:/path/to/file' ), 'C:\\ path\\ to\\ file' )
1606
1606
self .assertEqual (fn ('//localhost/C|/path/to/file' ), 'C:\\ path\\ to\\ file' )
1607
+ self .assertEqual (fn ('//localhost/path/to/file' ), '\\ path\\ to\\ file' )
1608
+ self .assertEqual (fn ('//localhost//server/path/to/file' ), '\\ \\ server\\ path\\ to\\ file' )
1607
1609
# Percent-encoded forward slashes are preserved for backwards compatibility
1608
1610
self .assertEqual (fn ('C:/foo%2fbar' ), 'C:\\ foo/bar' )
1609
1611
self .assertEqual (fn ('//server/share/foo%2fbar' ), '\\ \\ server\\ share\\ foo/bar' )
@@ -1622,7 +1624,7 @@ def test_url2pathname_posix(self):
1622
1624
self .assertEqual (fn ('//foo/bar' ), '//foo/bar' )
1623
1625
self .assertEqual (fn ('///foo/bar' ), '/foo/bar' )
1624
1626
self .assertEqual (fn ('////foo/bar' ), '//foo/bar' )
1625
- self .assertEqual (fn ('//localhost/foo/bar' ), '//localhost/ foo/bar' )
1627
+ self .assertEqual (fn ('//localhost/foo/bar' ), '/foo/bar' )
1626
1628
1627
1629
@unittest .skipUnless (os_helper .FS_NONASCII , 'need os_helper.FS_NONASCII' )
1628
1630
def test_url2pathname_nonascii (self ):
Original file line number Diff line number Diff line change @@ -1685,6 +1685,9 @@ def url2pathname(pathname):
1685
1685
# URL has an empty authority section, so the path begins on the
1686
1686
# third character.
1687
1687
pathname = pathname [2 :]
1688
+ elif pathname [:12 ] == '//localhost/' :
1689
+ # Skip past 'localhost' authority.
1690
+ pathname = pathname [11 :]
1688
1691
encoding = sys .getfilesystemencoding ()
1689
1692
errors = sys .getfilesystemencodeerrors ()
1690
1693
return unquote (pathname , encoding = encoding , errors = errors )
Original file line number Diff line number Diff line change
1
+ Fix issue where :func: `urllib.request.url2pathname ` failed to discard any
2
+ 'localhost' authority present in the URL.
You can’t perform that action at this time.
0 commit comments