Skip to content

Commit

Permalink
Preserve percent-encoded forward slashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Oct 31, 2024
1 parent 8d313bf commit d3687bb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Lib/nturl2path.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def url2pathname(url):
# convert this to \\host\path\on\remote\host
# (notice halving of slashes at the start of the path)
url = url[2:]
return urllib.parse.unquote(url).replace('/', '\\')
return urllib.parse.unquote(url.replace('/', '\\'))
comp = url.split('|')
if len(comp) != 2 or comp[0][-1] not in string.ascii_letters:
error = 'Bad URL: ' + url
raise OSError(error)
drive = comp[0][-1].upper()
tail = urllib.parse.unquote(comp[1]).replace('/', '\\')
tail = urllib.parse.unquote(comp[1].replace('/', '\\'))
return drive + ':' + tail

def pathname2url(p):
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,9 @@ def test_url2pathname_win(self):
# Localhost paths
self.assertEqual(fn('//localhost/C:/path/to/file'), 'C:\\path\\to\\file')
self.assertEqual(fn('//localhost/C|/path/to/file'), 'C:\\path\\to\\file')
# Percent-encoded forward slashes are preserved for backwards compatibility
self.assertEqual(fn('C:/foo%2fbar'), 'C:\\foo/bar')
self.assertEqual(fn('//server/share/foo%2fbar'), '\\\\server\\share\\foo/bar')
# Round-tripping
paths = ['C:',
r'\\\C\test\\',
Expand Down

0 comments on commit d3687bb

Please sign in to comment.