Skip to content

Commit 96b29b3

Browse files
[3.12] gh-117503: Fix support of non-ASCII user names in posixpath.expanduser() (GH-117504) (GH-117970)
They are now supported in bytes paths as well as in string paths. (cherry picked from commit 51132da)
1 parent ac48fde commit 96b29b3

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

Lib/posixpath.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def expanduser(path):
290290
return path
291291
name = path[1:i]
292292
if isinstance(name, bytes):
293-
name = str(name, 'ASCII')
293+
name = os.fsdecode(name)
294294
try:
295295
pwent = pwd.getpwnam(name)
296296
except KeyError:

Lib/test/test_posixpath.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,17 @@ def test_expanduser_pwd(self):
334334
for path in ('~', '~/.local', '~vstinner/'):
335335
self.assertEqual(posixpath.expanduser(path), path)
336336

337+
@unittest.skipIf(sys.platform == "vxworks",
338+
"no home directory on VxWorks")
339+
def test_expanduser_pwd2(self):
340+
pwd = import_helper.import_module('pwd')
341+
for e in pwd.getpwall():
342+
name = e.pw_name
343+
home = e.pw_dir
344+
self.assertEqual(posixpath.expanduser('~' + name), home)
345+
self.assertEqual(posixpath.expanduser(os.fsencode('~' + name)),
346+
os.fsencode(home))
347+
337348
NORMPATH_CASES = [
338349
("", "."),
339350
("/", "/"),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix support of non-ASCII user names in bytes paths in
2+
:func:`os.path.expanduser` on Posix.

0 commit comments

Comments
 (0)