Skip to content

Commit c45a2aa

Browse files
lazkazooba
authored andcommitted
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.
1 parent 61f4db8 commit c45a2aa

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
@@ -1383,8 +1383,16 @@ def _test_home(self, p):
13831383
self.assertTrue(p.is_absolute())
13841384

13851385
def test_home(self):
1386-
p = self.cls.home()
1387-
self._test_home(p)
1386+
with support.EnvironmentVarGuard() as env:
1387+
self._test_home(self.cls.home())
1388+
1389+
env.clear()
1390+
env['USERPROFILE'] = os.path.join(BASE, 'userprofile')
1391+
self._test_home(self.cls.home())
1392+
1393+
# bpo-38883: ignore `HOME` when set on windows
1394+
env['HOME'] = os.path.join(BASE, 'home')
1395+
self._test_home(self.cls.home())
13881396

13891397
def test_samefile(self):
13901398
fileA_path = os.path.join(BASE, 'fileA')
@@ -2448,12 +2456,6 @@ def check():
24482456
self.assertEqual(p5.expanduser(), p5)
24492457
self.assertEqual(p6.expanduser(), p6)
24502458

2451-
# Test the first lookup key in the env vars.
2452-
env['HOME'] = 'C:\\Users\\alice'
2453-
check()
2454-
2455-
# Test that HOMEPATH is available instead.
2456-
env.pop('HOME', None)
24572459
env['HOMEPATH'] = 'C:\\Users\\alice'
24582460
check()
24592461

@@ -2466,6 +2468,10 @@ def check():
24662468
env['USERPROFILE'] = 'C:\\Users\\alice'
24672469
check()
24682470

2471+
# bpo-38883: ignore `HOME` when set on windows
2472+
env['HOME'] = 'C:\\Users\\eve'
2473+
check()
2474+
24692475

24702476
class CompatiblePathTest(unittest.TestCase):
24712477
"""
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)