Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f141e8e

Browse files
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 <[email protected]>
1 parent 6c776ab commit f141e8e

File tree

5 files changed

+45
-19
lines changed

5 files changed

+45
-19
lines changed

Doc/library/pathlib.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -886,9 +886,11 @@ conforming to :rfc:`8089`.
886886
>>> p.as_uri()
887887
'file:///c:/Windows'
888888
889-
For historical reasons, this method is also available from
890-
:class:`PurePath` objects. However, its use of :func:`os.fsencode` makes
891-
it strictly impure.
889+
.. deprecated-removed:: 3.14 3.19
890+
891+
Calling this method from :class:`PurePath` rather than :class:`Path` is
892+
possible but deprecated. The method's use of :func:`os.fsencode` makes
893+
it strictly impure.
892894

893895

894896
Expanding and resolving paths

Doc/whatsnew/3.14.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,11 @@ Deprecated
11591159
write new code. The :mod:`subprocess` module is recommended instead.
11601160
(Contributed by Victor Stinner in :gh:`120743`.)
11611161

1162+
* :mod:`pathlib`:
1163+
:meth:`!pathlib.PurePath.as_uri` is deprecated and will be removed in Python
1164+
3.19. Use :meth:`pathlib.Path.as_uri` instead.
1165+
(Contributed by Barney Gale in :gh:`123599`.)
1166+
11621167
* :mod:`pdb`:
11631168
The undocumented ``pdb.Pdb.curframe_locals`` attribute is now a deprecated
11641169
read-only property. The low overhead dynamic frame locals access added in

Lib/pathlib/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,13 +525,17 @@ def is_reserved(self):
525525
msg = ("pathlib.PurePath.is_reserved() is deprecated and scheduled "
526526
"for removal in Python 3.15. Use os.path.isreserved() to "
527527
"detect reserved paths on Windows.")
528-
warnings.warn(msg, DeprecationWarning, stacklevel=2)
528+
warnings._deprecated("pathlib.PurePath.is_reserved", msg, remove=(3, 15))
529529
if self.parser is ntpath:
530530
return self.parser.isreserved(self)
531531
return False
532532

533533
def as_uri(self):
534534
"""Return the path as a URI."""
535+
import warnings
536+
msg = ("pathlib.PurePath.as_uri() is deprecated and scheduled "
537+
"for removal in Python 3.19. Use pathlib.Path.as_uri().")
538+
warnings._deprecated("pathlib.PurePath.as_uri", msg, remove=(3, 19))
535539
if not self.is_absolute():
536540
raise ValueError("relative path can't be expressed as a file URI")
537541

@@ -1266,6 +1270,13 @@ def home(cls):
12661270
raise RuntimeError("Could not determine home directory.")
12671271
return cls(homedir)
12681272

1273+
def as_uri(self):
1274+
"""Return the path as a URI."""
1275+
if not self.is_absolute():
1276+
raise ValueError("relative paths can't be expressed as file URIs")
1277+
from urllib.request import pathname2url
1278+
return f'file:{pathname2url(str(self))}'
1279+
12691280
@classmethod
12701281
def from_uri(cls, uri):
12711282
"""Return a new path from the given 'file' URI."""

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -412,12 +412,18 @@ def assertLess(a, b):
412412
with self.assertRaises(TypeError):
413413
P() < {}
414414

415+
def make_uri(self, path):
416+
if isinstance(path, pathlib.Path):
417+
return path.as_uri()
418+
with self.assertWarns(DeprecationWarning):
419+
return path.as_uri()
420+
415421
def test_as_uri_common(self):
416422
P = self.cls
417423
with self.assertRaises(ValueError):
418-
P('a').as_uri()
424+
self.make_uri(P('a'))
419425
with self.assertRaises(ValueError):
420-
P().as_uri()
426+
self.make_uri(P())
421427

422428
def test_repr_roundtrips(self):
423429
for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'):
@@ -645,9 +651,9 @@ def test_eq_posix(self):
645651
@needs_posix
646652
def test_as_uri_posix(self):
647653
P = self.cls
648-
self.assertEqual(P('/').as_uri(), 'file:///')
649-
self.assertEqual(P('/a/b.c').as_uri(), 'file:///a/b.c')
650-
self.assertEqual(P('/a/b%#c').as_uri(), 'file:///a/b%25%23c')
654+
self.assertEqual(self.make_uri(P('/')), 'file:///')
655+
self.assertEqual(self.make_uri(P('/a/b.c')), 'file:///a/b.c')
656+
self.assertEqual(self.make_uri(P('/a/b%#c')), 'file:///a/b%25%23c')
651657

652658
@needs_posix
653659
def test_as_uri_non_ascii(self):
@@ -657,7 +663,7 @@ def test_as_uri_non_ascii(self):
657663
os.fsencode('\xe9')
658664
except UnicodeEncodeError:
659665
self.skipTest("\\xe9 cannot be encoded to the filesystem encoding")
660-
self.assertEqual(P('/a/b\xe9').as_uri(),
666+
self.assertEqual(self.make_uri(P('/a/b\xe9')),
661667
'file:///a/b' + quote_from_bytes(os.fsencode('\xe9')))
662668

663669
@needs_posix
@@ -751,17 +757,17 @@ def test_eq_windows(self):
751757
def test_as_uri_windows(self):
752758
P = self.cls
753759
with self.assertRaises(ValueError):
754-
P('/a/b').as_uri()
760+
self.make_uri(P('/a/b'))
755761
with self.assertRaises(ValueError):
756-
P('c:a/b').as_uri()
757-
self.assertEqual(P('c:/').as_uri(), 'file:///c:/')
758-
self.assertEqual(P('c:/a/b.c').as_uri(), 'file:///c:/a/b.c')
759-
self.assertEqual(P('c:/a/b%#c').as_uri(), 'file:///c:/a/b%25%23c')
760-
self.assertEqual(P('c:/a/b\xe9').as_uri(), 'file:///c:/a/b%C3%A9')
761-
self.assertEqual(P('//some/share/').as_uri(), 'file://some/share/')
762-
self.assertEqual(P('//some/share/a/b.c').as_uri(),
762+
self.make_uri(P('c:a/b'))
763+
self.assertEqual(self.make_uri(P('c:/')), 'file:///c:/')
764+
self.assertEqual(self.make_uri(P('c:/a/b.c')), 'file:///c:/a/b.c')
765+
self.assertEqual(self.make_uri(P('c:/a/b%#c')), 'file:///c:/a/b%25%23c')
766+
self.assertEqual(self.make_uri(P('c:/a/b\xe9')), 'file:///c:/a/b%C3%A9')
767+
self.assertEqual(self.make_uri(P('//some/share/')), 'file://some/share/')
768+
self.assertEqual(self.make_uri(P('//some/share/a/b.c')),
763769
'file://some/share/a/b.c')
764-
self.assertEqual(P('//some/share/a/b%#c\xe9').as_uri(),
770+
self.assertEqual(self.make_uri(P('//some/share/a/b%#c\xe9')),
765771
'file://some/share/a/b%25%23c%C3%A9')
766772

767773
@needs_windows
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deprecate :meth:`!pathlib.PurePath.as_uri`; use :meth:`pathlib.Path.as_uri`
2+
instead.

0 commit comments

Comments
 (0)