]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-117503: Fix support of non-ASCII user names in posixpath.expanduser() (GH-117504)
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 17 Apr 2024 09:53:40 +0000 (12:53 +0300)
committerGitHub <noreply@github.com>
Wed, 17 Apr 2024 09:53:40 +0000 (12:53 +0300)
They are now supported in bytes paths as well as in string paths.

Lib/posixpath.py
Lib/test/test_posixpath.py
Misc/NEWS.d/next/Library/2024-04-03-15-04-23.gh-issue-117503.NMfwup.rst [new file with mode: 0644]

index dd29fbb1614aa8007d3a6e37ed1d2d7c5eeb5e20..11cbaca53edb125467c6ea04d869995c6e49e130 100644 (file)
@@ -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:
index 7c122e6204b1005ad3abb7f25557272d911845ec..604af5bafa7f1367ac1d31bb526b4f7122ace8a8 100644 (file)
@@ -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 (file)
index 0000000..f0ea513
--- /dev/null
@@ -0,0 +1,2 @@
+Fix support of non-ASCII user names in bytes paths in
+:func:`os.path.expanduser` on Posix.