Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd783d7b

Browse files
authored
GH-126367:url2pathname(): handle NTFS alternate data streams (#131428)
Adjust `url2pathname()` to decode embedded colon characters in WindowsURIs, rather than bailing out with an `OSError`.
1 parent01b5abb commitd783d7b

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

‎Doc/library/urllib.request.rst‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ The :mod:`urllib.request` module defines the following functions:
182182
'C:\\Program Files'
183183

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

187189

188190
..function::getproxies()

‎Lib/nturl2path.py‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def url2pathname(url):
1414
# ///C:/foo/bar/spam.foo
1515
# become
1616
# C:\foo\bar\spam.foo
17-
importstring,urllib.parse
17+
importurllib.parse
1818
ifurl[:3]=='///':
1919
# URL has an empty authority section, so the path begins on the third
2020
# character.
@@ -25,19 +25,14 @@ def url2pathname(url):
2525
ifurl[:3]=='///':
2626
# Skip past extra slash before UNC drive in URL path.
2727
url=url[1:]
28-
# Windows itself uses ":" even in URLs.
29-
url=url.replace(':','|')
30-
ifnot'|'inurl:
31-
# No drive specifier, just convert slashes
32-
# make sure not to convert quoted slashes :-)
33-
returnurllib.parse.unquote(url.replace('/','\\'))
34-
comp=url.split('|')
35-
iflen(comp)!=2orcomp[0][-1]notinstring.ascii_letters:
36-
error='Bad URL: '+url
37-
raiseOSError(error)
38-
drive=comp[0][-1]
39-
tail=urllib.parse.unquote(comp[1].replace('/','\\'))
40-
returndrive+':'+tail
28+
else:
29+
ifurl[:1]=='/'andurl[2:3]in (':','|'):
30+
# Skip past extra slash before DOS drive in URL path.
31+
url=url[1:]
32+
ifurl[1:2]=='|':
33+
# Older URLs use a pipe after a drive letter
34+
url=url[:1]+':'+url[2:]
35+
returnurllib.parse.unquote(url.replace('/','\\'))
4136

4237
defpathname2url(p):
4338
"""OS-specific conversion from a file system path to a relative URL

‎Lib/test/test_urllib.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,7 @@ def test_pathname2url_nonascii(self):
14841484
'test specific to Windows pathnames.')
14851485
deftest_url2pathname_win(self):
14861486
fn=urllib.request.url2pathname
1487+
self.assertEqual(fn('/'),'\\')
14871488
self.assertEqual(fn('/C:/'),'C:\\')
14881489
self.assertEqual(fn("///C|"),'C:')
14891490
self.assertEqual(fn("///C:"),'C:')
@@ -1502,8 +1503,10 @@ def test_url2pathname_win(self):
15021503
self.assertEqual(fn('/C|/path/to/file'),'C:\\path\\to\\file')
15031504
self.assertEqual(fn('///C|/path/to/file'),'C:\\path\\to\\file')
15041505
self.assertEqual(fn("///C|/foo/bar/spam.foo"),'C:\\foo\\bar\\spam.foo')
1505-
# Non-ASCII drive letter
1506-
self.assertRaises(IOError,fn,"///\u00e8|/")
1506+
# Colons in URI
1507+
self.assertEqual(fn('///\u00e8|/'),'\u00e8:\\')
1508+
self.assertEqual(fn('//host/share/spam.txt:eggs'),'\\\\host\\share\\spam.txt:eggs')
1509+
self.assertEqual(fn('///c:/spam.txt:eggs'),'c:\\spam.txt:eggs')
15071510
# UNC paths
15081511
self.assertEqual(fn('//server/path/to/file'),'\\\\server\\path\\to\\file')
15091512
self.assertEqual(fn('////server/path/to/file'),'\\\\server\\path\\to\\file')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix issue where:func:`urllib.request.url2pathname` raised:exc:`OSError`
2+
when given a Windows URI containing a colon character not following a drive
3+
letter, such as before an NTFS alternate data stream.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp