-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
GH-125866: Deprecate nturl2path module
#131432
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
barneygale
merged 8 commits into
python:main
from
barneygale:gh-125866-nturl2path-tests
Mar 19, 2025
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
033baf5
GH-125866: Add tests for `nturl2path` module
barneygale 85a84fc
Merge branch 'main' into gh-125866-nturl2path-tests
barneygale 90e597d
Merge `nturl2path` implementation into `urllib.request`
barneygale f27489c
Deprecate `nturl2path`
barneygale e47c565
Fix lint
barneygale 7183bd6
Fix windows tests, spacing.
barneygale ff5e729
Address review feedback
barneygale 41452b3
Tiny simplification
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
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 |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import unittest | ||
|
|
||
| from test.support import warnings_helper | ||
|
|
||
|
|
||
| nturl2path = warnings_helper.import_deprecated("nturl2path") | ||
|
|
||
|
|
||
| class NTURL2PathTest(unittest.TestCase): | ||
| """Test pathname2url() and url2pathname()""" | ||
|
|
||
| def test_basic(self): | ||
| # Make sure simple tests pass | ||
| expected_path = r"parts\of\a\path" | ||
| expected_url = "parts/of/a/path" | ||
| result = nturl2path.pathname2url(expected_path) | ||
| self.assertEqual(expected_url, result, | ||
| "pathname2url() failed; %s != %s" % | ||
| (result, expected_url)) | ||
| result = nturl2path.url2pathname(expected_url) | ||
| self.assertEqual(expected_path, result, | ||
| "url2pathame() failed; %s != %s" % | ||
| (result, expected_path)) | ||
|
|
||
| def test_pathname2url(self): | ||
| # Test special prefixes are correctly handled in pathname2url() | ||
| fn = nturl2path.pathname2url | ||
| self.assertEqual(fn('\\\\?\\C:\\dir'), '///C:/dir') | ||
| self.assertEqual(fn('\\\\?\\unc\\server\\share\\dir'), '//server/share/dir') | ||
| self.assertEqual(fn("C:"), '///C:') | ||
| self.assertEqual(fn("C:\\"), '///C:/') | ||
| self.assertEqual(fn('c:\\a\\b.c'), '///c:/a/b.c') | ||
| self.assertEqual(fn('C:\\a\\b.c'), '///C:/a/b.c') | ||
| self.assertEqual(fn('C:\\a\\b.c\\'), '///C:/a/b.c/') | ||
| self.assertEqual(fn('C:\\a\\\\b.c'), '///C:/a//b.c') | ||
| self.assertEqual(fn('C:\\a\\b%#c'), '///C:/a/b%25%23c') | ||
| self.assertEqual(fn('C:\\a\\b\xe9'), '///C:/a/b%C3%A9') | ||
| self.assertEqual(fn('C:\\foo\\bar\\spam.foo'), "///C:/foo/bar/spam.foo") | ||
| # NTFS alternate data streams | ||
| self.assertEqual(fn('C:\\foo:bar'), '///C:/foo%3Abar') | ||
| self.assertEqual(fn('foo:bar'), 'foo%3Abar') | ||
| # No drive letter | ||
| self.assertEqual(fn("\\folder\\test\\"), '///folder/test/') | ||
| self.assertEqual(fn("\\\\folder\\test\\"), '//folder/test/') | ||
| self.assertEqual(fn("\\\\\\folder\\test\\"), '///folder/test/') | ||
| self.assertEqual(fn('\\\\some\\share\\'), '//some/share/') | ||
| self.assertEqual(fn('\\\\some\\share\\a\\b.c'), '//some/share/a/b.c') | ||
| self.assertEqual(fn('\\\\some\\share\\a\\b%#c\xe9'), '//some/share/a/b%25%23c%C3%A9') | ||
| # Alternate path separator | ||
| self.assertEqual(fn('C:/a/b.c'), '///C:/a/b.c') | ||
| self.assertEqual(fn('//some/share/a/b.c'), '//some/share/a/b.c') | ||
| self.assertEqual(fn('//?/C:/dir'), '///C:/dir') | ||
| self.assertEqual(fn('//?/unc/server/share/dir'), '//server/share/dir') | ||
| # Round-tripping | ||
| urls = ['///C:', | ||
| '///folder/test/', | ||
| '///C:/foo/bar/spam.foo'] | ||
| for url in urls: | ||
| self.assertEqual(fn(nturl2path.url2pathname(url)), url) | ||
|
|
||
| def test_url2pathname(self): | ||
| fn = nturl2path.url2pathname | ||
| self.assertEqual(fn('/'), '\\') | ||
| self.assertEqual(fn('/C:/'), 'C:\\') | ||
| self.assertEqual(fn("///C|"), 'C:') | ||
| self.assertEqual(fn("///C:"), 'C:') | ||
| self.assertEqual(fn('///C:/'), 'C:\\') | ||
| self.assertEqual(fn('/C|//'), 'C:\\\\') | ||
| self.assertEqual(fn('///C|/path'), 'C:\\path') | ||
| # No DOS drive | ||
| self.assertEqual(fn("///C/test/"), '\\C\\test\\') | ||
| self.assertEqual(fn("////C/test/"), '\\\\C\\test\\') | ||
| # DOS drive paths | ||
| self.assertEqual(fn('c:/path/to/file'), 'c:\\path\\to\\file') | ||
| self.assertEqual(fn('C:/path/to/file'), 'C:\\path\\to\\file') | ||
| self.assertEqual(fn('C:/path/to/file/'), 'C:\\path\\to\\file\\') | ||
| self.assertEqual(fn('C:/path/to//file'), 'C:\\path\\to\\\\file') | ||
| self.assertEqual(fn('C|/path/to/file'), 'C:\\path\\to\\file') | ||
| self.assertEqual(fn('/C|/path/to/file'), 'C:\\path\\to\\file') | ||
| self.assertEqual(fn('///C|/path/to/file'), 'C:\\path\\to\\file') | ||
| self.assertEqual(fn("///C|/foo/bar/spam.foo"), 'C:\\foo\\bar\\spam.foo') | ||
| # Colons in URI | ||
| self.assertEqual(fn('///\u00e8|/'), '\u00e8:\\') | ||
| self.assertEqual(fn('//host/share/spam.txt:eggs'), '\\\\host\\share\\spam.txt:eggs') | ||
| self.assertEqual(fn('///c:/spam.txt:eggs'), 'c:\\spam.txt:eggs') | ||
| # UNC paths | ||
| self.assertEqual(fn('//server/path/to/file'), '\\\\server\\path\\to\\file') | ||
| self.assertEqual(fn('////server/path/to/file'), '\\\\server\\path\\to\\file') | ||
| self.assertEqual(fn('/////server/path/to/file'), '\\\\server\\path\\to\\file') | ||
| # Localhost paths | ||
| self.assertEqual(fn('//localhost/C:/path/to/file'), 'C:\\path\\to\\file') | ||
| self.assertEqual(fn('//localhost/C|/path/to/file'), 'C:\\path\\to\\file') | ||
| self.assertEqual(fn('//localhost/path/to/file'), '\\path\\to\\file') | ||
| self.assertEqual(fn('//localhost//server/path/to/file'), '\\\\server\\path\\to\\file') | ||
| # Percent-encoded forward slashes are preserved for backwards compatibility | ||
| self.assertEqual(fn('C:/foo%2fbar'), 'C:\\foo/bar') | ||
| self.assertEqual(fn('//server/share/foo%2fbar'), '\\\\server\\share\\foo/bar') | ||
| # Round-tripping | ||
| paths = ['C:', | ||
| r'\C\test\\', | ||
| r'C:\foo\bar\spam.foo'] | ||
| for path in paths: | ||
| self.assertEqual(fn(nturl2path.pathname2url(path)), path) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
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
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2025-03-19-00-09-15.gh-issue-125866.sIIJ5N.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,3 @@ | ||
| Deprecate the :mod:`!nturl2path` module. Call | ||
| :func:`urllib.request.url2pathname` and :func:`~urllib.request.pathname2url` | ||
| instead. |
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.