Skip to content

GH-126367: url2pathname(): handle NTFS alternate data streams #131428

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 3 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Doc/library/urllib.request.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ The :mod:`urllib.request` module defines the following functions:
'C:\\Program Files'

.. versionchanged:: 3.14
Windows drive letters are no longer converted to uppercase.
Windows drive letters are no longer converted to uppercase, and ``:``
characters not following a drive letter no longer cause an
:exc:`OSError` exception to be raised on Windows.


.. function:: getproxies()
Expand Down
23 changes: 9 additions & 14 deletions Lib/nturl2path.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def url2pathname(url):
# ///C:/foo/bar/spam.foo
# become
# C:\foo\bar\spam.foo
import string, urllib.parse
import urllib.parse
if url[:3] == '///':
# URL has an empty authority section, so the path begins on the third
# character.
Expand All @@ -25,19 +25,14 @@ def url2pathname(url):
if url[:3] == '///':
# Skip past extra slash before UNC drive in URL path.
url = url[1:]
# Windows itself uses ":" even in URLs.
url = url.replace(':', '|')
if not '|' in url:
# No drive specifier, just convert slashes
# make sure not to convert quoted slashes :-)
return urllib.parse.unquote(url.replace('/', '\\'))
comp = url.split('|')
if len(comp) != 2 or comp[0][-1] not in string.ascii_letters:
error = 'Bad URL: ' + url
raise OSError(error)
drive = comp[0][-1]
tail = urllib.parse.unquote(comp[1].replace('/', '\\'))
return drive + ':' + tail
else:
if url[:1] == '/' and url[2:3] in (':', '|'):
# Skip past extra slash before DOS drive in URL path.
url = url[1:]
if url[1:2] == '|':
# Older URLs use a pipe after a drive letter
url = url[:1] + ':' + url[2:]
return urllib.parse.unquote(url.replace('/', '\\'))

def pathname2url(p):
"""OS-specific conversion from a file system path to a relative URL
Expand Down
7 changes: 5 additions & 2 deletions Lib/test/test_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,7 @@ def test_pathname2url_nonascii(self):
'test specific to Windows pathnames.')
def test_url2pathname_win(self):
fn = urllib.request.url2pathname
self.assertEqual(fn('/'), '\\')
self.assertEqual(fn('/C:/'), 'C:\\')
self.assertEqual(fn("///C|"), 'C:')
self.assertEqual(fn("///C:"), 'C:')
Expand All @@ -1502,8 +1503,10 @@ def test_url2pathname_win(self):
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')
# Non-ASCII drive letter
self.assertRaises(IOError, fn, "///\u00e8|/")
# 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')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix issue where :func:`urllib.request.url2pathname` raised :exc:`OSError`
when given a Windows URI containing a colon character not following a drive
letter, such as before an NTFS alternate data stream.
Loading