Skip to content

Commit 873c19a

Browse files
committed
Address some review feedback
1 parent 9e75357 commit 873c19a

File tree

5 files changed

+18
-27
lines changed

5 files changed

+18
-27
lines changed

Doc/library/urllib.request.rst

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ The :mod:`urllib.request` module defines the following functions:
175175
The *add_scheme* argument was added.
176176

177177

178-
.. function:: url2pathname(url, *, require_scheme=False, resolve_netloc=False)
178+
.. function:: url2pathname(url, *, require_scheme=False, resolve_host=False)
179179

180180
Convert the given ``file:`` URL to a local path. This function uses
181181
:func:`~urllib.parse.unquote` to decode the URL.
@@ -186,11 +186,11 @@ The :mod:`urllib.request` module defines the following functions:
186186
if it doesn't.
187187

188188
The URL authority is discarded if it is empty, ``localhost``, or the local
189-
hostname. Otherwise, if *resolve_netloc* is set to true, the authority is
189+
hostname. Otherwise, if *resolve_host* is set to true, the authority is
190190
resolved using :func:`socket.gethostbyname` and discarded if it matches a
191-
local IP address. If the authority is still unhandled, then on Windows a
192-
UNC path is returned, and on other platforms a
193-
:exc:`~urllib.error.URLError` is raised.
191+
local IP address (as per :rfc:`RFC 8089 §3 <8089#section-3>`). If the
192+
authority is still unhandled, then on Windows a UNC path is returned, and
193+
on other platforms a :exc:`~urllib.error.URLError` is raised.
194194

195195
This example shows the function being used on Windows::
196196

@@ -211,10 +211,7 @@ The :mod:`urllib.request` module defines the following functions:
211211
:exc:`~urllib.error.URLError` is raised.
212212

213213
.. versionchanged:: next
214-
The *require_scheme* argument was added.
215-
216-
.. versionchanged:: next
217-
The *resolve_netloc* argument was added.
214+
The *require_scheme* and *resolve_host* arguments were added.
218215

219216

220217
.. function:: getproxies()

Doc/whatsnew/3.14.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ urllib
12321232
true.
12331233
- Discard URL authority if it matches the local hostname.
12341234
- Discard URL authority if it resolves to a local IP address when the new
1235-
*resolve_netloc* argument is set to true.
1235+
*resolve_host* argument is set to true.
12361236
- Raise :exc:`~urllib.error.URLError` if a URL authority isn't local,
12371237
except on Windows where we return a UNC path as before.
12381238

Lib/test/test_urllib.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,12 +1569,12 @@ def test_url2pathname_require_scheme_errors(self):
15691569
urllib.request.url2pathname,
15701570
url, require_scheme=True)
15711571

1572-
def test_url2pathname_resolve_netloc(self):
1572+
def test_url2pathname_resolve_host(self):
15731573
fn = urllib.request.url2pathname
15741574
sep = os.path.sep
1575-
self.assertEqual(fn('//127.0.0.1/foo/bar', resolve_netloc=True), f'{sep}foo{sep}bar')
1575+
self.assertEqual(fn('//127.0.0.1/foo/bar', resolve_host=True), f'{sep}foo{sep}bar')
15761576
self.assertEqual(fn(f'//{socket.gethostname()}/foo/bar'), f'{sep}foo{sep}bar')
1577-
self.assertEqual(fn(f'//{socket.gethostname()}/foo/bar', resolve_netloc=True), f'{sep}foo{sep}bar')
1577+
self.assertEqual(fn(f'//{socket.gethostname()}/foo/bar', resolve_host=True), f'{sep}foo{sep}bar')
15781578

15791579
@unittest.skipUnless(sys.platform == 'win32',
15801580
'test specific to Windows pathnames.')

Lib/urllib/request.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,7 @@ def get_names(self):
14661466
def open_local_file(self, req):
14671467
import email.utils
14681468
import mimetypes
1469-
localfile = url2pathname(req.full_url, require_scheme=True, resolve_netloc=True)
1469+
localfile = url2pathname(req.full_url, require_scheme=True, resolve_host=True)
14701470
try:
14711471
stats = os.stat(localfile)
14721472
size = stats.st_size
@@ -1645,22 +1645,22 @@ def data_open(self, req):
16451645

16461646
# Code moved from the old urllib module
16471647

1648-
def url2pathname(url, *, require_scheme=False, resolve_netloc=False):
1648+
def url2pathname(url, *, require_scheme=False, resolve_host=False):
16491649
"""Convert the given file URL to a local file system path.
16501650
16511651
The 'file:' scheme prefix must be omitted unless *require_scheme*
16521652
is set to true.
16531653
16541654
The URL authority may be resolved with gethostbyname() if
1655-
*resolve_netloc* is set to true.
1655+
*resolve_host* is set to true.
16561656
"""
16571657
if require_scheme:
16581658
scheme, url = _splittype(url)
16591659
if scheme != 'file':
16601660
raise URLError("URL is missing a 'file:' scheme")
16611661
authority, url = _splithost(url)
16621662
if os.name == 'nt':
1663-
if not _is_local_authority(authority, resolve_netloc):
1663+
if not _is_local_authority(authority, resolve_host):
16641664
# e.g. file://server/share/file.txt
16651665
url = '//' + authority + url
16661666
elif url[:3] == '///':
@@ -1674,7 +1674,7 @@ def url2pathname(url, *, require_scheme=False, resolve_netloc=False):
16741674
# Older URLs use a pipe after a drive letter
16751675
url = url[:1] + ':' + url[2:]
16761676
url = url.replace('/', '\\')
1677-
elif not _is_local_authority(authority, resolve_netloc):
1677+
elif not _is_local_authority(authority, resolve_host):
16781678
raise URLError("file:// scheme is supported only on localhost")
16791679
encoding = sys.getfilesystemencoding()
16801680
errors = sys.getfilesystemencodeerrors()
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
Fix issue where :func:`urllib.request.url2pathname` mishandled file URLs with
2-
authorities. The process now works as follows:
3-
4-
1. Discard authority if it is empty or ``localhost``; otherwise
5-
2. (New) Discard authority if it matches the local hostname; otherwise
6-
3. (New) If the new *resolve_netloc* keyword-only argument is set to true,
7-
discard authority if it resolves to a local IP address; otherwise
8-
4. On Windows, return a UNC path; otherwise
9-
5. (New) Raise :exc:`urllib.error.URLError`.
1+
Add *resolve_host* keyword-only argument to
2+
:func:`urllib.request.url2pathname`, and fix handling of file URLs with
3+
authorities.

0 commit comments

Comments
 (0)