GH-120423: pathname2url(): handle forward slashes in Windows paths (#126593)

Adjust `urllib.request.pathname2url()` so that forward slashes in Windows
paths are handled identically to backward slashes.
This commit is contained in:
Barney Gale 2024-11-12 19:52:30 +00:00 committed by GitHub
parent 7577307ebd
commit bf224bd7ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 6 deletions

View file

@ -44,20 +44,21 @@ def pathname2url(p):
import urllib.parse import urllib.parse
# First, clean up some special forms. We are going to sacrifice # First, clean up some special forms. We are going to sacrifice
# the additional information anyway # the additional information anyway
if p[:4] == '\\\\?\\': p = p.replace('\\', '/')
if p[:4] == '//?/':
p = p[4:] p = p[4:]
if p[:4].upper() == 'UNC\\': if p[:4].upper() == 'UNC/':
p = '\\\\' + p[4:] p = '//' + p[4:]
elif p[1:2] != ':': elif p[1:2] != ':':
raise OSError('Bad path: ' + p) raise OSError('Bad path: ' + p)
if not ':' in p: if not ':' in p:
# No drive specifier, just convert slashes and quote the name # No DOS drive specified, just quote the pathname
return urllib.parse.quote(p.replace('\\', '/')) return urllib.parse.quote(p)
comp = p.split(':', maxsplit=2) comp = p.split(':', maxsplit=2)
if len(comp) != 2 or len(comp[0]) > 1: if len(comp) != 2 or len(comp[0]) > 1:
error = 'Bad path: ' + p error = 'Bad path: ' + p
raise OSError(error) raise OSError(error)
drive = urllib.parse.quote(comp[0].upper()) drive = urllib.parse.quote(comp[0].upper())
tail = urllib.parse.quote(comp[1].replace('\\', '/')) tail = urllib.parse.quote(comp[1])
return '///' + drive + ':' + tail return '///' + drive + ':' + tail

View file

@ -1542,6 +1542,11 @@ class Pathname_Tests(unittest.TestCase):
self.assertEqual(fn('\\\\some\\share\\'), '//some/share/') 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'), '//some/share/a/b.c')
self.assertEqual(fn('\\\\some\\share\\a\\b%#c\xe9'), '//some/share/a/b%25%23c%C3%A9') 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 # Round-tripping
urls = ['///C:', urls = ['///C:',
'///folder/test/', '///folder/test/',

View file

@ -0,0 +1,2 @@
Fix issue where :func:`urllib.request.pathname2url` mishandled Windows paths
with embedded forward slashes.