Update nturl2path from Python 3.11

This commit is contained in:
DimitrisJim
2023-06-14 01:07:12 +03:00
parent 3c8fb93ab4
commit 1df0a44958

17
Lib/nturl2path.py vendored
View File

@@ -1,4 +1,9 @@
"""Convert a NT pathname to a file URL and vice versa."""
"""Convert a NT pathname to a file URL and vice versa.
This module only exists to provide OS-specific code
for urllib.requests, thus do not use directly.
"""
# Testing is done through test_urllib.
def url2pathname(url):
"""OS-specific conversion from a relative URL of the 'file' scheme
@@ -45,6 +50,14 @@ def pathname2url(p):
# becomes
# ///C:/foo/bar/spam.foo
import urllib.parse
# First, clean up some special forms. We are going to sacrifice
# the additional information anyway
if p[:4] == '\\\\?\\':
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
if p[:2] == '\\\\':
@@ -54,7 +67,7 @@ def pathname2url(p):
p = '\\\\' + p
components = p.split('\\')
return urllib.parse.quote('/'.join(components))
comp = p.split(':')
comp = p.split(':', maxsplit=2)
if len(comp) != 2 or len(comp[0]) > 1:
error = 'Bad path: ' + p
raise OSError(error)