mirror of
https://github.com/python/cpython.git
synced 2025-08-09 19:38:42 +00:00
[3.12] GH-85168: Use filesystem encoding when converting to/from file
URIs (GH-126852) (#127040)
GH-85168: Use filesystem encoding when converting to/from `file` URIs (GH-126852)
Adjust `urllib.request.url2pathname()` and `pathname2url()` to use the
filesystem encoding when quoting and unquoting file URIs, rather than
forcing use of UTF-8.
No changes are needed in the `nturl2path` module because Windows always
uses UTF-8, per PEP 529.
(cherry picked from commit c9b399fbdb
)
Co-authored-by: Barney Gale <barney.gale@gmail.com>
This commit is contained in:
parent
153221a1d0
commit
7735f58ea5
4 changed files with 26 additions and 10 deletions
|
@ -718,10 +718,6 @@ class urlretrieve_FileTests(unittest.TestCase):
|
|||
|
||||
def constructLocalFileUrl(self, filePath):
|
||||
filePath = os.path.abspath(filePath)
|
||||
try:
|
||||
filePath.encode("utf-8")
|
||||
except UnicodeEncodeError:
|
||||
raise unittest.SkipTest("filePath is not encodable to utf8")
|
||||
return "file://%s" % urllib.request.pathname2url(filePath)
|
||||
|
||||
def createNewTempFile(self, data=b""):
|
||||
|
@ -1571,6 +1567,13 @@ class Pathname_Tests(unittest.TestCase):
|
|||
self.assertEqual(fn('/a/b.c'), '/a/b.c')
|
||||
self.assertEqual(fn('/a/b%#c'), '/a/b%25%23c')
|
||||
|
||||
@unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
|
||||
def test_pathname2url_nonascii(self):
|
||||
encoding = sys.getfilesystemencoding()
|
||||
errors = sys.getfilesystemencodeerrors()
|
||||
url = urllib.parse.quote(os_helper.FS_NONASCII, encoding=encoding, errors=errors)
|
||||
self.assertEqual(urllib.request.pathname2url(os_helper.FS_NONASCII), url)
|
||||
|
||||
@unittest.skipUnless(sys.platform == 'win32',
|
||||
'test specific to Windows pathnames.')
|
||||
def test_url2pathname_win(self):
|
||||
|
@ -1621,6 +1624,15 @@ class Pathname_Tests(unittest.TestCase):
|
|||
self.assertEqual(fn('////foo/bar'), '//foo/bar')
|
||||
self.assertEqual(fn('//localhost/foo/bar'), '//localhost/foo/bar')
|
||||
|
||||
@unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
|
||||
def test_url2pathname_nonascii(self):
|
||||
encoding = sys.getfilesystemencoding()
|
||||
errors = sys.getfilesystemencodeerrors()
|
||||
url = os_helper.FS_NONASCII
|
||||
self.assertEqual(urllib.request.url2pathname(url), os_helper.FS_NONASCII)
|
||||
url = urllib.parse.quote(url, encoding=encoding, errors=errors)
|
||||
self.assertEqual(urllib.request.url2pathname(url), os_helper.FS_NONASCII)
|
||||
|
||||
class Utility_Tests(unittest.TestCase):
|
||||
"""Testcase to test the various utility functions in the urllib."""
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue