]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38883: Don't use POSIX `$HOME` in `pathlib.Path.home/expanduser` on Windows ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 28 Jan 2020 09:59:43 +0000 (01:59 -0800)
committerGitHub <noreply@github.com>
Tue, 28 Jan 2020 09:59:43 +0000 (01:59 -0800)
In bpo-36264 os.path.expanduser was changed to ignore HOME on Windows.

Path.expanduser/home still honored HOME despite being documented as behaving the same
as os.path.expanduser. This makes them also ignore HOME so that both implementations
behave the same way again.
(cherry picked from commit c45a2aa9e255b5c7c211faa79f6b23895b64ab27)

Co-authored-by: Christoph Reiter <reiter.christoph@gmail.com>
Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/NEWS.d/next/Windows/2020-01-11-22-53-55.bpo-38883.X7FRaN.rst [new file with mode: 0644]

index 8ed3c883c60067dfe7af85c3edb9586009c153b0..015370a860e656f6bec1fe7c576cb81a10be0e04 100644 (file)
@@ -253,9 +253,7 @@ class _WindowsFlavour(_Flavour):
             return 'file:' + urlquote_from_bytes(path.as_posix().encode('utf-8'))
 
     def gethomedir(self, username):
-        if 'HOME' in os.environ:
-            userhome = os.environ['HOME']
-        elif 'USERPROFILE' in os.environ:
+        if 'USERPROFILE' in os.environ:
             userhome = os.environ['USERPROFILE']
         elif 'HOMEPATH' in os.environ:
             try:
index b127343982ca9aef0c1c2d1f7e7ac43b60b9c321..97fc5d8ad783829f502455952a3941648669ed71 100644 (file)
@@ -1296,8 +1296,16 @@ class _BasePathTest(object):
         self.assertTrue(p.is_absolute())
 
     def test_home(self):
-        p = self.cls.home()
-        self._test_home(p)
+        with support.EnvironmentVarGuard() as env:
+            self._test_home(self.cls.home())
+
+            env.clear()
+            env['USERPROFILE'] = os.path.join(BASE, 'userprofile')
+            self._test_home(self.cls.home())
+
+            # bpo-38883: ignore `HOME` when set on windows
+            env['HOME'] = os.path.join(BASE, 'home')
+            self._test_home(self.cls.home())
 
     def test_samefile(self):
         fileA_path = os.path.join(BASE, 'fileA')
@@ -2348,12 +2356,6 @@ class WindowsPathTest(_BasePathTest, unittest.TestCase):
                 self.assertEqual(p5.expanduser(), p5)
                 self.assertEqual(p6.expanduser(), p6)
 
-            # Test the first lookup key in the env vars.
-            env['HOME'] = 'C:\\Users\\alice'
-            check()
-
-            # Test that HOMEPATH is available instead.
-            env.pop('HOME', None)
             env['HOMEPATH'] = 'C:\\Users\\alice'
             check()
 
@@ -2366,6 +2368,10 @@ class WindowsPathTest(_BasePathTest, unittest.TestCase):
             env['USERPROFILE'] = 'C:\\Users\\alice'
             check()
 
+            # bpo-38883: ignore `HOME` when set on windows
+            env['HOME'] = 'C:\\Users\\eve'
+            check()
+
 
 class CompatiblePathTest(unittest.TestCase):
     """
diff --git a/Misc/NEWS.d/next/Windows/2020-01-11-22-53-55.bpo-38883.X7FRaN.rst b/Misc/NEWS.d/next/Windows/2020-01-11-22-53-55.bpo-38883.X7FRaN.rst
new file mode 100644 (file)
index 0000000..c552e85
--- /dev/null
@@ -0,0 +1,5 @@
+:meth:`~pathlib.Path.home()` and :meth:`~pathlib.Path.expanduser()` on Windows\r
+now prefer :envvar:`USERPROFILE` and no longer use :envvar:`HOME`, which is not\r
+normally set for regular user accounts. This makes them again behave like\r
+:func:`os.path.expanduser`, which was changed to ignore :envvar:`HOME` in 3.8,\r
+see :issue:`36264`.\r