Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25ec4a4

Browse files
asottilezooba
authored andcommitted
bpo-36264: Don't honor POSIX HOME in os.path.expanduser on Windows (GH-12282)
1 parent 410aea1 commit 25ec4a4

6 files changed

Lines changed: 17 additions & 14 deletions

File tree

Lib/distutils/tests/test_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def setUp(self):
6060
super(BasePyPIRCCommandTestCase, self).setUp()
6161
self.tmp_dir = self.mkdtemp()
6262
os.environ['HOME'] = self.tmp_dir
63+
os.environ['USERPROFILE'] = self.tmp_dir
6364
self.rc = os.path.join(self.tmp_dir, '.pypirc')
6465
self.dist = Distribution()
6566

Lib/distutils/tests/test_dist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ def test_custom_pydistutils(self):
463463
# win32-style
464464
if sys.platform == 'win32':
465465
# home drive should be found
466-
os.environ['HOME'] = temp_dir
466+
os.environ['USERPROFILE'] = temp_dir
467467
files = dist.find_config_files()
468468
self.assertIn(user_filename, files,
469469
'%r not found in %r' % (user_filename, files))

Lib/ntpath.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,7 @@ def expanduser(path):
299299
while i < n and path[i] not in _get_bothseps(path):
300300
i += 1
301301

302-
if 'HOME' in os.environ:
303-
userhome = os.environ['HOME']
304-
elif 'USERPROFILE' in os.environ:
302+
if 'USERPROFILE' in os.environ:
305303
userhome = os.environ['USERPROFILE']
306304
elif not 'HOMEPATH' in os.environ:
307305
return path

Lib/test/test_netrc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def fake_expanduser(s):
154154
called.append(s)
155155
with support.EnvironmentVarGuard() as environ:
156156
environ.set('HOME', fake_home)
157+
environ.set('USERPROFILE', fake_home)
157158
result = orig_expanduser(s)
158159
return result
159160

Lib/test/test_ntpath.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,20 +262,21 @@ def test_expanduser(self):
262262
env['USERPROFILE'] = 'C:\\eric\\idle'
263263
tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
264264
tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
265-
266-
env.clear()
267-
env['HOME'] = 'C:\\idle\\eric'
268-
tester('ntpath.expanduser("~test")', 'C:\\idle\\test')
269-
tester('ntpath.expanduser("~")', 'C:\\idle\\eric')
270-
271265
tester('ntpath.expanduser("~test\\foo\\bar")',
272-
'C:\\idle\\test\\foo\\bar')
266+
'C:\\eric\\test\\foo\\bar')
273267
tester('ntpath.expanduser("~test/foo/bar")',
274-
'C:\\idle\\test/foo/bar')
268+
'C:\\eric\\test/foo/bar')
275269
tester('ntpath.expanduser("~\\foo\\bar")',
276-
'C:\\idle\\eric\\foo\\bar')
270+
'C:\\eric\\idle\\foo\\bar')
277271
tester('ntpath.expanduser("~/foo/bar")',
278-
'C:\\idle\\eric/foo/bar')
272+
'C:\\eric\\idle/foo/bar')
273+
274+
# bpo-36264: ignore `HOME` when set on windows
275+
env.clear()
276+
env['HOME'] = 'F:\\'
277+
env['USERPROFILE'] = 'C:\\eric\\idle'
278+
tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
279+
tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
279280

280281
@unittest.skipUnless(nt, "abspath requires 'nt' module")
281282
def test_abspath(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Don't honor POSIX ``HOME`` in ``os.path.expanduser`` on windows. Patch by
2+
Anthony Sottile.

0 commit comments

Comments
 (0)