From d3687bb233316f66be8b4d83bb2b5d415be6792a Mon Sep 17 00:00:00 2001 From: barneygale Date: Thu, 31 Oct 2024 23:21:55 +0000 Subject: [PATCH] Preserve percent-encoded forward slashes. --- Lib/nturl2path.py | 4 ++-- Lib/test/test_urllib.py | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Lib/nturl2path.py b/Lib/nturl2path.py index b373105167d74d..054525fa7d6442 100644 --- a/Lib/nturl2path.py +++ b/Lib/nturl2path.py @@ -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): diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 57227265fb6b25..28369b21db06d4 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -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\\',