MCPcopy Index your code
hub / github.com/python/cpython / pathname2url

Function pathname2url

Lib/nturl2path.py:45–74  ·  view source on GitHub ↗

OS-specific conversion from a file system path to a relative URL of the 'file' scheme; not recommended for general use.

(p)

Source from the content-addressed store, hash-verified

43 return urllib.parse.unquote(url.replace('/', '\\'))
44
45def pathname2url(p):
46 """OS-specific conversion from a file system path to a relative URL
47 of the 'file' scheme; not recommended for general use."""
48 # e.g.
49 # C:\foo\bar\spam.foo
50 # becomes
51 # ///C:/foo/bar/spam.foo
52 import ntpath
53 import urllib.parse
54 # First, clean up some special forms. We are going to sacrifice
55 # the additional information anyway
56 p = p.replace('\\', '/')
57 if p[:4] == '//?/':
58 p = p[4:]
59 if p[:4].upper() == 'UNC/':
60 p = '//' + p[4:]
61 drive, root, tail = ntpath.splitroot(p)
62 if drive:
63 if drive[1:] == ':':
64 # DOS drive specified. Add three slashes to the start, producing
65 # an authority section with a zero-length authority, and a path
66 # section starting with a single slash.
67 drive = f'///{drive}'
68 drive = urllib.parse.quote(drive, safe='/:')
69 elif root:
70 # Add explicitly empty authority to path beginning with one slash.
71 root = f'//{root}'
72
73 tail = urllib.parse.quote(tail)
74 return drive + root + tail

Callers

nothing calls this directly

Calls 3

quoteMethod · 0.80
replaceMethod · 0.45
upperMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…