bpo-38883: Don't use POSIX `$HOME` in `pathlib.Path.home/expanduser` on Windows (GH-17961)

https://github.com/python/cpython/commit/c45a2aa9e255b5c7c211faa79f6b23895b6... commit: c45a2aa9e255b5c7c211faa79f6b23895b64ab27 branch: master author: Christoph Reiter <reiter.christoph@gmail.com> committer: Steve Dower <steve.dower@python.org> date: 2020-01-28T20:41:50+11:00 summary: bpo-38883: Don't use POSIX `$HOME` in `pathlib.Path.home/expanduser` on Windows (GH-17961) 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. files: A Misc/NEWS.d/next/Windows/2020-01-11-22-53-55.bpo-38883.X7FRaN.rst M Lib/pathlib.py M Lib/test/test_pathlib.py diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 7d1d1150b0da8..a5f3313902e1b 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -253,9 +253,7 @@ def make_uri(self, path): 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: diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 3232649f55477..a50dce0171808 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1383,8 +1383,16 @@ def _test_home(self, p): 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') @@ -2448,12 +2456,6 @@ def check(): 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() @@ -2466,6 +2468,10 @@ def check(): 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 index 0000000000000..c552e850a3684 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2020-01-11-22-53-55.bpo-38883.X7FRaN.rst @@ -0,0 +1,5 @@ +:meth:`~pathlib.Path.home()` and :meth:`~pathlib.Path.expanduser()` on Windows +now prefer :envvar:`USERPROFILE` and no longer use :envvar:`HOME`, which is not +normally set for regular user accounts. This makes them again behave like +:func:`os.path.expanduser`, which was changed to ignore :envvar:`HOME` in 3.8, +see :issue:`36264`.
participants (1)
-
Steve Dower