Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Nov 15, 2024
1 parent 11b9602 commit a6f57d3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
27 changes: 14 additions & 13 deletions Lib/test/test_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1557,13 +1557,13 @@ def test_pathname2url_posix(self):
self.assertEqual(fn('/'), '/')
self.assertEqual(fn('/a/b.c'), '/a/b.c')
self.assertEqual(fn('/a/b%#c'), '/a/b%25%23c')
try:
expect = os.fsencode('\xe9')
except UnicodeEncodeError:
pass
else:
expect = urllib.parse.quote_from_bytes(expect)
self.assertEqual(fn('\xe9'), expect)

@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.')
Expand Down Expand Up @@ -1614,12 +1614,13 @@ def test_url2pathname_posix(self):
self.assertEqual(fn('///foo/bar'), '/foo/bar')
self.assertEqual(fn('////foo/bar'), '//foo/bar')
self.assertEqual(fn('//localhost/foo/bar'), '//localhost/foo/bar')
try:
expect = os.fsdecode(b'\xe9')
except UnicodeDecodeError:
pass
else:
self.assertEqual(fn('%e9'), expect)

@unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
def test_url2pathname_nonascii(self):
encoding = sys.getfilesystemencoding()
errors = sys.getfilesystemencodeerrors()
url = urllib.parse.quote(os_helper.FS_NONASCII, 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."""
Expand Down
8 changes: 6 additions & 2 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1660,12 +1660,16 @@ def url2pathname(pathname):
# URL has an empty authority section, so the path begins on the
# third character.
pathname = pathname[2:]
return os.fsdecode(unquote_to_bytes(pathname))
encoding = sys.getfilesystemencoding()
errors = sys.getfilesystemencodeerrors()
return unquote(pathname, encoding=encoding, errors=errors)

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


ftpcache = {}
Expand Down

0 comments on commit a6f57d3

Please sign in to comment.