MCPcopy Index your code
hub / github.com/RustPython/RustPython / pathname2url

Function pathname2url

Lib/urllib/request.py:1698–1730  ·  view source on GitHub ↗

Convert the given local file system path to a file URL. The 'file:' scheme prefix is omitted unless *add_scheme* is set to true.

(pathname, *, add_scheme=False)

Source from the content-addressed store, hash-verified

1696
1697
1698def pathname2url(pathname, *, add_scheme=False):
1699 """Convert the given local file system path to a file URL.
1700
1701 The 'file:' scheme prefix is omitted unless *add_scheme*
1702 is set to true.
1703 """
1704 if os.name == 'nt':
1705 pathname = pathname.replace('\\', '/')
1706 encoding = sys.getfilesystemencoding()
1707 errors = sys.getfilesystemencodeerrors()
1708 scheme = 'file:' if add_scheme else ''
1709 drive, root, tail = os.path.splitroot(pathname)
1710 if drive:
1711 # First, clean up some special forms. We are going to sacrifice the
1712 # additional information anyway
1713 if drive[:4] == '//?/':
1714 drive = drive[4:]
1715 if drive[:4].upper() == 'UNC/':
1716 drive = '//' + drive[4:]
1717 if drive[1:] == ':':
1718 # DOS drive specified. Add three slashes to the start, producing
1719 # an authority section with a zero-length authority, and a path
1720 # section starting with a single slash.
1721 drive = '///' + drive
1722 drive = quote(drive, encoding=encoding, errors=errors, safe='/:')
1723 elif root:
1724 # Add explicitly empty authority to absolute path. If the path
1725 # starts with exactly one slash then this change is mostly
1726 # cosmetic, but if it begins with two or more slashes then this
1727 # avoids interpreting the path as a URL authority.
1728 root = '//' + root
1729 tail = quote(tail, encoding=encoding, errors=errors)
1730 return scheme + drive + root + tail
1731
1732
1733# Utility functions

Callers 4

as_uriMethod · 0.90
open_local_fileMethod · 0.70

Calls 3

quoteFunction · 0.90
replaceMethod · 0.45
upperMethod · 0.45