]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-117503: Fix support of non-ASCII user names in posixpath.expanduser() ...
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 17 Apr 2024 10:43:37 +0000 (13:43 +0300)
committerGitHub <noreply@github.com>
Wed, 17 Apr 2024 10:43:37 +0000 (10:43 +0000)
They are now supported in bytes paths as well as in string paths.
(cherry picked from commit 51132da0c4dac13500d9bb86b2fdad42091d3fd9)

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 e4f155e41a32216c45c051feb5e64f9f17716158..bd81c09bb2bdd04110fd71b99371e63c80e8006d 100644 (file)
@@ -290,7 +290,7 @@ def expanduser(path):
             return path
         name = path[1:i]
         if isinstance(name, bytes):
-            name = str(name, 'ASCII')
+            name = os.fsdecode(name)
         try:
             pwent = pwd.getpwnam(name)
         except KeyError:
index 9be4640f970aeffc37d0e02b638cbadcfd448dd9..ddc097548542fc407c6291b5017fb617f58b5ece 100644 (file)
@@ -334,6 +334,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.