gh-156713: Use the filesystem encoding in nturl2path (GH-156717) · python/cpython@01599e2 · GitHub
Skip to content

Commit 01599e2

Browse files
gh-156713: Use the filesystem encoding in nturl2path (GH-156717)
urllib.request.pathname2url() and url2pathname() use the filesystem encoding and error handler since gh-85168, but nturl2path, which implements them on Windows before 3.14, was left unchanged. Paths containing surrogate characters raised UnicodeEncodeError.
1 parent e7a3937 commit 01599e2

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

Lib/nturl2path.py

Lines changed: 11 additions & 3 deletions

Lib/test/test_nturl2path.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import sys
12
import unittest
3+
import urllib.parse
24

35
from test.support import warnings_helper
46

@@ -58,6 +60,15 @@ def test_pathname2url(self):
5860
for url in urls:
5961
self.assertEqual(fn(nturl2path.url2pathname(url)), url)
6062

63+
def test_pathname2url_surrogates(self):
64+
# gh-156713: the filesystem encoding and error handler are used,
65+
# so that paths containing surrogate characters can be converted.
66+
encoding = sys.getfilesystemencoding()
67+
errors = sys.getfilesystemencodeerrors()
68+
tail = urllib.parse.quote('a\udcff', encoding=encoding, errors=errors)
69+
self.assertEqual(nturl2path.pathname2url('C:\\a\udcff'),
70+
'///C:/' + tail)
71+
6172
def test_url2pathname(self):
6273
fn = nturl2path.url2pathname
6374
self.assertEqual(fn('/'), '\\')
@@ -103,5 +114,15 @@ def test_url2pathname(self):
103114
self.assertEqual(fn(nturl2path.pathname2url(path)), path)
104115

105116

117+
def test_url2pathname_surrogates(self):
118+
# gh-156713: the filesystem encoding and error handler are used, so
119+
# that URLs containing percent-encoded surrogates can be converted.
120+
encoding = sys.getfilesystemencoding()
121+
errors = sys.getfilesystemencodeerrors()
122+
url = urllib.parse.quote('a\udcff', encoding=encoding, errors=errors)
123+
self.assertEqual(nturl2path.url2pathname('///C:/' + url),
124+
'C:\\a\udcff')
125+
126+
106127
if __name__ == '__main__':
107128
unittest.main()
Lines changed: 4 additions & 0 deletions

0 commit comments

Comments
 (0)