Skip to content

Commit 983d1ab

Browse files
bpo-10496: posixpath.expanduser() catchs pwd.getpwuid() error (GH-10919)
* posixpath.expanduser() now returns the input path unchanged if the HOME environment variable is not set and pwd.getpwuid() raises KeyError (the current user identifier doesn't exist in the password database). * Add test_no_home_directory() to test_site. (cherry picked from commit f2f4555) Co-authored-by: Victor Stinner <vstinner@redhat.com>
1 parent 2d594f8 commit 983d1ab

4 files changed

Lines changed: 99 additions & 30 deletions

File tree

Lib/posixpath.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,12 @@ def expanduser(path):
246246
if i == 1:
247247
if 'HOME' not in os.environ:
248248
import pwd
249-
userhome = pwd.getpwuid(os.getuid()).pw_dir
249+
try:
250+
userhome = pwd.getpwuid(os.getuid()).pw_dir
251+
except KeyError:
252+
# bpo-10496: if the current user identifier doesn't exist in the
253+
# password database, return the path unchanged
254+
return path
250255
else:
251256
userhome = os.environ['HOME']
252257
else:
@@ -257,6 +262,8 @@ def expanduser(path):
257262
try:
258263
pwent = pwd.getpwnam(name)
259264
except KeyError:
265+
# bpo-10496: if the user name from the path doesn't exist in the
266+
# password database, return the path unchanged
260267
return path
261268
userhome = pwent.pw_dir
262269
if isinstance(path, bytes):

Lib/test/test_posixpath.py

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from posixpath import realpath, abspath, dirname, basename
66
from test import support, test_genericpath
77
from test.support import FakePath
8+
from unittest import mock
89

910
try:
1011
import posix
@@ -230,42 +231,61 @@ def fake_lstat(path):
230231
def test_expanduser(self):
231232
self.assertEqual(posixpath.expanduser("foo"), "foo")
232233
self.assertEqual(posixpath.expanduser(b"foo"), b"foo")
234+
235+
def test_expanduser_home_envvar(self):
233236
with support.EnvironmentVarGuard() as env:
237+
env['HOME'] = '/home/victor'
238+
self.assertEqual(posixpath.expanduser("~"), "/home/victor")
239+
240+
# expanduser() strips trailing slash
241+
env['HOME'] = '/home/victor/'
242+
self.assertEqual(posixpath.expanduser("~"), "/home/victor")
243+
234244
for home in '/', '', '//', '///':
235245
with self.subTest(home=home):
236246
env['HOME'] = home
237247
self.assertEqual(posixpath.expanduser("~"), "/")
238248
self.assertEqual(posixpath.expanduser("~/"), "/")
239249
self.assertEqual(posixpath.expanduser("~/foo"), "/foo")
240-
try:
241-
import pwd
242-
except ImportError:
243-
pass
244-
else:
245-
self.assertIsInstance(posixpath.expanduser("~/"), str)
246-
self.assertIsInstance(posixpath.expanduser(b"~/"), bytes)
247-
# if home directory == root directory, this test makes no sense
248-
if posixpath.expanduser("~") != '/':
249-
self.assertEqual(
250-
posixpath.expanduser("~") + "/",
251-
posixpath.expanduser("~/")
252-
)
253-
self.assertEqual(
254-
posixpath.expanduser(b"~") + b"/",
255-
posixpath.expanduser(b"~/")
256-
)
257-
self.assertIsInstance(posixpath.expanduser("~root/"), str)
258-
self.assertIsInstance(posixpath.expanduser("~foo/"), str)
259-
self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes)
260-
self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes)
261-
262-
with support.EnvironmentVarGuard() as env:
263-
# expanduser should fall back to using the password database
264-
del env['HOME']
265-
home = pwd.getpwuid(os.getuid()).pw_dir
266-
# $HOME can end with a trailing /, so strip it (see #17809)
267-
home = home.rstrip("/") or '/'
268-
self.assertEqual(posixpath.expanduser("~"), home)
250+
251+
def test_expanduser_pwd(self):
252+
pwd = support.import_module('pwd')
253+
254+
self.assertIsInstance(posixpath.expanduser("~/"), str)
255+
self.assertIsInstance(posixpath.expanduser(b"~/"), bytes)
256+
257+
# if home directory == root directory, this test makes no sense
258+
if posixpath.expanduser("~") != '/':
259+
self.assertEqual(
260+
posixpath.expanduser("~") + "/",
261+
posixpath.expanduser("~/")
262+
)
263+
self.assertEqual(
264+
posixpath.expanduser(b"~") + b"/",
265+
posixpath.expanduser(b"~/")
266+
)
267+
self.assertIsInstance(posixpath.expanduser("~root/"), str)
268+
self.assertIsInstance(posixpath.expanduser("~foo/"), str)
269+
self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes)
270+
self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes)
271+
272+
with support.EnvironmentVarGuard() as env:
273+
# expanduser should fall back to using the password database
274+
del env['HOME']
275+
276+
home = pwd.getpwuid(os.getuid()).pw_dir
277+
# $HOME can end with a trailing /, so strip it (see #17809)
278+
home = home.rstrip("/") or '/'
279+
self.assertEqual(posixpath.expanduser("~"), home)
280+
281+
# bpo-10496: If the HOME environment variable is not set and the
282+
# user (current identifier or name in the path) doesn't exist in
283+
# the password database (pwd.getuid() or pwd.getpwnam() fail),
284+
# expanduser() must return the path unchanged.
285+
with mock.patch.object(pwd, 'getpwuid', side_effect=KeyError), \
286+
mock.patch.object(pwd, 'getpwnam', side_effect=KeyError):
287+
for path in ('~', '~/.local', '~vstinner/'):
288+
self.assertEqual(posixpath.expanduser(path), path)
269289

270290
def test_normpath(self):
271291
self.assertEqual(posixpath.normpath(""), ".")

Lib/test/test_site.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
import unittest
88
import test.support
9+
from test import support
910
from test.support import (captured_stderr, TESTFN, EnvironmentVarGuard,
1011
change_cwd)
1112
import builtins
@@ -19,6 +20,7 @@
1920
import subprocess
2021
import sysconfig
2122
import tempfile
23+
from unittest import mock
2224
from copy import copy
2325

2426
# These tests are not particularly useful if Python was invoked with -S.
@@ -258,6 +260,7 @@ def test_getusersitepackages(self):
258260
# the call sets USER_BASE *and* USER_SITE
259261
self.assertEqual(site.USER_SITE, user_site)
260262
self.assertTrue(user_site.startswith(site.USER_BASE), user_site)
263+
self.assertEqual(site.USER_BASE, site.getuserbase())
261264

262265
def test_getsitepackages(self):
263266
site.PREFIXES = ['xoxo']
@@ -276,6 +279,40 @@ def test_getsitepackages(self):
276279
wanted = os.path.join('xoxo', 'lib', 'site-packages')
277280
self.assertEqual(dirs[1], wanted)
278281

282+
def test_no_home_directory(self):
283+
# bpo-10496: getuserbase() and getusersitepackages() must not fail if
284+
# the current user has no home directory (if expanduser() returns the
285+
# path unchanged).
286+
site.USER_SITE = None
287+
site.USER_BASE = None
288+
289+
with EnvironmentVarGuard() as environ, \
290+
mock.patch('os.path.expanduser', lambda path: path):
291+
292+
del environ['PYTHONUSERBASE']
293+
del environ['APPDATA']
294+
295+
user_base = site.getuserbase()
296+
self.assertTrue(user_base.startswith('~' + os.sep),
297+
user_base)
298+
299+
user_site = site.getusersitepackages()
300+
self.assertTrue(user_site.startswith(user_base), user_site)
301+
302+
with mock.patch('os.path.isdir', return_value=False) as mock_isdir, \
303+
mock.patch.object(site, 'addsitedir') as mock_addsitedir, \
304+
support.swap_attr(site, 'ENABLE_USER_SITE', True):
305+
306+
# addusersitepackages() must not add user_site to sys.path
307+
# if it is not an existing directory
308+
known_paths = set()
309+
site.addusersitepackages(known_paths)
310+
311+
mock_isdir.assert_called_once_with(user_site)
312+
mock_addsitedir.assert_not_called()
313+
self.assertFalse(known_paths)
314+
315+
279316
class PthFile(object):
280317
"""Helper class for handling testing of .pth files"""
281318

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:func:`posixpath.expanduser` now returns the input *path* unchanged if the
2+
``HOME`` environment variable is not set and the current user has no home
3+
directory (if the current user identifier doesn't exist in the password
4+
database). This change fix the :mod:`site` module if the current user doesn't
5+
exist in the password database (if the user has no home directory).

0 commit comments

Comments
 (0)