summaryrefslogtreecommitdiffstats
path: root/Lib/distutils
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2018-12-18 15:17:56 (GMT)
committerGitHub <noreply@github.com>2018-12-18 15:17:56 (GMT)
commit17d0c0595e101c4ce76b58e55de37e6b5083e6cd (patch)
treee3e1374312f58fcd88dcd0c22df73f77cdf9d56c /Lib/distutils
parente6b247c8e524dbe5fc03b3492f628d0d5348bc49 (diff)
downloadcpython-17d0c0595e101c4ce76b58e55de37e6b5083e6cd.zip
cpython-17d0c0595e101c4ce76b58e55de37e6b5083e6cd.tar.gz
cpython-17d0c0595e101c4ce76b58e55de37e6b5083e6cd.tar.bz2
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.
Diffstat (limited to 'Lib/distutils')
-rw-r--r--Lib/distutils/tests/test_util.py34
-rw-r--r--Lib/distutils/util.py9
2 files changed, 32 insertions, 11 deletions
diff --git a/Lib/distutils/tests/test_util.py b/Lib/distutils/tests/test_util.py
index e2fc380..bf0d433 100644
--- a/Lib/distutils/tests/test_util.py
+++ b/Lib/distutils/tests/test_util.py
@@ -4,6 +4,7 @@ import sys
import unittest
from copy import copy
from test.support import run_unittest
+from unittest import mock
from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError
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):
util._environ_checked = 0
- if 'HOME' in os.environ:
- del os.environ['HOME']
+ os.environ.pop('HOME', None)
- # posix without HOME
- 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()
+ check_environ()
self.assertEqual(os.environ['PLAT'], get_platform())
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):
self.assertEqual(split_quoted('""one"" "two" \'three\' \\four'),
['one', 'two', 'three', 'four'])
diff --git a/Lib/distutils/util.py b/Lib/distutils/util.py
index 8368262..30a21e4 100644
--- a/Lib/distutils/util.py
+++ b/Lib/distutils/util.py
@@ -157,8 +157,13 @@ def check_environ ():
return
if os.name == 'posix' and 'HOME' not in os.environ:
- import pwd
- os.environ['HOME'] = pwd.getpwuid(os.getuid())[5]
+ try:
+ 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:
os.environ['PLAT'] = get_platform()