url = url.replace(':', '|')
if not '|' in url:
# No drive specifier, just convert slashes
- if url[:4] == '////':
- # path is something like ////host/path/on/remote/host
- # convert this to \\host\path\on\remote\host
- # (notice halving of slashes at the start of the path)
+ if url[:3] == '///':
+ # URL has an empty authority section, so the path begins on the
+ # third character.
url = url[2:]
# make sure not to convert quoted slashes :-)
return urllib.parse.unquote(url.replace('/', '\\'))
self.assertEqual(fn('//?/unc/server/share/dir'), '//server/share/dir')
# Round-tripping
urls = ['///C:',
- '///folder/test/',
+ '/folder/test/',
'///C:/foo/bar/spam.foo']
for url in urls:
self.assertEqual(fn(urllib.request.url2pathname(url)), url)
self.assertEqual(fn('/C|//'), 'C:\\\\')
self.assertEqual(fn('///C|/path'), 'C:\\path')
# No DOS drive
- self.assertEqual(fn("///C/test/"), '\\\\\\C\\test\\')
+ self.assertEqual(fn("///C/test/"), '\\C\\test\\')
self.assertEqual(fn("////C/test/"), '\\\\C\\test\\')
# DOS drive paths
self.assertEqual(fn('C:/path/to/file'), 'C:\\path\\to\\file')
self.assertEqual(fn('//server/share/foo%2fbar'), '\\\\server\\share\\foo/bar')
# Round-tripping
paths = ['C:',
- r'\\\C\test\\',
+ r'\C\test\\',
r'C:\foo\bar\spam.foo']
for path in paths:
self.assertEqual(fn(urllib.request.pathname2url(path)), path)
fn = urllib.request.url2pathname
self.assertEqual(fn('/foo/bar'), '/foo/bar')
self.assertEqual(fn('//foo/bar'), '//foo/bar')
- self.assertEqual(fn('///foo/bar'), '///foo/bar')
- self.assertEqual(fn('////foo/bar'), '////foo/bar')
+ self.assertEqual(fn('///foo/bar'), '/foo/bar')
+ self.assertEqual(fn('////foo/bar'), '//foo/bar')
self.assertEqual(fn('//localhost/foo/bar'), '//localhost/foo/bar')
class Utility_Tests(unittest.TestCase):
def url2pathname(pathname):
"""OS-specific conversion from a relative URL of the 'file' scheme
to a file system path; not recommended for general use."""
+ if pathname[:3] == '///':
+ # URL has an empty authority section, so the path begins on the
+ # third character.
+ pathname = pathname[2:]
return unquote(pathname)
def pathname2url(pathname):