From 51132da0c4dac13500d9bb86b2fdad42091d3fd9 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 17 Apr 2024 12:53:40 +0300 Subject: 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. --- Lib/posixpath.py | 2 +- Lib/test/test_posixpath.py | 11 +++++++++++ .../Library/2024-04-03-15-04-23.gh-issue-117503.NMfwup.rst | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2024-04-03-15-04-23.gh-issue-117503.NMfwup.rst diff --git a/Lib/posixpath.py b/Lib/posixpath.py index dd29fbb..11cbaca 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -262,7 +262,7 @@ def expanduser(path): return path name = path[1:i] if isinstance(name, bytes): - name = name.decode('ascii') + name = os.fsdecode(name) try: pwent = pwd.getpwnam(name) except KeyError: diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 7c122e6..604af5b 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -344,6 +344,17 @@ class PosixPathTest(unittest.TestCase): for path in ('~', '~/.local', '~vstinner/'): self.assertEqual(posixpath.expanduser(path), path) + @unittest.skipIf(sys.platform == "vxworks", + "no home directory on VxWorks") + def test_expanduser_pwd2(self): + pwd = import_helper.import_module('pwd') + for e in pwd.getpwall(): + name = e.pw_name + home = e.pw_dir + self.assertEqual(posixpath.expanduser('~' + name), home) + self.assertEqual(posixpath.expanduser(os.fsencode('~' + name)), + os.fsencode(home)) + NORMPATH_CASES = [ ("", "."), ("/", "/"), diff --git a/Misc/NEWS.d/next/Library/2024-04-03-15-04-23.gh-issue-117503.NMfwup.rst b/Misc/NEWS.d/next/Library/2024-04-03-15-04-23.gh-issue-117503.NMfwup.rst new file mode 100644 index 0000000..f0ea513 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-04-03-15-04-23.gh-issue-117503.NMfwup.rst @@ -0,0 +1,2 @@ +Fix support of non-ASCII user names in bytes paths in +:func:`os.path.expanduser` on Posix. -- cgit v0.12