Skip to content

Commit 90e597d

Browse files
committed
Merge nturl2path implementation into urllib.request
1 parent 85a84fc commit 90e597d

File tree

1 file changed

+57
-29
lines changed

1 file changed

+57
-29
lines changed

Lib/urllib/request.py

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,35 +1647,63 @@ def data_open(self, req):
16471647
# Code move from the old urllib module
16481648

16491649
# Helper for non-unix systems
1650-
if os.name == 'nt':
1651-
from nturl2path import url2pathname, pathname2url
1652-
else:
1653-
def url2pathname(pathname):
1654-
"""OS-specific conversion from a relative URL of the 'file' scheme
1655-
to a file system path; not recommended for general use."""
1656-
if pathname[:3] == '///':
1657-
# URL has an empty authority section, so the path begins on the
1658-
# third character.
1659-
pathname = pathname[2:]
1660-
elif pathname[:12] == '//localhost/':
1661-
# Skip past 'localhost' authority.
1662-
pathname = pathname[11:]
1663-
encoding = sys.getfilesystemencoding()
1664-
errors = sys.getfilesystemencodeerrors()
1665-
return unquote(pathname, encoding=encoding, errors=errors)
1666-
1667-
def pathname2url(pathname):
1668-
"""OS-specific conversion from a file system path to a relative URL
1669-
of the 'file' scheme; not recommended for general use."""
1670-
if pathname[:1] == '/':
1671-
# Add explicitly empty authority to absolute path. If the path
1672-
# starts with exactly one slash then this change is mostly
1673-
# cosmetic, but if it begins with two or more slashes then this
1674-
# avoids interpreting the path as a URL authority.
1675-
pathname = '//' + pathname
1676-
encoding = sys.getfilesystemencoding()
1677-
errors = sys.getfilesystemencodeerrors()
1678-
return quote(pathname, encoding=encoding, errors=errors)
1650+
def url2pathname(url):
1651+
"""OS-specific conversion from a relative URL of the 'file' scheme
1652+
to a file system path; not recommended for general use."""
1653+
if url[:3] == '///':
1654+
# Empty authority section, so the path begins on the third character.
1655+
url = url[2:]
1656+
elif url[:12] == '//localhost/':
1657+
# Skip past 'localhost' authority.
1658+
url = url[11:]
1659+
1660+
if os.name == 'nt':
1661+
if url[:3] == '///':
1662+
# Skip past extra slash before UNC drive in URL path.
1663+
url = url[1:]
1664+
else:
1665+
if url[:1] == '/' and url[2:3] in (':', '|'):
1666+
# Skip past extra slash before DOS drive in URL path.
1667+
url = url[1:]
1668+
if url[1:2] == '|':
1669+
# Older URLs use a pipe after a drive letter
1670+
url = url[:1] + ':' + url[2:]
1671+
url = url.replace('/', '\\')
1672+
encoding = sys.getfilesystemencoding()
1673+
errors = sys.getfilesystemencodeerrors()
1674+
return unquote(url, encoding=encoding, errors=errors)
1675+
1676+
1677+
def pathname2url(pathname):
1678+
"""OS-specific conversion from a file system path to a relative URL
1679+
of the 'file' scheme; not recommended for general use."""
1680+
if os.name == 'nt':
1681+
pathname = pathname.replace('\\', '/')
1682+
encoding = sys.getfilesystemencoding()
1683+
errors = sys.getfilesystemencodeerrors()
1684+
prefix = ''
1685+
drive, root, tail = os.path.splitroot(pathname)
1686+
if drive:
1687+
# First, clean up some special forms. We are going to sacrifice the
1688+
# additional information anyway
1689+
if drive[:4] == '//?/':
1690+
drive = drive[4:]
1691+
if drive[:4].upper() == 'UNC/':
1692+
drive = '//' + drive[4:]
1693+
if drive[1:] == ':':
1694+
# DOS drive specified. Add three slashes to the start, producing
1695+
# an authority section with a zero-length authority, and a path
1696+
# section starting with a single slash.
1697+
prefix += '///'
1698+
drive = quote(drive, encoding=encoding, errors=errors, safe='/:')
1699+
elif root:
1700+
# Add explicitly empty authority to absolute path. If the path
1701+
# starts with exactly one slash then this change is mostly
1702+
# cosmetic, but if it begins with two or more slashes then this
1703+
# avoids interpreting the path as a URL authority.
1704+
prefix += '//'
1705+
tail = quote(tail, encoding=encoding, errors=errors)
1706+
return prefix + drive + root + tail
16791707

16801708

16811709
# Utility functions

0 commit comments

Comments
 (0)