Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-44136: pathlib: merge _Flavour.make_uri() into PurePath.as_uri()
Removes a bit of indirection.
  • Loading branch information
barneygale committed Jan 1, 2022
commit e3c29f71c73de7b98e39b07edc4e8433c0847e80
37 changes: 18 additions & 19 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,6 @@ def is_reserved(self, parts):
name = parts[-1].partition('.')[0].partition(':')[0].rstrip(' ')
return name.upper() in self.reserved_names

def make_uri(self, path):
# Under Windows, file URIs use the UTF-8 encoding.
drive = path.drive
if len(drive) == 2 and drive[1] == ':':
# It's a path on a local drive => 'file:///c:/a/b'
rest = path.as_posix()[2:].lstrip('/')
return 'file:///%s/%s' % (
drive, urlquote_from_bytes(rest.encode('utf-8')))
else:
# It's a path on a network drive => 'file://host/share/a/b'
return 'file:' + urlquote_from_bytes(path.as_posix().encode('utf-8'))


class _PosixFlavour(_Flavour):
sep = '/'
Expand Down Expand Up @@ -263,12 +251,6 @@ def compile_pattern(self, pattern):
def is_reserved(self, parts):
return False

def make_uri(self, path):
# We represent the path using the local filesystem encoding,
# for portability to other applications.
bpath = bytes(path)
return 'file://' + urlquote_from_bytes(bpath)


_windows_flavour = _WindowsFlavour()
_posix_flavour = _PosixFlavour()
Expand Down Expand Up @@ -647,7 +629,10 @@ def as_uri(self):
"""Return the path as a 'file' URI."""
if not self.is_absolute():
raise ValueError("relative path can't be expressed as a file URI")
return self._flavour.make_uri(self)
# We represent the path using the local filesystem encoding,
# for portability to other applications.
bpath = bytes(self)
return 'file://' + urlquote_from_bytes(bpath)

@property
def _cparts(self):
Expand Down Expand Up @@ -936,6 +921,20 @@ class PureWindowsPath(PurePath):
_flavour = _windows_flavour
__slots__ = ()

def as_uri(self):
"""Return the path as a 'file' URI."""
if not self.is_absolute():
raise ValueError("relative path can't be expressed as a file URI")
# Under Windows, file URIs use the UTF-8 encoding.
drive = self.drive
if len(drive) == 2 and drive[1] == ':':
# It's a path on a local drive => 'file:///c:/a/b'
rest = self.as_posix()[2:].lstrip('/')
return 'file:///%s/%s' % (
drive, urlquote_from_bytes(rest.encode('utf-8')))
else:
# It's a path on a network drive => 'file://host/share/a/b'
return 'file:' + urlquote_from_bytes(self.as_posix().encode('utf-8'))

# Filesystem-accessing classes

Expand Down