Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 14 additions & 8 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()

Expand All @@ -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):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.