Skip to content
Closed
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
Move as_uri() back
  • Loading branch information
barneygale committed Jun 23, 2023
commit 0ea24cc2e7355627ba97d64aa69bdb6bba56b669
45 changes: 21 additions & 24 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,26 @@ def as_posix(self):
def __repr__(self):
return "{}({!r})".format(self.__class__.__name__, self.as_posix())

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")

drive = self.drive
if len(drive) == 2 and drive[1] == ':':
# It's a path on a local drive => 'file:///c:/a/b'
prefix = 'file:///' + drive
path = self.as_posix()[2:]
elif drive:
# It's a path on a network drive => 'file://host/share/a/b'
prefix = 'file:'
path = self.as_posix()
else:
# It's a posix path => 'file:///etc/hosts'
prefix = 'file://'
path = str(self)
return prefix + urlquote_from_bytes(os.fsencode(path))

@property
def _str_normcase(self):
# String with normalized case, for hashing and equality checks
Expand Down Expand Up @@ -708,10 +728,7 @@ def match(self, path_pattern, *, case_sensitive=None):


class _PurePathExt(PurePath):
"""PurePath subclass that adds some non-lexical methods.

This class provides the methods __fspath__, __bytes__ and as_uri.
"""
"""PurePath subclass that provides __fspath__ and __bytes__ methods."""
__slots__ = ()

def __fspath__(self):
Expand All @@ -722,26 +739,6 @@ def __bytes__(self):
recommended to use under Unix."""
Comment thread
barneygale marked this conversation as resolved.
return os.fsencode(self)

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")

drive = self.drive
if len(drive) == 2 and drive[1] == ':':
# It's a path on a local drive => 'file:///c:/a/b'
prefix = 'file:///' + drive
path = self.as_posix()[2:]
elif drive:
# It's a path on a network drive => 'file://host/share/a/b'
prefix = 'file:'
path = self.as_posix()
else:
# It's a posix path => 'file:///etc/hosts'
prefix = 'file://'
path = str(self)
return prefix + urlquote_from_bytes(os.fsencode(path))


# Subclassing os.PathLike makes isinstance() checks slower,
# which in turn makes Path construction slower. Register instead!
Expand Down
14 changes: 7 additions & 7 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ def test_as_posix_common(self):
self.assertEqual(P(pathstr).as_posix(), pathstr)
# Other tests for as_posix() are in test_equivalences().

def test_as_uri_common(self):
P = self.cls
with self.assertRaises(ValueError):
P('a').as_uri()
with self.assertRaises(ValueError):
P().as_uri()

def test_repr_common(self):
for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'):
with self.subTest(pathstr=pathstr):
Expand Down Expand Up @@ -759,13 +766,6 @@ def test_bytes_common(self):
P = self.cls
self.assertEqual(bytes(P('a/b')), b'a' + sep + b'b')

def test_as_uri_common(self):
P = self.cls
with self.assertRaises(ValueError):
P('a').as_uri()
with self.assertRaises(ValueError):
P().as_uri()


class PurePosixPathTest(PurePathExtTest):
cls = pathlib.PurePosixPath
Expand Down