Skip to content

Commit a6f57d3

Browse files
committed
Address review feedback
1 parent 11b9602 commit a6f57d3

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

Lib/test/test_urllib.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,13 +1557,13 @@ def test_pathname2url_posix(self):
15571557
self.assertEqual(fn('/'), '/')
15581558
self.assertEqual(fn('/a/b.c'), '/a/b.c')
15591559
self.assertEqual(fn('/a/b%#c'), '/a/b%25%23c')
1560-
try:
1561-
expect = os.fsencode('\xe9')
1562-
except UnicodeEncodeError:
1563-
pass
1564-
else:
1565-
expect = urllib.parse.quote_from_bytes(expect)
1566-
self.assertEqual(fn('\xe9'), expect)
1560+
1561+
@unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
1562+
def test_pathname2url_nonascii(self):
1563+
encoding = sys.getfilesystemencoding()
1564+
errors = sys.getfilesystemencodeerrors()
1565+
url = urllib.parse.quote(os_helper.FS_NONASCII, encoding=encoding, errors=errors)
1566+
self.assertEqual(urllib.request.pathname2url(os_helper.FS_NONASCII), url)
15671567

15681568
@unittest.skipUnless(sys.platform == 'win32',
15691569
'test specific to Windows pathnames.')
@@ -1614,12 +1614,13 @@ def test_url2pathname_posix(self):
16141614
self.assertEqual(fn('///foo/bar'), '/foo/bar')
16151615
self.assertEqual(fn('////foo/bar'), '//foo/bar')
16161616
self.assertEqual(fn('//localhost/foo/bar'), '//localhost/foo/bar')
1617-
try:
1618-
expect = os.fsdecode(b'\xe9')
1619-
except UnicodeDecodeError:
1620-
pass
1621-
else:
1622-
self.assertEqual(fn('%e9'), expect)
1617+
1618+
@unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
1619+
def test_url2pathname_nonascii(self):
1620+
encoding = sys.getfilesystemencoding()
1621+
errors = sys.getfilesystemencodeerrors()
1622+
url = urllib.parse.quote(os_helper.FS_NONASCII, encoding=encoding, errors=errors)
1623+
self.assertEqual(urllib.request.url2pathname(url), os_helper.FS_NONASCII)
16231624

16241625
class Utility_Tests(unittest.TestCase):
16251626
"""Testcase to test the various utility functions in the urllib."""

Lib/urllib/request.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,12 +1660,16 @@ def url2pathname(pathname):
16601660
# URL has an empty authority section, so the path begins on the
16611661
# third character.
16621662
pathname = pathname[2:]
1663-
return os.fsdecode(unquote_to_bytes(pathname))
1663+
encoding = sys.getfilesystemencoding()
1664+
errors = sys.getfilesystemencodeerrors()
1665+
return unquote(pathname, encoding=encoding, errors=errors)
16641666

16651667
def pathname2url(pathname):
16661668
"""OS-specific conversion from a file system path to a relative URL
16671669
of the 'file' scheme; not recommended for general use."""
1668-
return quote_from_bytes(os.fsencode(pathname))
1670+
encoding = sys.getfilesystemencoding()
1671+
errors = sys.getfilesystemencodeerrors()
1672+
return quote(pathname, encoding=encoding, errors=errors)
16691673

16701674

16711675
ftpcache = {}

0 commit comments

Comments
 (0)