Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
GH-107465: Add pathlib.Path.from_uri() classmethod.#107640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0f51181
GH-107465: Add `pathlib.Path.from_uri()` classmethod.
barneygale 35993b4
Fix whatsnew entry.
barneygale f2cc33f
Add news blurb
barneygale d61d665
Remove test/docs for five-slash DOS drive variant, which doesn't
barneygale 754ba5a
Merge branch 'main' into gh-107465-pathlib-from-uri
barneygale 4647947
Docs tweak
barneygale 2beb5e4
Handle `file://localhost/` URIs
barneygale d73f74c
Merge branch 'main' into gh-107465-pathlib-from-uri
barneygale 310d71c
Merge branch 'main' into gh-107465-pathlib-from-uri
barneygale b89fd33
Raise `ValueError` for relative paths and schemeless URIs
barneygale b198ae0
Fix windows tests
barneygale 6c7c80c
Update Doc/whatsnew/3.13.rst
barneygale bd39b25
Add test for non-`file` scheme
barneygale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -18,7 +18,6 @@ | ||
| from _collections_abc import Sequence | ||
| from errno import ENOENT, ENOTDIR, EBADF, ELOOP, EINVAL | ||
| from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO | ||
| from urllib.parse import quote_from_bytes as urlquote_from_bytes | ||
| try: | ||
| import pwd | ||
| @@ -452,7 +451,8 @@ def as_uri(self): | ||
| # It's a posix path => 'file:///etc/hosts' | ||
| prefix = 'file://' | ||
| path = str(self) | ||
| return prefix + urlquote_from_bytes(os.fsencode(path)) | ||
| from urllib.parse import quote_from_bytes | ||
| return prefix + quote_from_bytes(os.fsencode(path)) | ||
| @property | ||
| def _str_normcase(self): | ||
| @@ -814,9 +814,10 @@ class _PathBase(PurePath): | ||
| __bytes__ = None | ||
| __fspath__ = None # virtual paths have no local file system representation | ||
| def _unsupported(self, method_name): | ||
| msg = f"{type(self).__name__}.{method_name}() is unsupported" | ||
| if isinstance(self, Path): | ||
| @classmethod | ||
| def _unsupported(cls, method_name): | ||
| msg = f"{cls.__name__}.{method_name}() is unsupported" | ||
| if issubclass(cls, Path): | ||
barneygale marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| msg += " on this system" | ||
| raise UnsupportedOperation(msg) | ||
| @@ -1418,6 +1419,11 @@ def group(self): | ||
| """ | ||
| self._unsupported("group") | ||
| @classmethod | ||
| def from_uri(cls, uri): | ||
| """Return a new path from the given 'file' URI.""" | ||
| cls._unsupported("from_uri") | ||
| def as_uri(self): | ||
| """Return the path as a URI.""" | ||
| self._unsupported("as_uri") | ||
| @@ -1661,6 +1667,30 @@ def expanduser(self): | ||
| return self | ||
| @classmethod | ||
| def from_uri(cls, uri): | ||
| """Return a new path from the given 'file' URI.""" | ||
| if not uri.startswith('file:'): | ||
| raise ValueError(f"URI does not start with 'file:': {uri!r}") | ||
| path = uri[5:] | ||
| if path[:3] == '///': | ||
| # Remove empty authority | ||
| path = path[2:] | ||
| elif path[:12] == '//localhost/': | ||
| # Remove 'localhost' authority | ||
| path = path[11:] | ||
| if path[:3] == '///' or (path[:1] == '/' and path[2:3] in ':|'): | ||
| # Remove slash before DOS device/UNC path | ||
| path = path[1:] | ||
| if path[1:2] == '|': | ||
| # Replace bar with colon in DOS drive | ||
| path = path[:1] + ':' + path[2:] | ||
| from urllib.parse import unquote_to_bytes | ||
| path = cls(os.fsdecode(unquote_to_bytes(path))) | ||
| if not path.is_absolute(): | ||
| raise ValueError(f"URI is not absolute: {uri!r}") | ||
| return path | ||
| class PosixPath(Path, PurePosixPath): | ||
| """Path subclass for non-Windows systems. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -11,6 +11,7 @@ | ||
| 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 @@ def test_handling_bad_descriptor(self): | ||
| 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 @@ def check(): | ||
| 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 | ||
barneygale marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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): | ||
1 change: 1 addition & 0 deletions
1 Misc/NEWS.d/next/Library/2023-08-04-19-00-53.gh-issue-107465.Vc1Il3.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add :meth:`pathlib.Path.from_uri` classmethod. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.