From e3c29f71c73de7b98e39b07edc4e8433c0847e80 Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 1 Jan 2022 05:11:57 +0000 Subject: [PATCH 1/2] bpo-44136: pathlib: merge `_Flavour.make_uri()` into `PurePath.as_uri()` Removes a bit of indirection. --- Lib/pathlib.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index f1a33178e2958b..6058e283f1a322 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -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 = '/' @@ -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() @@ -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): @@ -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 From 94c41e550bdadbedb4ec7ee63e48bea7c2e9efe3 Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 5 Feb 2022 23:02:04 +0000 Subject: [PATCH 2/2] Delete duplicated docstring --- Lib/pathlib.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 6058e283f1a322..fdc4520fdb7e1b 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -922,7 +922,6 @@ class PureWindowsPath(PurePath): __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.