Skip to content

Commit 79f5d05

Browse files
committed
Restore previous Windows behaviour
1 parent 69807b4 commit 79f5d05

6 files changed

Lines changed: 25 additions & 25 deletions

File tree

Doc/library/pathlib.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -872,10 +872,11 @@ conforming to :rfc:`8089`.
872872
.. versionadded:: 3.13
873873

874874
.. versionchanged:: 3.14
875-
On non-Windows platforms, if a URL authority (e.g. a hostname) is
876-
present, then it is discarded if it resolves to ``localhost``, otherwise
877-
:exc:`ValueError` is raised. In previous versions the authority is
878-
included in the returned path.
875+
If a URL authority (e.g. a hostname) is present and resolves to
876+
``localhost``, it is discarded. If an authority is present and
877+
*doesn't* resolve to ``localhost``, then on Windows a UNC path is
878+
returned (as before), and on other platforms a :exc:`ValueError` is
879+
raised.
879880

880881
.. method:: Path.as_uri()
881882

Doc/library/urllib.request.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,11 @@ The :mod:`urllib.request` module defines the following functions:
182182
'C:\\Program Files'
183183

184184
.. versionchanged:: 3.14
185-
On non-Windows platforms, if a URL authority (e.g. a hostname) is
186-
present, then it is discarded if it resolves to ``localhost``, otherwise
187-
:exc:`~urllib.error.URLError` is raised. In previous versions the
188-
authority is included in the returned path.
185+
If a URL authority (e.g. a hostname) is present and resolves to
186+
``localhost``, it is discarded. If an authority is present and
187+
*doesn't* resolve to ``localhost``, then on Windows a UNC path is
188+
returned (as before), and on other platforms a
189+
:exc:`~urllib.error.URLError` is raised.
189190

190191
.. versionchanged:: 3.14
191192
Windows drive letters are no longer converted to uppercase, and ``:``

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3284,7 +3284,9 @@ def test_from_uri_posix(self):
32843284
self.assertEqual(P.from_uri('file:////foo/bar'), P('//foo/bar'))
32853285
self.assertEqual(P.from_uri('file://localhost/foo/bar'), P('/foo/bar'))
32863286
self.assertEqual(P.from_uri('file://127.0.0.1/foo/bar'), P('/foo/bar'))
3287-
self.assertEqual(P.from_uri(f'file://{socket.gethostname()}/foo/bar'), P('/foo/bar'))
3287+
if not is_wasi:
3288+
self.assertEqual(P.from_uri(f'file://{socket.gethostname()}/foo/bar'),
3289+
P('/foo/bar'))
32883290
self.assertRaises(ValueError, P.from_uri, 'foo/bar')
32893291
self.assertRaises(ValueError, P.from_uri, '/foo/bar')
32903292
self.assertRaises(ValueError, P.from_uri, '//foo/bar')

Lib/test/test_urllib2.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -816,17 +816,14 @@ def test_file(self):
816816
urls = [
817817
canonurl,
818818
parsed._replace(netloc='localhost').geturl(),
819+
parsed._replace(netloc=socket.gethostbyname('localhost')).geturl(),
819820
]
820-
if os.name != 'nt':
821-
# On POSIX the local hostname may appear in a local file URL.
822-
# On Windows this would be decoded as a UNC path.
823-
urls.append(parsed._replace(netloc=socket.gethostbyname('localhost')).geturl())
824-
try:
825-
localaddr = socket.gethostbyname(socket.gethostname())
826-
except socket.gaierror:
827-
localaddr = ''
828-
if localaddr:
829-
urls.append(parsed._replace(netloc=localaddr).geturl())
821+
try:
822+
localaddr = socket.gethostbyname(socket.gethostname())
823+
except socket.gaierror:
824+
localaddr = ''
825+
if localaddr:
826+
urls.append(parsed._replace(netloc=localaddr).geturl())
830827

831828
for url in urls:
832829
f = open(TESTFN, "wb")

Lib/urllib/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,7 @@ def url2pathname(url):
16391639
to a file system path; not recommended for general use."""
16401640
authority, url = _splithost(url)
16411641
if os.name == 'nt':
1642-
if authority and authority != 'localhost':
1642+
if not _is_local_authority(authority):
16431643
# e.g. file://server/share/file.txt
16441644
url = '//' + authority + url
16451645
elif url[:3] == '///':
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
Fix issue where :func:`urllib.request.url2pathname` mishandled file URLs with
2-
non-empty, non-``localhost`` authorities on non-Windows systems. Authorities
3-
that resolve to ``localhost`` are now discarded; other authorities now cause
4-
a :exc:`urllib.error.URLError` to be raised. Previously these authorities
5-
were incorrectly included in the returned path. This change does not affect
6-
Windows, where UNC paths are returned for non-local URLs.
2+
authorities. If an authority is present and resolves to ``localhost``, it is
3+
now discarded. If an authority is present but *doesn't* resolve to
4+
``localhost``, then on Windows a UNC path is returned (as before), and on
5+
other platforms a :exc:`urllib.errors.URLError` is now raised.

0 commit comments

Comments
 (0)