Skip to content

Commit

Permalink
Merge branch '3.13' into backport-d6b3e78-3.13
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently authored Nov 20, 2024
2 parents 99044c2 + 746a0c5 commit 2ab72ad
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ jobs:
steps:
- name: Install Git
run: |
apt install git -yq
apt update && apt install git -yq
git config --global --add safe.directory "$GITHUB_WORKSPACE"
- uses: actions/checkout@v4
with:
Expand Down
20 changes: 16 additions & 4 deletions Lib/test/test_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,10 +709,6 @@ def tearDown(self):

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""):
Expand Down Expand Up @@ -1562,6 +1558,13 @@ def test_pathname2url_posix(self):
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):
Expand Down Expand Up @@ -1612,6 +1615,15 @@ def test_url2pathname_posix(self):
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."""

Expand Down
4 changes: 0 additions & 4 deletions Lib/test/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,10 +717,6 @@ def test_processors(self):


def sanepathname2url(path):
try:
path.encode("utf-8")
except UnicodeEncodeError:
raise unittest.SkipTest("path is not encodable to utf8")
urlpath = urllib.request.pathname2url(path)
if os.name == "nt" and urlpath.startswith("///"):
urlpath = urlpath[2:]
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 unquote(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(pathname)
encoding = sys.getfilesystemencoding()
errors = sys.getfilesystemencodeerrors()
return quote(pathname, encoding=encoding, errors=errors)


ftpcache = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix issue where :func:`urllib.request.url2pathname` and
:func:`~urllib.request.pathname2url` always used UTF-8 when quoting and
unquoting file URIs. They now use the :term:`filesystem encoding and error
handler`.

0 comments on commit 2ab72ad

Please sign in to comment.