mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
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:
parent
7577307ebd
commit
bf224bd7ce
3 changed files with 14 additions and 6 deletions
|
@ -44,20 +44,21 @@ def pathname2url(p):
|
|||
import urllib.parse
|
||||
# First, clean up some special forms. We are going to sacrifice
|
||||
# the additional information anyway
|
||||
if p[:4] == '\\\\?\\':
|
||||
p = p.replace('\\', '/')
|
||||
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('\\', '/'))
|
||||
# No DOS drive specified, just quote the pathname
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue