Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-44136: pathlib: merge _Flavour.make_uri() into PurePath.as_uri() #30320

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
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
36 changes: 17 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,19 @@ class PureWindowsPath(PurePath):
_flavour = _windows_flavour
__slots__ = ()

def as_uri(self):
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