GH-107465: Add pathlib.Path.from_uri() classmethod. (#107640)

This method supports file URIs (including variants) as described in RFC 8089, such as URIs generated by `pathlib.Path.as_uri()` and `urllib.request.pathname2url()`.

The method is added to `Path` rather than `PurePath` because it uses `os.fsdecode()`, and so its results vary from system to system. I intend to deprecate `PurePath.as_uri()` and move it to `Path` for the same reason.

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
Barney Gale 2023-10-01 16:14:02 +01:00 committed by GitHub
parent 06faa9a39b
commit 15de493395
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 120 additions and 5 deletions

View file

@ -11,6 +11,7 @@ import stat
import tempfile
import unittest
from unittest import mock
from urllib.request import pathname2url
from test.support import import_helper
from test.support import set_recursion_limit
@ -3602,6 +3603,24 @@ class PosixPathTest(PathTest):
self.fail("Bad file descriptor not handled.")
raise
def test_from_uri(self):
P = self.cls
self.assertEqual(P.from_uri('file:/foo/bar'), P('/foo/bar'))
self.assertEqual(P.from_uri('file://foo/bar'), P('//foo/bar'))
self.assertEqual(P.from_uri('file:///foo/bar'), P('/foo/bar'))
self.assertEqual(P.from_uri('file:////foo/bar'), P('//foo/bar'))
self.assertEqual(P.from_uri('file://localhost/foo/bar'), P('/foo/bar'))
self.assertRaises(ValueError, P.from_uri, 'foo/bar')
self.assertRaises(ValueError, P.from_uri, '/foo/bar')
self.assertRaises(ValueError, P.from_uri, '//foo/bar')
self.assertRaises(ValueError, P.from_uri, 'file:foo/bar')
self.assertRaises(ValueError, P.from_uri, 'http://foo/bar')
def test_from_uri_pathname2url(self):
P = self.cls
self.assertEqual(P.from_uri('file:' + pathname2url('/foo/bar')), P('/foo/bar'))
self.assertEqual(P.from_uri('file:' + pathname2url('//foo/bar')), P('//foo/bar'))
@only_nt
class WindowsPathTest(PathTest):
@ -3721,6 +3740,31 @@ class WindowsPathTest(PathTest):
env['HOME'] = 'C:\\Users\\eve'
check()
def test_from_uri(self):
P = self.cls
# DOS drive paths
self.assertEqual(P.from_uri('file:c:/path/to/file'), P('c:/path/to/file'))
self.assertEqual(P.from_uri('file:c|/path/to/file'), P('c:/path/to/file'))
self.assertEqual(P.from_uri('file:/c|/path/to/file'), P('c:/path/to/file'))
self.assertEqual(P.from_uri('file:///c|/path/to/file'), P('c:/path/to/file'))
# UNC paths
self.assertEqual(P.from_uri('file://server/path/to/file'), P('//server/path/to/file'))
self.assertEqual(P.from_uri('file:////server/path/to/file'), P('//server/path/to/file'))
self.assertEqual(P.from_uri('file://///server/path/to/file'), P('//server/path/to/file'))
# Localhost paths
self.assertEqual(P.from_uri('file://localhost/c:/path/to/file'), P('c:/path/to/file'))
self.assertEqual(P.from_uri('file://localhost/c|/path/to/file'), P('c:/path/to/file'))
# Invalid paths
self.assertRaises(ValueError, P.from_uri, 'foo/bar')
self.assertRaises(ValueError, P.from_uri, 'c:/foo/bar')
self.assertRaises(ValueError, P.from_uri, '//foo/bar')
self.assertRaises(ValueError, P.from_uri, 'file:foo/bar')
self.assertRaises(ValueError, P.from_uri, 'http://foo/bar')
def test_from_uri_pathname2url(self):
P = self.cls
self.assertEqual(P.from_uri('file:' + pathname2url(r'c:\path\to\file')), P('c:/path/to/file'))
self.assertEqual(P.from_uri('file:' + pathname2url(r'\\server\path\to\file')), P('//server/path/to/file'))
class PathSubclassTest(PathTest):