Skip to content

Commit

Permalink
pythonGH-120423: pathname2url(): handle forward slashes in Windows …
Browse files Browse the repository at this point in the history
…paths

Adjust `urllib.request.pathname2url()` so that forward slashes in Windows
paths are handled identically to backward slashes.
  • Loading branch information
barneygale committed Nov 8, 2024
1 parent 54c63a3 commit 35ce106
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
11 changes: 6 additions & 5 deletions Lib/nturl2path.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,23 @@ def pathname2url(p):
# becomes
# ///C:/foo/bar/spam.foo
import urllib.parse
p = p.replace('\\', '/')
# First, clean up some special forms. We are going to sacrifice
# the additional information anyway
if p[:4] == '\\\\?\\':
if p[:4] == '//?/':
p = p[4:]
if p[:4].upper() == 'UNC\\':
p = '\\\\' + p[4:]
if p[:4].upper() == 'UNC/':
p = '//' + p[4:]
elif p[1:2] != ':':
raise OSError('Bad path: ' + p)
if not ':' in p:
# No drive specifier, just convert slashes and quote the name
return urllib.parse.quote(p.replace('\\', '/'))
return urllib.parse.quote(p)
comp = p.split(':', maxsplit=2)
if len(comp) != 2 or len(comp[0]) > 1:
error = 'Bad path: ' + p
raise OSError(error)

drive = urllib.parse.quote(comp[0].upper())
tail = urllib.parse.quote(comp[1].replace('\\', '/'))
tail = urllib.parse.quote(comp[1])
return '///' + drive + ':' + tail
5 changes: 5 additions & 0 deletions Lib/test/test_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,11 @@ def test_pathname2url_win(self):
self.assertEqual(fn('\\\\some\\share\\'), '//some/share/')
self.assertEqual(fn('\\\\some\\share\\a\\b.c'), '//some/share/a/b.c')
self.assertEqual(fn('\\\\some\\share\\a\\b%#c\xe9'), '//some/share/a/b%25%23c%C3%A9')
# Alternate path separator
self.assertEqual(fn('C:/a/b.c'), '///C:/a/b.c')
self.assertEqual(fn('//some/share/a/b.c'), '//some/share/a/b.c')
self.assertEqual(fn('//?/C:/dir'), '///C:/dir')
self.assertEqual(fn('//?/unc/server/share/dir'), '//server/share/dir')
# Round-tripping
urls = ['///C:',
'///folder/test/',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix issue where :func:`urllib.request.pathname2url` mishandled Windows paths
with embedded forward slashes.

0 comments on commit 35ce106

Please sign in to comment.