Skip to content

Commit 595b516

Browse files
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. (cherry picked from commit c45a2aa) Co-authored-by: Christoph Reiter <reiter.christoph@gmail.com>
1 parent 41f4dc3 commit 595b516

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

Lib/pathlib.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,7 @@ def make_uri(self, path):
253253
return 'file:' + urlquote_from_bytes(path.as_posix().encode('utf-8'))
254254

255255
def gethomedir(self, username):
256-
if 'HOME' in os.environ:
257-
userhome = os.environ['HOME']
258-
elif 'USERPROFILE' in os.environ:
256+
if 'USERPROFILE' in os.environ:
259257
userhome = os.environ['USERPROFILE']
260258
elif 'HOMEPATH' in os.environ:
261259
try:

Lib/test/test_pathlib.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,8 +1296,16 @@ def _test_home(self, p):
12961296
self.assertTrue(p.is_absolute())
12971297

12981298
def test_home(self):
1299-
p = self.cls.home()
1300-
self._test_home(p)
1299+
with support.EnvironmentVarGuard() as env:
1300+
self._test_home(self.cls.home())
1301+
1302+
env.clear()
1303+
env['USERPROFILE'] = os.path.join(BASE, 'userprofile')
1304+
self._test_home(self.cls.home())
1305+
1306+
# bpo-38883: ignore `HOME` when set on windows
1307+
env['HOME'] = os.path.join(BASE, 'home')
1308+
self._test_home(self.cls.home())
13011309

13021310
def test_samefile(self):
13031311
fileA_path = os.path.join(BASE, 'fileA')
@@ -2348,12 +2356,6 @@ def check():
23482356
self.assertEqual(p5.expanduser(), p5)
23492357
self.assertEqual(p6.expanduser(), p6)
23502358

2351-
# Test the first lookup key in the env vars.
2352-
env['HOME'] = 'C:\\Users\\alice'
2353-
check()
2354-
2355-
# Test that HOMEPATH is available instead.
2356-
env.pop('HOME', None)
23572359
env['HOMEPATH'] = 'C:\\Users\\alice'
23582360
check()
23592361

@@ -2366,6 +2368,10 @@ def check():
23662368
env['USERPROFILE'] = 'C:\\Users\\alice'
23672369
check()
23682370

2371+
# bpo-38883: ignore `HOME` when set on windows
2372+
env['HOME'] = 'C:\\Users\\eve'
2373+
check()
2374+
23692375

23702376
class CompatiblePathTest(unittest.TestCase):
23712377
"""
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:meth:`~pathlib.Path.home()` and :meth:`~pathlib.Path.expanduser()` on Windows
2+
now prefer :envvar:`USERPROFILE` and no longer use :envvar:`HOME`, which is not
3+
normally set for regular user accounts. This makes them again behave like
4+
:func:`os.path.expanduser`, which was changed to ignore :envvar:`HOME` in 3.8,
5+
see :issue:`36264`.

0 commit comments

Comments
 (0)