Skip to content

Commit 51132da

Browse files
gh-117503: Fix support of non-ASCII user names in posixpath.expanduser() (GH-117504)
They are now supported in bytes paths as well as in string paths.
1 parent 44890b2 commit 51132da

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
@@ -262,7 +262,7 @@ def expanduser(path):
262262
return path
263263
name = path[1:i]
264264
if isinstance(name, bytes):
265-
name = name.decode('ascii')
265+
name = os.fsdecode(name)
266266
try:
267267
pwent = pwd.getpwnam(name)
268268
except KeyError:

Lib/test/test_posixpath.py

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

347+
@unittest.skipIf(sys.platform == "vxworks",
348+
"no home directory on VxWorks")
349+
def test_expanduser_pwd2(self):
350+
pwd = import_helper.import_module('pwd')
351+
for e in pwd.getpwall():
352+
name = e.pw_name
353+
home = e.pw_dir
354+
self.assertEqual(posixpath.expanduser('~' + name), home)
355+
self.assertEqual(posixpath.expanduser(os.fsencode('~' + name)),
356+
os.fsencode(home))
357+
347358
NORMPATH_CASES = [
348359
("", "."),
349360
("/", "/"),
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)