Skip to content

Improve path_to_url() tests #13496

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
merged 5 commits into from
Jul 27, 2025
Merged

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented Jul 21, 2025

Use urllib.parse.quote() rather than urllib.request.pathname2url() to generate expected URLs in tests for path_to_url(). This makes expectations a little more explicit.

(The implementation of path_to_url() itself calls pathname2url(), and so we were marking our own homework!)

Also stop relying on urllib to transform Windows drive letters to uppercase (this behaviour was removed in 3.14.)

Use `urllib.parse.quote()` rather than `urllib.request.pathname2url()` to
generate expected URLs in tests for `path_to_url()`. This makes
expectations a little more explicit.

(The implementation of `path_to_url()` itself calls `pathname2url()`, and
so we were marking our own homework!)
@@ -11,15 +11,15 @@
def test_path_to_url_unix() -> None:
assert path_to_url("/tmp/file") == "file:///tmp/file"
path = os.path.join(os.getcwd(), "file")
assert path_to_url("file") == "file://" + path
assert path_to_url("file") == "file://" + urllib.parse.quote(path)
Copy link
Contributor Author

@barneygale barneygale Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this change, tests will fail if the working directory contains a non-urlsafe character.

@barneygale
Copy link
Contributor Author

Hey @ichard26, could I request your review on this please? It's quite closely related to #13423, which has just landed.

@ichard26
Copy link
Member

Unfortunately as I've already mentioned, I am utterly useless when it comes to URLs and filenames. I'd prefer if someone with a bit more expertise reviewed this. Alternatively, I can review this, but it will be pretty far down on my priority list as I have a release to cut in a few days.

@barneygale
Copy link
Contributor Author

Ah OK, no worries. I'll wait for someone else to review I suppose.

Copy link
Member

@ichard26 ichard26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some reading on filepaths and URLs. This seems fine, although I do have some questions to make sure I'm understanding this right.

Comment on lines +41 to +42
path = os.path.join(os.getcwd(), "file").replace("\\", "/")
assert path_to_url("file") == "file:///" + urllib.parse.quote(path, safe="/:")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure, file:c:/file (and file:/c:/file) is the non-normative form of file:///c:/file but they are effectively equivalent, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, on Windows, does pathname2url() also include : in its url-safe default list? I'm on Linux, and pathname2url("C:\\Program Files") returns file:C%3A%5CProgram%20Files which is fine for Linux, but would be nonsensial for Windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure, file:c:/file (and file:/c:/file) is the non-normative form of file:///c:/file but they are effectively equivalent, right?

That's correct.

Also, on Windows, does pathname2url() also include : in its url-safe default list?

It does for the drive, yes: https://github.com/python/cpython/blob/6784ef7da7cbf1a944fd0685630ced54e4a0066c/Lib/urllib/request.py#L1711

(NTFS allows colons in other parts of the path, but Windows does not.)

Copy link
Member

@ichard26 ichard26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some more reading on the URL RFCs. This looks good. I'll merge this now to make progress towards unblocking the release, but more work will be needed to fully fix 3.14 CI.

In addition, a post-merge review from people with more expertise than me (when time permits) would be appreciated.

@ichard26 ichard26 merged commit 853a593 into pypa:main Jul 27, 2025
29 checks passed
@ichard26
Copy link
Member

Also thank you @barneygale! I will need your help with resolving the remaining failures in #13506.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants