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 @@ -1595,6 +1595,8 @@ def test_url2pathname_win(self):
1595
1595
# Localhost paths
1596
1596
self .assertEqual (fn ('//localhost/C:/path/to/file' ), 'C:\\ path\\ to\\ file' )
1597
1597
self .assertEqual (fn ('//localhost/C|/path/to/file' ), 'C:\\ path\\ to\\ file' )
1598
+ self .assertEqual (fn ('//localhost/path/to/file' ), '\\ path\\ to\\ file' )
1599
+ self .assertEqual (fn ('//localhost//server/path/to/file' ), '\\ \\ server\\ path\\ to\\ file' )
1598
1600
# Percent-encoded forward slashes are preserved for backwards compatibility
1599
1601
self .assertEqual (fn ('C:/foo%2fbar' ), 'C:\\ foo/bar' )
1600
1602
self .assertEqual (fn ('//server/share/foo%2fbar' ), '\\ \\ server\\ share\\ foo/bar' )
@@ -1613,7 +1615,7 @@ def test_url2pathname_posix(self):
1613
1615
self .assertEqual (fn ('//foo/bar' ), '//foo/bar' )
1614
1616
self .assertEqual (fn ('///foo/bar' ), '/foo/bar' )
1615
1617
self .assertEqual (fn ('////foo/bar' ), '//foo/bar' )
1616
- self .assertEqual (fn ('//localhost/foo/bar' ), '//localhost/ foo/bar' )
1618
+ self .assertEqual (fn ('//localhost/foo/bar' ), '/foo/bar' )
1617
1619
1618
1620
@unittest .skipUnless (os_helper .FS_NONASCII , 'need os_helper.FS_NONASCII' )
1619
1621
def test_url2pathname_nonascii (self ):
Original file line number Diff line number Diff line change @@ -1660,6 +1660,9 @@ def url2pathname(pathname):
1660
1660
# URL has an empty authority section, so the path begins on the
1661
1661
# third character.
1662
1662
pathname = pathname [2 :]
1663
+ elif pathname [:12 ] == '//localhost/' :
1664
+ # Skip past 'localhost' authority.
1665
+ pathname = pathname [11 :]
1663
1666
encoding = sys .getfilesystemencoding ()
1664
1667
errors = sys .getfilesystemencodeerrors ()
1665
1668
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