-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Make conversion of file URLs more consistent #13501
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
Open
barneygale
wants to merge
20
commits into
pypa:main
Choose a base branch
from
barneygale:ship-conversion-utils
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
eac855f
Stop using `pathname2url()` and `url2pathname()` in `urls`
barneygale 3fb3599
Add `clean_file_url()` utility.
barneygale 9860f51
Windows tests fixes
barneygale 2f3d8d9
More Windows gubbins
barneygale 60edfd8
Tweak
barneygale 0757e0d
Tweak test regex
barneygale f846efd
More Windows test fixes, cleanups.
barneygale 359557e
Undo test changes
barneygale d07a876
Support backslashes in URLs
barneygale 071a7e3
Add news blurb
barneygale 5c23ec0
Add comments, improve naming.
barneygale 8c817be
Enable Python 3.14 CI 🤞
barneygale b2997be
Undo drive letter case change
barneygale 7db59a0
Very minor tweaks
barneygale 00b7fa0
Undo change to `pip._internal.vcs.git` and un-move function.
barneygale 90a9055
Merge branch 'main' into ship-conversion-utils
barneygale b92581f
Merge branch 'main' into ship-conversion-utils
barneygale f30c187
Fix lint
barneygale 1ad7fbe
Revert "Enable Python 3.14 CI 🤞"
barneygale ddb84bc
Merge branch 'main' into ship-conversion-utils
barneygale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Make conversion of file URLs more consistent across Python versions. |
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 |
---|---|---|
@@ -1,55 +1,70 @@ | ||
import os | ||
import string | ||
import sys | ||
import urllib.parse | ||
import urllib.request | ||
|
||
from .compat import WINDOWS | ||
|
||
|
||
def path_to_url(path: str) -> str: | ||
def path_to_url(path: str, normalize_path: bool = True) -> str: | ||
""" | ||
Convert a path to a file: URL. The path will be made absolute and have | ||
quoted path parts. | ||
Convert a path to a file: URL with quoted path parts. The path will be | ||
normalized and made absolute if *normalize_path* is true (the default.) | ||
""" | ||
path = os.path.normpath(os.path.abspath(path)) | ||
url = urllib.parse.urljoin("file://", urllib.request.pathname2url(path)) | ||
return url | ||
if normalize_path: | ||
path = os.path.abspath(path) | ||
if WINDOWS: | ||
path = path.replace("\\", "/") | ||
|
||
drive, tail = os.path.splitdrive(path) | ||
if drive: | ||
if drive[:4] == "//?/": | ||
drive = drive[4:] | ||
if drive[:4].upper() == "UNC/": | ||
drive = "//" + drive[4:] | ||
if drive[1:] == ":": | ||
drive = "///" + drive | ||
elif tail.startswith("/"): | ||
tail = "//" + tail | ||
|
||
encoding = sys.getfilesystemencoding() | ||
errors = sys.getfilesystemencodeerrors() | ||
drive = urllib.parse.quote(drive, "/:", encoding, errors) | ||
tail = urllib.parse.quote(tail, "/", encoding, errors) | ||
return "file:" + drive + tail | ||
|
||
|
||
def url_to_path(url: str) -> str: | ||
""" | ||
Convert a file: URL to a path. | ||
""" | ||
assert url.startswith( | ||
"file:" | ||
scheme, netloc, path = urllib.parse.urlsplit(url)[:3] | ||
assert scheme == "file" or scheme.endswith( | ||
"+file" | ||
), f"You can only turn file: urls into filenames (not {url!r})" | ||
|
||
_, netloc, path, _, _ = urllib.parse.urlsplit(url) | ||
if WINDOWS: | ||
# e.g. file://c:/foo | ||
if netloc[1:2] == ":": | ||
path = netloc + path | ||
|
||
# e.g. file://server/share/foo | ||
elif netloc and netloc != "localhost": | ||
path = "//" + netloc + path | ||
|
||
# e.g. file://///server/share/foo | ||
elif path[:3] == "///": | ||
path = path[1:] | ||
|
||
# e.g. file:///c:/foo | ||
elif path[:1] == "/" and path[2:3] == ":": | ||
path = path[1:] | ||
|
||
if not netloc or netloc == "localhost": | ||
# According to RFC 8089, same as empty authority. | ||
netloc = "" | ||
elif WINDOWS: | ||
# If we have a UNC path, prepend UNC share notation. | ||
netloc = "\\\\" + netloc | ||
else: | ||
path = path.replace("/", "\\") | ||
elif netloc and netloc != "localhost": | ||
raise ValueError( | ||
f"non-local file URIs are not supported on this platform: {url!r}" | ||
) | ||
|
||
path = urllib.request.url2pathname(netloc + path) | ||
|
||
# On Windows, urlsplit parses the path as something like "/C:/Users/foo". | ||
# This creates issues for path-related functions like io.open(), so we try | ||
# to detect and strip the leading slash. | ||
if ( | ||
WINDOWS | ||
and not netloc # Not UNC. | ||
and len(path) >= 3 | ||
and path[0] == "/" # Leading slash to strip. | ||
and path[1] in string.ascii_letters # Drive letter. | ||
and path[2:4] in (":", ":/") # Colon + end of string, or colon + absolute path. | ||
): | ||
path = path[1:] | ||
|
||
return path | ||
encoding = sys.getfilesystemencoding() | ||
errors = sys.getfilesystemencodeerrors() | ||
return urllib.parse.unquote(path, encoding, errors) |
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 |
---|---|---|
|
@@ -33,17 +33,13 @@ | |
Link, | ||
LinkHash, | ||
MetadataFile, | ||
_clean_file_url, | ||
_clean_url_path, | ||
_ensure_quoted_url, | ||
) | ||
from pip._internal.network.session import PipSession | ||
|
||
from tests.lib import ( | ||
TestData, | ||
make_test_link_collector, | ||
skip_needs_new_pathname2url_trailing_slash_behavior_win, | ||
skip_needs_old_pathname2url_trailing_slash_behavior_win, | ||
) | ||
from tests.lib import TestData, make_test_link_collector | ||
|
||
ACCEPT = ", ".join( | ||
[ | ||
|
@@ -296,31 +292,30 @@ def test_get_simple_response_dont_log_clear_text_password( | |
("a %2f b", "a%20%2F%20b"), | ||
], | ||
) | ||
@pytest.mark.parametrize("is_local_path", [True, False]) | ||
def test_clean_url_path(path: str, expected: str, is_local_path: bool) -> None: | ||
assert _clean_url_path(path, is_local_path=is_local_path) == expected | ||
def test_clean_url_path(path: str, expected: str) -> None: | ||
assert _clean_url_path(path) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"path, expected", | ||
"url, expected", | ||
[ | ||
# Test a VCS path with a Windows drive letter and revision. | ||
pytest.param( | ||
"/T:/with space/[email protected]", | ||
"/T:/with%20space/[email protected]", | ||
"file:/T:/with space/[email protected]", | ||
"file:///T:/with%20space/[email protected]", | ||
marks=pytest.mark.skipif("sys.platform != 'win32'"), | ||
), | ||
# Test a VCS path with a Windows drive letter and revision, | ||
# running on non-windows platform. | ||
pytest.param( | ||
"/T:/with space/[email protected]", | ||
"/T%3A/with%20space/[email protected]", | ||
"file:/T:/with space/[email protected]", | ||
"file:///T%3A/with%20space/[email protected]", | ||
marks=pytest.mark.skipif("sys.platform == 'win32'"), | ||
), | ||
], | ||
) | ||
def test_clean_url_path_with_local_path(path: str, expected: str) -> None: | ||
actual = _clean_url_path(path, is_local_path=True) | ||
def test_clean_file_url(url: str, expected: str) -> None: | ||
actual = _clean_file_url(url) | ||
assert actual == expected | ||
|
||
|
||
|
@@ -387,16 +382,11 @@ def test_clean_url_path_with_local_path(path: str, expected: str) -> None: | |
), | ||
# URL with Windows drive letter. The `:` after the drive | ||
# letter should not be quoted. The trailing `/` should be | ||
# removed. | ||
pytest.param( | ||
"file:///T:/path/with spaces/", | ||
"file:///T:/path/with%20spaces", | ||
marks=skip_needs_old_pathname2url_trailing_slash_behavior_win, | ||
), | ||
# retained. | ||
pytest.param( | ||
"file:///T:/path/with spaces/", | ||
"file:///T:/path/with%20spaces/", | ||
marks=skip_needs_new_pathname2url_trailing_slash_behavior_win, | ||
marks=pytest.mark.skipif("sys.platform != 'win32'"), | ||
), | ||
# URL with Windows drive letter, running on non-windows | ||
# platform. The `:` after the drive should be quoted. | ||
|
Oops, something went wrong.
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.