mirror of
https://github.com/python/cpython.git
synced 2025-08-01 15:43:13 +00:00
bpo-10496: distutils check_environ() handles getpwuid() error (GH-10931)
check_environ() of distutils.utils now catchs KeyError on calling pwd.getpwuid(): don't create the HOME environment variable in this case.
This commit is contained in:
parent
e6b247c8e5
commit
17d0c0595e
3 changed files with 35 additions and 11 deletions
|
@ -4,6 +4,7 @@ import sys
|
||||||
import unittest
|
import unittest
|
||||||
from copy import copy
|
from copy import copy
|
||||||
from test.support import run_unittest
|
from test.support import run_unittest
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError
|
from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError
|
||||||
from distutils.util import (get_platform, convert_path, change_root,
|
from distutils.util import (get_platform, convert_path, change_root,
|
||||||
|
@ -234,20 +235,35 @@ class UtilTestCase(support.EnvironGuard, unittest.TestCase):
|
||||||
|
|
||||||
def test_check_environ(self):
|
def test_check_environ(self):
|
||||||
util._environ_checked = 0
|
util._environ_checked = 0
|
||||||
if 'HOME' in os.environ:
|
os.environ.pop('HOME', None)
|
||||||
del os.environ['HOME']
|
|
||||||
|
|
||||||
# posix without HOME
|
check_environ()
|
||||||
if os.name == 'posix': # this test won't run on windows
|
|
||||||
check_environ()
|
|
||||||
import pwd
|
|
||||||
self.assertEqual(os.environ['HOME'], pwd.getpwuid(os.getuid())[5])
|
|
||||||
else:
|
|
||||||
check_environ()
|
|
||||||
|
|
||||||
self.assertEqual(os.environ['PLAT'], get_platform())
|
self.assertEqual(os.environ['PLAT'], get_platform())
|
||||||
self.assertEqual(util._environ_checked, 1)
|
self.assertEqual(util._environ_checked, 1)
|
||||||
|
|
||||||
|
@unittest.skipUnless(os.name == 'posix', 'specific to posix')
|
||||||
|
def test_check_environ_getpwuid(self):
|
||||||
|
util._environ_checked = 0
|
||||||
|
os.environ.pop('HOME', None)
|
||||||
|
|
||||||
|
import pwd
|
||||||
|
|
||||||
|
# only set pw_dir field, other fields are not used
|
||||||
|
result = pwd.struct_passwd((None, None, None, None, None,
|
||||||
|
'/home/distutils', None))
|
||||||
|
with mock.patch.object(pwd, 'getpwuid', return_value=result):
|
||||||
|
check_environ()
|
||||||
|
self.assertEqual(os.environ['HOME'], '/home/distutils')
|
||||||
|
|
||||||
|
util._environ_checked = 0
|
||||||
|
os.environ.pop('HOME', None)
|
||||||
|
|
||||||
|
# bpo-10496: Catch pwd.getpwuid() error
|
||||||
|
with mock.patch.object(pwd, 'getpwuid', side_effect=KeyError):
|
||||||
|
check_environ()
|
||||||
|
self.assertNotIn('HOME', os.environ)
|
||||||
|
|
||||||
def test_split_quoted(self):
|
def test_split_quoted(self):
|
||||||
self.assertEqual(split_quoted('""one"" "two" \'three\' \\four'),
|
self.assertEqual(split_quoted('""one"" "two" \'three\' \\four'),
|
||||||
['one', 'two', 'three', 'four'])
|
['one', 'two', 'three', 'four'])
|
||||||
|
|
|
@ -157,8 +157,13 @@ def check_environ ():
|
||||||
return
|
return
|
||||||
|
|
||||||
if os.name == 'posix' and 'HOME' not in os.environ:
|
if os.name == 'posix' and 'HOME' not in os.environ:
|
||||||
import pwd
|
try:
|
||||||
os.environ['HOME'] = pwd.getpwuid(os.getuid())[5]
|
import pwd
|
||||||
|
os.environ['HOME'] = pwd.getpwuid(os.getuid())[5]
|
||||||
|
except (ImportError, KeyError):
|
||||||
|
# bpo-10496: if the current user identifier doesn't exist in the
|
||||||
|
# password database, do nothing
|
||||||
|
pass
|
||||||
|
|
||||||
if 'PLAT' not in os.environ:
|
if 'PLAT' not in os.environ:
|
||||||
os.environ['PLAT'] = get_platform()
|
os.environ['PLAT'] = get_platform()
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
:func:`~distutils.utils.check_environ` of :mod:`distutils.utils` now catchs
|
||||||
|
:exc:`KeyError` on calling :func:`pwd.getpwuid`: don't create the ``HOME``
|
||||||
|
environment variable in this case.
|
Loading…
Add table
Add a link
Reference in a new issue