mirror of
https://github.com/python/cpython.git
synced 2025-10-17 20:28:43 +00:00
GH-123599: Deprecate duplicate pathname2url()
implementation (#127380)
Call `urllib.request.pathname2url()` from `pathlib.Path.as_uri()`, and deprecate the duplicate implementation in `PurePath`. Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
parent
6c776abb90
commit
f141e8ec2a
5 changed files with 45 additions and 19 deletions
|
@ -525,13 +525,17 @@ class PurePath:
|
|||
msg = ("pathlib.PurePath.is_reserved() is deprecated and scheduled "
|
||||
"for removal in Python 3.15. Use os.path.isreserved() to "
|
||||
"detect reserved paths on Windows.")
|
||||
warnings.warn(msg, DeprecationWarning, stacklevel=2)
|
||||
warnings._deprecated("pathlib.PurePath.is_reserved", msg, remove=(3, 15))
|
||||
if self.parser is ntpath:
|
||||
return self.parser.isreserved(self)
|
||||
return False
|
||||
|
||||
def as_uri(self):
|
||||
"""Return the path as a URI."""
|
||||
import warnings
|
||||
msg = ("pathlib.PurePath.as_uri() is deprecated and scheduled "
|
||||
"for removal in Python 3.19. Use pathlib.Path.as_uri().")
|
||||
warnings._deprecated("pathlib.PurePath.as_uri", msg, remove=(3, 19))
|
||||
if not self.is_absolute():
|
||||
raise ValueError("relative path can't be expressed as a file URI")
|
||||
|
||||
|
@ -1266,6 +1270,13 @@ class Path(PurePath):
|
|||
raise RuntimeError("Could not determine home directory.")
|
||||
return cls(homedir)
|
||||
|
||||
def as_uri(self):
|
||||
"""Return the path as a URI."""
|
||||
if not self.is_absolute():
|
||||
raise ValueError("relative paths can't be expressed as file URIs")
|
||||
from urllib.request import pathname2url
|
||||
return f'file:{pathname2url(str(self))}'
|
||||
|
||||
@classmethod
|
||||
def from_uri(cls, uri):
|
||||
"""Return a new path from the given 'file' URI."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue