Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 1 | """Tests for 'site'. |
| 2 | |
| 3 | Tests assume the initial paths in sys.path once the interpreter has begun |
| 4 | executing have not been removed. |
| 5 | |
| 6 | """ |
| 7 | import unittest |
Senthil Kumaran | 8ef519b | 2013-09-07 13:59:17 -0700 | [diff] [blame] | 8 | import test.support |
Victor Stinner | f2f4555 | 2018-12-05 16:49:35 +0100 | [diff] [blame] | 9 | from test import support |
Hai Shi | 79bb2c9 | 2020-08-06 19:51:29 +0800 | [diff] [blame] | 10 | from test.support import os_helper |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 11 | from test.support import socket_helper |
Hai Shi | c7decc2 | 2020-08-04 23:53:12 +0800 | [diff] [blame] | 12 | from test.support import captured_stderr |
| 13 | from test.support.os_helper import TESTFN, EnvironmentVarGuard, change_cwd |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 14 | import builtins |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 15 | import encodings |
Victor Stinner | d18de46 | 2020-03-18 18:27:32 +0100 | [diff] [blame] | 16 | import glob |
native-api | 2145c8c | 2020-06-12 09:20:11 +0300 | [diff] [blame] | 17 | import io |
Victor Stinner | d18de46 | 2020-03-18 18:27:32 +0100 | [diff] [blame] | 18 | import os |
| 19 | import re |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 20 | import shutil |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 21 | import subprocess |
Victor Stinner | d18de46 | 2020-03-18 18:27:32 +0100 | [diff] [blame] | 22 | import sys |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 23 | import sysconfig |
Zachary Ware | d48214f | 2017-05-14 15:49:46 -0500 | [diff] [blame] | 24 | import tempfile |
Victor Stinner | d18de46 | 2020-03-18 18:27:32 +0100 | [diff] [blame] | 25 | import urllib.error |
| 26 | import urllib.request |
Victor Stinner | f2f4555 | 2018-12-05 16:49:35 +0100 | [diff] [blame] | 27 | from unittest import mock |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 28 | from copy import copy |
| 29 | |
Zachary Ware | 36193e7 | 2013-12-11 16:59:44 -0600 | [diff] [blame] | 30 | # These tests are not particularly useful if Python was invoked with -S. |
| 31 | # If you add tests that are useful under -S, this skip should be moved |
| 32 | # to the class level. |
| 33 | if sys.flags.no_site: |
| 34 | raise unittest.SkipTest("Python was invoked with -S") |
| 35 | |
| 36 | import site |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 37 | |
Victor Stinner | b85c136 | 2017-04-20 13:39:39 +0200 | [diff] [blame] | 38 | |
| 39 | OLD_SYS_PATH = None |
| 40 | |
| 41 | |
| 42 | def setUpModule(): |
| 43 | global OLD_SYS_PATH |
| 44 | OLD_SYS_PATH = sys.path[:] |
| 45 | |
| 46 | if site.ENABLE_USER_SITE and not os.path.isdir(site.USER_SITE): |
| 47 | # need to add user site directory for tests |
| 48 | try: |
| 49 | os.makedirs(site.USER_SITE) |
| 50 | # modify sys.path: will be restored by tearDownModule() |
| 51 | site.addsitedir(site.USER_SITE) |
| 52 | except PermissionError as exc: |
| 53 | raise unittest.SkipTest('unable to create user site directory (%r): %s' |
| 54 | % (site.USER_SITE, exc)) |
| 55 | |
| 56 | |
| 57 | def tearDownModule(): |
| 58 | sys.path[:] = OLD_SYS_PATH |
Victor Stinner | 21d0e1b | 2016-03-14 17:47:03 +0100 | [diff] [blame] | 59 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 60 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 61 | class HelperFunctionsTests(unittest.TestCase): |
| 62 | """Tests for helper functions. |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 63 | """ |
| 64 | |
| 65 | def setUp(self): |
| 66 | """Save a copy of sys.path""" |
| 67 | self.sys_path = sys.path[:] |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 68 | self.old_base = site.USER_BASE |
| 69 | self.old_site = site.USER_SITE |
| 70 | self.old_prefixes = site.PREFIXES |
Brett Cannon | 8ac95ee | 2012-04-04 17:31:16 -0400 | [diff] [blame] | 71 | self.original_vars = sysconfig._CONFIG_VARS |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 72 | self.old_vars = copy(sysconfig._CONFIG_VARS) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 73 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 74 | def tearDown(self): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 75 | """Restore sys.path""" |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 76 | sys.path[:] = self.sys_path |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 77 | site.USER_BASE = self.old_base |
| 78 | site.USER_SITE = self.old_site |
| 79 | site.PREFIXES = self.old_prefixes |
Brett Cannon | 8ac95ee | 2012-04-04 17:31:16 -0400 | [diff] [blame] | 80 | sysconfig._CONFIG_VARS = self.original_vars |
| 81 | sysconfig._CONFIG_VARS.clear() |
| 82 | sysconfig._CONFIG_VARS.update(self.old_vars) |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 83 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 84 | def test_makepath(self): |
| 85 | # Test makepath() have an absolute path for its first return value |
| 86 | # and a case-normalized version of the absolute path for its |
| 87 | # second value. |
| 88 | path_parts = ("Beginning", "End") |
| 89 | original_dir = os.path.join(*path_parts) |
| 90 | abs_dir, norm_dir = site.makepath(*path_parts) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 91 | self.assertEqual(os.path.abspath(original_dir), abs_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 92 | if original_dir == os.path.normcase(original_dir): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 93 | self.assertEqual(abs_dir, norm_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 94 | else: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 95 | self.assertEqual(os.path.normcase(abs_dir), norm_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 96 | |
| 97 | def test_init_pathinfo(self): |
| 98 | dir_set = site._init_pathinfo() |
| 99 | for entry in [site.makepath(path)[1] for path in sys.path |
Brett Cannon | 5f0507d | 2016-04-08 15:04:28 -0700 | [diff] [blame] | 100 | if path and os.path.exists(path)]: |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 101 | self.assertIn(entry, dir_set, |
| 102 | "%s from sys.path not found in set returned " |
| 103 | "by _init_pathinfo(): %s" % (entry, dir_set)) |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 104 | |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 105 | def pth_file_tests(self, pth_file): |
| 106 | """Contain common code for testing results of reading a .pth file""" |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 107 | self.assertIn(pth_file.imported, sys.modules, |
| 108 | "%s not in sys.modules" % pth_file.imported) |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 109 | self.assertIn(site.makepath(pth_file.good_dir_path)[0], sys.path) |
| 110 | self.assertFalse(os.path.exists(pth_file.bad_dir_path)) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 111 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 112 | def test_addpackage(self): |
| 113 | # Make sure addpackage() imports if the line starts with 'import', |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 114 | # adds directories to sys.path for any line in the file that is not a |
| 115 | # comment or import that is a valid directory name for where the .pth |
| 116 | # file resides; invalid directories are not added |
| 117 | pth_file = PthFile() |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 118 | pth_file.cleanup(prep=True) # to make sure that nothing is |
| 119 | # pre-existing that shouldn't be |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 120 | try: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 121 | pth_file.create() |
| 122 | site.addpackage(pth_file.base_dir, pth_file.filename, set()) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 123 | self.pth_file_tests(pth_file) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 124 | finally: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 125 | pth_file.cleanup() |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 126 | |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 127 | def make_pth(self, contents, pth_dir='.', pth_name=TESTFN): |
| 128 | # Create a .pth file and return its (abspath, basename). |
| 129 | pth_dir = os.path.abspath(pth_dir) |
| 130 | pth_basename = pth_name + '.pth' |
| 131 | pth_fn = os.path.join(pth_dir, pth_basename) |
Serhiy Storchaka | 5b10b98 | 2019-03-05 10:06:26 +0200 | [diff] [blame] | 132 | with open(pth_fn, 'w', encoding='utf-8') as pth_file: |
| 133 | self.addCleanup(lambda: os.remove(pth_fn)) |
| 134 | pth_file.write(contents) |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 135 | return pth_dir, pth_basename |
| 136 | |
| 137 | def test_addpackage_import_bad_syntax(self): |
| 138 | # Issue 10642 |
Serhiy Storchaka | 94cf308 | 2018-12-17 17:34:14 +0200 | [diff] [blame] | 139 | pth_dir, pth_fn = self.make_pth("import bad-syntax\n") |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 140 | with captured_stderr() as err_out: |
| 141 | site.addpackage(pth_dir, pth_fn, set()) |
| 142 | self.assertRegex(err_out.getvalue(), "line 1") |
R. David Murray | ab9d8d6 | 2010-12-27 00:03:13 +0000 | [diff] [blame] | 143 | self.assertRegex(err_out.getvalue(), |
| 144 | re.escape(os.path.join(pth_dir, pth_fn))) |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 145 | # XXX: the previous two should be independent checks so that the |
| 146 | # order doesn't matter. The next three could be a single check |
| 147 | # but my regex foo isn't good enough to write it. |
| 148 | self.assertRegex(err_out.getvalue(), 'Traceback') |
Serhiy Storchaka | 94cf308 | 2018-12-17 17:34:14 +0200 | [diff] [blame] | 149 | self.assertRegex(err_out.getvalue(), r'import bad-syntax') |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 150 | self.assertRegex(err_out.getvalue(), 'SyntaxError') |
| 151 | |
| 152 | def test_addpackage_import_bad_exec(self): |
| 153 | # Issue 10642 |
| 154 | pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n") |
| 155 | with captured_stderr() as err_out: |
| 156 | site.addpackage(pth_dir, pth_fn, set()) |
| 157 | self.assertRegex(err_out.getvalue(), "line 2") |
R. David Murray | ab9d8d6 | 2010-12-27 00:03:13 +0000 | [diff] [blame] | 158 | self.assertRegex(err_out.getvalue(), |
| 159 | re.escape(os.path.join(pth_dir, pth_fn))) |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 160 | # XXX: ditto previous XXX comment. |
| 161 | self.assertRegex(err_out.getvalue(), 'Traceback') |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 162 | self.assertRegex(err_out.getvalue(), 'ModuleNotFoundError') |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 163 | |
idomic | 0c71a66 | 2020-09-19 15:13:29 -0400 | [diff] [blame] | 164 | def test_addpackage_empty_lines(self): |
| 165 | # Issue 33689 |
| 166 | pth_dir, pth_fn = self.make_pth("\n\n \n\n") |
| 167 | known_paths = site.addpackage(pth_dir, pth_fn, set()) |
| 168 | self.assertEqual(known_paths, set()) |
| 169 | |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 170 | def test_addpackage_import_bad_pth_file(self): |
| 171 | # Issue 5258 |
| 172 | pth_dir, pth_fn = self.make_pth("abc\x00def\n") |
| 173 | with captured_stderr() as err_out: |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 174 | self.assertFalse(site.addpackage(pth_dir, pth_fn, set())) |
| 175 | self.assertEqual(err_out.getvalue(), "") |
| 176 | for path in sys.path: |
| 177 | if isinstance(path, str): |
| 178 | self.assertNotIn("abc\x00def", path) |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 179 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 180 | def test_addsitedir(self): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 181 | # Same tests for test_addpackage since addsitedir() essentially just |
| 182 | # calls addpackage() for every .pth file in the directory |
| 183 | pth_file = PthFile() |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 184 | pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing |
| 185 | # that is tested for |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 186 | try: |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 187 | pth_file.create() |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 188 | site.addsitedir(pth_file.base_dir, set()) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 189 | self.pth_file_tests(pth_file) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 190 | finally: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 191 | pth_file.cleanup() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 192 | |
native-api | 45d8d24 | 2019-03-03 19:05:19 +0300 | [diff] [blame] | 193 | # This tests _getuserbase, hence the double underline |
| 194 | # to distinguish from a test for getuserbase |
| 195 | def test__getuserbase(self): |
INADA Naoki | a8f8d5b | 2017-06-29 00:31:53 +0900 | [diff] [blame] | 196 | self.assertEqual(site._getuserbase(), sysconfig._getuserbase()) |
| 197 | |
| 198 | def test_get_path(self): |
INADA Naoki | ba9ddb7 | 2017-07-28 21:28:19 +0900 | [diff] [blame] | 199 | if sys.platform == 'darwin' and sys._framework: |
| 200 | scheme = 'osx_framework_user' |
| 201 | else: |
| 202 | scheme = os.name + '_user' |
INADA Naoki | a8f8d5b | 2017-06-29 00:31:53 +0900 | [diff] [blame] | 203 | self.assertEqual(site._get_path(site._getuserbase()), |
INADA Naoki | ba9ddb7 | 2017-07-28 21:28:19 +0900 | [diff] [blame] | 204 | sysconfig.get_path('purelib', scheme)) |
INADA Naoki | a8f8d5b | 2017-06-29 00:31:53 +0900 | [diff] [blame] | 205 | |
Ned Deily | 316f573 | 2011-10-31 16:16:35 -0700 | [diff] [blame] | 206 | @unittest.skipUnless(site.ENABLE_USER_SITE, "requires access to PEP 370 " |
| 207 | "user-site (site.ENABLE_USER_SITE)") |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 208 | def test_s_option(self): |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 209 | # (ncoghlan) Change this to use script_helper... |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 210 | usersite = site.USER_SITE |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 211 | self.assertIn(usersite, sys.path) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 212 | |
Éric Araujo | 63ebe1c | 2011-01-03 17:51:11 +0000 | [diff] [blame] | 213 | env = os.environ.copy() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 214 | rc = subprocess.call([sys.executable, '-c', |
Éric Araujo | 63ebe1c | 2011-01-03 17:51:11 +0000 | [diff] [blame] | 215 | 'import sys; sys.exit(%r in sys.path)' % usersite], |
| 216 | env=env) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 217 | self.assertEqual(rc, 1) |
| 218 | |
Éric Araujo | 63ebe1c | 2011-01-03 17:51:11 +0000 | [diff] [blame] | 219 | env = os.environ.copy() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 220 | rc = subprocess.call([sys.executable, '-s', '-c', |
Éric Araujo | 63ebe1c | 2011-01-03 17:51:11 +0000 | [diff] [blame] | 221 | 'import sys; sys.exit(%r in sys.path)' % usersite], |
| 222 | env=env) |
Antoine Pitrou | a1782e1 | 2013-10-23 22:03:22 +0200 | [diff] [blame] | 223 | if usersite == site.getsitepackages()[0]: |
| 224 | self.assertEqual(rc, 1) |
| 225 | else: |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 226 | self.assertEqual(rc, 0, "User site still added to path with -s") |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 227 | |
| 228 | env = os.environ.copy() |
| 229 | env["PYTHONNOUSERSITE"] = "1" |
| 230 | rc = subprocess.call([sys.executable, '-c', |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 231 | 'import sys; sys.exit(%r in sys.path)' % usersite], |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 232 | env=env) |
Antoine Pitrou | a1782e1 | 2013-10-23 22:03:22 +0200 | [diff] [blame] | 233 | if usersite == site.getsitepackages()[0]: |
| 234 | self.assertEqual(rc, 1) |
| 235 | else: |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 236 | self.assertEqual(rc, 0, |
| 237 | "User site still added to path with PYTHONNOUSERSITE") |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 238 | |
| 239 | env = os.environ.copy() |
| 240 | env["PYTHONUSERBASE"] = "/tmp" |
| 241 | rc = subprocess.call([sys.executable, '-c', |
| 242 | 'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'], |
| 243 | env=env) |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 244 | self.assertEqual(rc, 1, |
| 245 | "User base not set by PYTHONUSERBASE") |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 246 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 247 | def test_getuserbase(self): |
| 248 | site.USER_BASE = None |
| 249 | user_base = site.getuserbase() |
| 250 | |
| 251 | # the call sets site.USER_BASE |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 252 | self.assertEqual(site.USER_BASE, user_base) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 253 | |
| 254 | # let's set PYTHONUSERBASE and see if it uses it |
| 255 | site.USER_BASE = None |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 256 | import sysconfig |
| 257 | sysconfig._CONFIG_VARS = None |
| 258 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 259 | with EnvironmentVarGuard() as environ: |
| 260 | environ['PYTHONUSERBASE'] = 'xoxo' |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 261 | self.assertTrue(site.getuserbase().startswith('xoxo'), |
| 262 | site.getuserbase()) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 263 | |
| 264 | def test_getusersitepackages(self): |
| 265 | site.USER_SITE = None |
| 266 | site.USER_BASE = None |
| 267 | user_site = site.getusersitepackages() |
| 268 | |
| 269 | # the call sets USER_BASE *and* USER_SITE |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 270 | self.assertEqual(site.USER_SITE, user_site) |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 271 | self.assertTrue(user_site.startswith(site.USER_BASE), user_site) |
Victor Stinner | f2f4555 | 2018-12-05 16:49:35 +0100 | [diff] [blame] | 272 | self.assertEqual(site.USER_BASE, site.getuserbase()) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 273 | |
| 274 | def test_getsitepackages(self): |
| 275 | site.PREFIXES = ['xoxo'] |
| 276 | dirs = site.getsitepackages() |
Ned Deily | 763f094 | 2018-01-30 05:14:09 -0500 | [diff] [blame] | 277 | if os.sep == '/': |
| 278 | # OS X, Linux, FreeBSD, etc |
Victor Stinner | 8510f43 | 2020-03-10 09:53:09 +0100 | [diff] [blame] | 279 | if sys.platlibdir != "lib": |
| 280 | self.assertEqual(len(dirs), 2) |
| 281 | wanted = os.path.join('xoxo', sys.platlibdir, |
| 282 | 'python%d.%d' % sys.version_info[:2], |
| 283 | 'site-packages') |
| 284 | self.assertEqual(dirs[0], wanted) |
| 285 | else: |
| 286 | self.assertEqual(len(dirs), 1) |
Serhiy Storchaka | 885bdc4 | 2016-02-11 13:10:36 +0200 | [diff] [blame] | 287 | wanted = os.path.join('xoxo', 'lib', |
| 288 | 'python%d.%d' % sys.version_info[:2], |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 289 | 'site-packages') |
Victor Stinner | 8510f43 | 2020-03-10 09:53:09 +0100 | [diff] [blame] | 290 | self.assertEqual(dirs[-1], wanted) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 291 | else: |
Ned Deily | d531b29 | 2012-02-06 00:58:18 +0100 | [diff] [blame] | 292 | # other platforms |
Ezio Melotti | fc8b205 | 2010-08-17 08:35:41 +0000 | [diff] [blame] | 293 | self.assertEqual(len(dirs), 2) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 294 | self.assertEqual(dirs[0], 'xoxo') |
Tarek Ziadé | 8c0e217 | 2009-10-27 21:24:21 +0000 | [diff] [blame] | 295 | wanted = os.path.join('xoxo', 'lib', 'site-packages') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 296 | self.assertEqual(dirs[1], wanted) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 297 | |
Victor Stinner | f2f4555 | 2018-12-05 16:49:35 +0100 | [diff] [blame] | 298 | def test_no_home_directory(self): |
| 299 | # bpo-10496: getuserbase() and getusersitepackages() must not fail if |
| 300 | # the current user has no home directory (if expanduser() returns the |
| 301 | # path unchanged). |
| 302 | site.USER_SITE = None |
| 303 | site.USER_BASE = None |
| 304 | |
| 305 | with EnvironmentVarGuard() as environ, \ |
| 306 | mock.patch('os.path.expanduser', lambda path: path): |
| 307 | |
| 308 | del environ['PYTHONUSERBASE'] |
| 309 | del environ['APPDATA'] |
| 310 | |
| 311 | user_base = site.getuserbase() |
| 312 | self.assertTrue(user_base.startswith('~' + os.sep), |
| 313 | user_base) |
| 314 | |
| 315 | user_site = site.getusersitepackages() |
| 316 | self.assertTrue(user_site.startswith(user_base), user_site) |
| 317 | |
| 318 | with mock.patch('os.path.isdir', return_value=False) as mock_isdir, \ |
| 319 | mock.patch.object(site, 'addsitedir') as mock_addsitedir, \ |
| 320 | support.swap_attr(site, 'ENABLE_USER_SITE', True): |
| 321 | |
| 322 | # addusersitepackages() must not add user_site to sys.path |
| 323 | # if it is not an existing directory |
| 324 | known_paths = set() |
| 325 | site.addusersitepackages(known_paths) |
| 326 | |
| 327 | mock_isdir.assert_called_once_with(user_site) |
| 328 | mock_addsitedir.assert_not_called() |
| 329 | self.assertFalse(known_paths) |
| 330 | |
native-api | 2145c8c | 2020-06-12 09:20:11 +0300 | [diff] [blame] | 331 | def test_trace(self): |
| 332 | message = "bla-bla-bla" |
| 333 | for verbose, out in (True, message + "\n"), (False, ""): |
| 334 | with mock.patch('sys.flags', mock.Mock(verbose=verbose)), \ |
| 335 | mock.patch('sys.stderr', io.StringIO()): |
| 336 | site._trace(message) |
| 337 | self.assertEqual(sys.stderr.getvalue(), out) |
| 338 | |
Victor Stinner | f2f4555 | 2018-12-05 16:49:35 +0100 | [diff] [blame] | 339 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 340 | class PthFile(object): |
| 341 | """Helper class for handling testing of .pth files""" |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 342 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 343 | def __init__(self, filename_base=TESTFN, imported="time", |
| 344 | good_dirname="__testdir__", bad_dirname="__bad"): |
| 345 | """Initialize instance variables""" |
| 346 | self.filename = filename_base + ".pth" |
| 347 | self.base_dir = os.path.abspath('') |
| 348 | self.file_path = os.path.join(self.base_dir, self.filename) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 349 | self.imported = imported |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 350 | self.good_dirname = good_dirname |
| 351 | self.bad_dirname = bad_dirname |
| 352 | self.good_dir_path = os.path.join(self.base_dir, self.good_dirname) |
| 353 | self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 354 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 355 | def create(self): |
| 356 | """Create a .pth file with a comment, blank lines, an ``import |
| 357 | <self.imported>``, a line with self.good_dirname, and a line with |
| 358 | self.bad_dirname. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 359 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 360 | Creation of the directory for self.good_dir_path (based off of |
| 361 | self.good_dirname) is also performed. |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 362 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 363 | Make sure to call self.cleanup() to undo anything done by this method. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 364 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 365 | """ |
Michael W. Hudson | ff52286 | 2005-05-27 14:58:06 +0000 | [diff] [blame] | 366 | FILE = open(self.file_path, 'w') |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 367 | try: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 368 | print("#import @bad module name", file=FILE) |
| 369 | print("\n", file=FILE) |
| 370 | print("import %s" % self.imported, file=FILE) |
| 371 | print(self.good_dirname, file=FILE) |
| 372 | print(self.bad_dirname, file=FILE) |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 373 | finally: |
| 374 | FILE.close() |
| 375 | os.mkdir(self.good_dir_path) |
| 376 | |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 377 | def cleanup(self, prep=False): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 378 | """Make sure that the .pth file is deleted, self.imported is not in |
| 379 | sys.modules, and that both self.good_dirname and self.bad_dirname are |
| 380 | not existing directories.""" |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 381 | if os.path.exists(self.file_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 382 | os.remove(self.file_path) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 383 | if prep: |
| 384 | self.imported_module = sys.modules.get(self.imported) |
| 385 | if self.imported_module: |
| 386 | del sys.modules[self.imported] |
| 387 | else: |
| 388 | if self.imported_module: |
| 389 | sys.modules[self.imported] = self.imported_module |
| 390 | if os.path.exists(self.good_dir_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 391 | os.rmdir(self.good_dir_path) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 392 | if os.path.exists(self.bad_dir_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 393 | os.rmdir(self.bad_dir_path) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 394 | |
| 395 | class ImportSideEffectTests(unittest.TestCase): |
| 396 | """Test side-effects from importing 'site'.""" |
| 397 | |
| 398 | def setUp(self): |
| 399 | """Make a copy of sys.path""" |
| 400 | self.sys_path = sys.path[:] |
| 401 | |
| 402 | def tearDown(self): |
| 403 | """Restore sys.path""" |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 404 | sys.path[:] = self.sys_path |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 405 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 406 | def test_abs_paths(self): |
| 407 | # Make sure all imported modules have their __file__ and __cached__ |
| 408 | # attributes as absolute paths. Arranging to put the Lib directory on |
| 409 | # PYTHONPATH would cause the os module to have a relative path for |
| 410 | # __file__ if abs_paths() does not get run. sys and builtins (the |
| 411 | # only other modules imported before site.py runs) do not have |
| 412 | # __file__ or __cached__ because they are built-in. |
Steve Dower | f14c28f | 2018-09-20 13:38:34 -0700 | [diff] [blame] | 413 | try: |
| 414 | parent = os.path.relpath(os.path.dirname(os.__file__)) |
| 415 | cwd = os.getcwd() |
| 416 | except ValueError: |
| 417 | # Failure to get relpath probably means we need to chdir |
| 418 | # to the same drive. |
| 419 | cwd, parent = os.path.split(os.path.dirname(os.__file__)) |
| 420 | with change_cwd(cwd): |
| 421 | env = os.environ.copy() |
| 422 | env['PYTHONPATH'] = parent |
| 423 | code = ('import os, sys', |
| 424 | # use ASCII to avoid locale issues with non-ASCII directories |
| 425 | 'os_file = os.__file__.encode("ascii", "backslashreplace")', |
| 426 | r'sys.stdout.buffer.write(os_file + b"\n")', |
| 427 | 'os_cached = os.__cached__.encode("ascii", "backslashreplace")', |
| 428 | r'sys.stdout.buffer.write(os_cached + b"\n")') |
| 429 | command = '\n'.join(code) |
| 430 | # First, prove that with -S (no 'import site'), the paths are |
| 431 | # relative. |
| 432 | proc = subprocess.Popen([sys.executable, '-S', '-c', command], |
| 433 | env=env, |
| 434 | stdout=subprocess.PIPE) |
| 435 | stdout, stderr = proc.communicate() |
Victor Stinner | f3bc258 | 2010-04-18 07:59:53 +0000 | [diff] [blame] | 436 | |
Steve Dower | f14c28f | 2018-09-20 13:38:34 -0700 | [diff] [blame] | 437 | self.assertEqual(proc.returncode, 0) |
| 438 | os__file__, os__cached__ = stdout.splitlines()[:2] |
| 439 | self.assertFalse(os.path.isabs(os__file__)) |
| 440 | self.assertFalse(os.path.isabs(os__cached__)) |
| 441 | # Now, with 'import site', it works. |
| 442 | proc = subprocess.Popen([sys.executable, '-c', command], |
| 443 | env=env, |
| 444 | stdout=subprocess.PIPE) |
| 445 | stdout, stderr = proc.communicate() |
| 446 | self.assertEqual(proc.returncode, 0) |
| 447 | os__file__, os__cached__ = stdout.splitlines()[:2] |
| 448 | self.assertTrue(os.path.isabs(os__file__), |
| 449 | "expected absolute path, got {}" |
| 450 | .format(os__file__.decode('ascii'))) |
| 451 | self.assertTrue(os.path.isabs(os__cached__), |
| 452 | "expected absolute path, got {}" |
| 453 | .format(os__cached__.decode('ascii'))) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 454 | |
INADA Naoki | d4c76d9 | 2018-10-01 21:10:37 +0900 | [diff] [blame] | 455 | def test_abs_paths_cached_None(self): |
| 456 | """Test for __cached__ is None. |
| 457 | |
| 458 | Regarding to PEP 3147, __cached__ can be None. |
| 459 | |
| 460 | See also: https://bugs.python.org/issue30167 |
| 461 | """ |
| 462 | sys.modules['test'].__cached__ = None |
| 463 | site.abs_paths() |
| 464 | self.assertIsNone(sys.modules['test'].__cached__) |
| 465 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 466 | def test_no_duplicate_paths(self): |
| 467 | # No duplicate paths should exist in sys.path |
| 468 | # Handled by removeduppaths() |
| 469 | site.removeduppaths() |
| 470 | seen_paths = set() |
| 471 | for path in sys.path: |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 472 | self.assertNotIn(path, seen_paths) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 473 | seen_paths.add(path) |
| 474 | |
Zachary Ware | 9fe6d86 | 2013-12-08 00:20:35 -0600 | [diff] [blame] | 475 | @unittest.skip('test not implemented') |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 476 | def test_add_build_dir(self): |
| 477 | # Test that the build directory's Modules directory is used when it |
| 478 | # should be. |
| 479 | # XXX: implement |
| 480 | pass |
| 481 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 482 | def test_setting_quit(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 483 | # 'quit' and 'exit' should be injected into builtins |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 484 | self.assertTrue(hasattr(builtins, "quit")) |
| 485 | self.assertTrue(hasattr(builtins, "exit")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 486 | |
| 487 | def test_setting_copyright(self): |
Senthil Kumaran | 8ef519b | 2013-09-07 13:59:17 -0700 | [diff] [blame] | 488 | # 'copyright', 'credits', and 'license' should be in builtins |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 489 | self.assertTrue(hasattr(builtins, "copyright")) |
| 490 | self.assertTrue(hasattr(builtins, "credits")) |
Senthil Kumaran | 8ef519b | 2013-09-07 13:59:17 -0700 | [diff] [blame] | 491 | self.assertTrue(hasattr(builtins, "license")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 492 | |
| 493 | def test_setting_help(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 494 | # 'help' should be set in builtins |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 495 | self.assertTrue(hasattr(builtins, "help")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 496 | |
| 497 | def test_aliasing_mbcs(self): |
| 498 | if sys.platform == "win32": |
| 499 | import locale |
| 500 | if locale.getdefaultlocale()[1].startswith('cp'): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 501 | for value in encodings.aliases.aliases.values(): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 502 | if value == "mbcs": |
| 503 | break |
| 504 | else: |
| 505 | self.fail("did not alias mbcs") |
| 506 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 507 | def test_sitecustomize_executed(self): |
| 508 | # If sitecustomize is available, it should have been imported. |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 509 | if "sitecustomize" not in sys.modules: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 510 | try: |
| 511 | import sitecustomize |
| 512 | except ImportError: |
| 513 | pass |
| 514 | else: |
| 515 | self.fail("sitecustomize not imported automatically") |
| 516 | |
R David Murray | 1bc6ceb | 2013-09-14 13:28:37 -0400 | [diff] [blame] | 517 | @test.support.requires_resource('network') |
Benjamin Peterson | 337578b | 2015-02-01 20:16:59 -0500 | [diff] [blame] | 518 | @test.support.system_must_validate_cert |
Georg Brandl | 78abc9d | 2013-10-27 09:41:57 +0100 | [diff] [blame] | 519 | @unittest.skipUnless(sys.version_info[3] == 'final', |
| 520 | 'only for released versions') |
Ned Deily | 5a507f0 | 2014-03-26 23:31:39 -0700 | [diff] [blame] | 521 | @unittest.skipUnless(hasattr(urllib.request, "HTTPSHandler"), |
| 522 | 'need SSL support to download license') |
R David Murray | 1bc6ceb | 2013-09-14 13:28:37 -0400 | [diff] [blame] | 523 | def test_license_exists_at_url(self): |
Ned Deily | 944d597 | 2014-03-26 23:43:26 -0700 | [diff] [blame] | 524 | # This test is a bit fragile since it depends on the format of the |
R David Murray | 1bc6ceb | 2013-09-14 13:28:37 -0400 | [diff] [blame] | 525 | # string displayed by license in the absence of a LICENSE file. |
| 526 | url = license._Printer__data.split()[1] |
| 527 | req = urllib.request.Request(url, method='HEAD') |
Victor Stinner | 1fce240 | 2020-10-05 18:24:00 +0200 | [diff] [blame^] | 528 | # Reset global urllib.request._opener |
| 529 | self.addCleanup(urllib.request.urlcleanup) |
R David Murray | 1bc6ceb | 2013-09-14 13:28:37 -0400 | [diff] [blame] | 530 | try: |
Serhiy Storchaka | bfb1cf4 | 2020-04-29 10:36:20 +0300 | [diff] [blame] | 531 | with socket_helper.transient_internet(url): |
R David Murray | 1bc6ceb | 2013-09-14 13:28:37 -0400 | [diff] [blame] | 532 | with urllib.request.urlopen(req) as data: |
| 533 | code = data.getcode() |
| 534 | except urllib.error.HTTPError as e: |
| 535 | code = e.code |
| 536 | self.assertEqual(code, 200, msg="Can't find " + url) |
Senthil Kumaran | 8ef519b | 2013-09-07 13:59:17 -0700 | [diff] [blame] | 537 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 538 | |
Christian Heimes | 8c9cd5a | 2013-10-12 00:24:55 +0200 | [diff] [blame] | 539 | class StartupImportTests(unittest.TestCase): |
| 540 | |
| 541 | def test_startup_imports(self): |
Victor Stinner | d18de46 | 2020-03-18 18:27:32 +0100 | [diff] [blame] | 542 | # Get sys.path in isolated mode (python3 -I) |
| 543 | popen = subprocess.Popen([sys.executable, '-I', '-c', |
| 544 | 'import sys; print(repr(sys.path))'], |
| 545 | stdout=subprocess.PIPE, |
| 546 | encoding='utf-8') |
| 547 | stdout = popen.communicate()[0] |
| 548 | self.assertEqual(popen.returncode, 0, repr(stdout)) |
| 549 | isolated_paths = eval(stdout) |
| 550 | |
| 551 | # bpo-27807: Even with -I, the site module executes all .pth files |
| 552 | # found in sys.path (see site.addpackage()). Skip the test if at least |
| 553 | # one .pth file is found. |
| 554 | for path in isolated_paths: |
Serhiy Storchaka | 9355868 | 2020-06-20 11:10:31 +0300 | [diff] [blame] | 555 | pth_files = glob.glob(os.path.join(glob.escape(path), "*.pth")) |
Victor Stinner | d18de46 | 2020-03-18 18:27:32 +0100 | [diff] [blame] | 556 | if pth_files: |
| 557 | self.skipTest(f"found {len(pth_files)} .pth files in: {path}") |
| 558 | |
Christian Heimes | 8c9cd5a | 2013-10-12 00:24:55 +0200 | [diff] [blame] | 559 | # This tests checks which modules are loaded by Python when it |
| 560 | # initially starts upon startup. |
Christian Heimes | 179a3db | 2013-10-12 12:32:21 +0200 | [diff] [blame] | 561 | popen = subprocess.Popen([sys.executable, '-I', '-v', '-c', |
| 562 | 'import sys; print(set(sys.modules))'], |
| 563 | stdout=subprocess.PIPE, |
Steve Dower | 313523c | 2016-09-17 12:22:41 -0700 | [diff] [blame] | 564 | stderr=subprocess.PIPE, |
| 565 | encoding='utf-8') |
Christian Heimes | 179a3db | 2013-10-12 12:32:21 +0200 | [diff] [blame] | 566 | stdout, stderr = popen.communicate() |
Victor Stinner | d18de46 | 2020-03-18 18:27:32 +0100 | [diff] [blame] | 567 | self.assertEqual(popen.returncode, 0, (stdout, stderr)) |
Christian Heimes | 179a3db | 2013-10-12 12:32:21 +0200 | [diff] [blame] | 568 | modules = eval(stdout) |
| 569 | |
Christian Heimes | 8c9cd5a | 2013-10-12 00:24:55 +0200 | [diff] [blame] | 570 | self.assertIn('site', modules) |
| 571 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 572 | # http://bugs.python.org/issue19205 |
Christian Heimes | 8c9cd5a | 2013-10-12 00:24:55 +0200 | [diff] [blame] | 573 | re_mods = {'re', '_sre', 'sre_compile', 'sre_constants', 'sre_parse'} |
Inada Naoki | c4d92c8 | 2019-05-05 18:06:30 +0900 | [diff] [blame] | 574 | self.assertFalse(modules.intersection(re_mods), stderr) |
| 575 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 576 | # http://bugs.python.org/issue9548 |
Christian Heimes | 179a3db | 2013-10-12 12:32:21 +0200 | [diff] [blame] | 577 | self.assertNotIn('locale', modules, stderr) |
Inada Naoki | c4d92c8 | 2019-05-05 18:06:30 +0900 | [diff] [blame] | 578 | |
| 579 | # http://bugs.python.org/issue19209 |
| 580 | self.assertNotIn('copyreg', modules, stderr) |
| 581 | |
| 582 | # http://bugs.python.org/issue19218 |
Christian Heimes | f1dc3ee | 2013-10-13 02:04:20 +0200 | [diff] [blame] | 583 | collection_mods = {'_collections', 'collections', 'functools', |
| 584 | 'heapq', 'itertools', 'keyword', 'operator', |
doko@ubuntu.com | 9574355 | 2014-04-15 20:37:54 +0200 | [diff] [blame] | 585 | 'reprlib', 'types', 'weakref' |
| 586 | }.difference(sys.builtin_module_names) |
Ned Deily | c22bd58 | 2017-07-28 03:02:10 -0400 | [diff] [blame] | 587 | self.assertFalse(modules.intersection(collection_mods), stderr) |
Christian Heimes | 1a5fb4e | 2013-10-12 01:00:51 +0200 | [diff] [blame] | 588 | |
Steve Dower | 6dd8eca | 2016-09-17 14:35:32 -0700 | [diff] [blame] | 589 | def test_startup_interactivehook(self): |
| 590 | r = subprocess.Popen([sys.executable, '-c', |
| 591 | 'import sys; sys.exit(hasattr(sys, "__interactivehook__"))']).wait() |
| 592 | self.assertTrue(r, "'__interactivehook__' not added by site") |
| 593 | |
| 594 | def test_startup_interactivehook_isolated(self): |
| 595 | # issue28192 readline is not automatically enabled in isolated mode |
| 596 | r = subprocess.Popen([sys.executable, '-I', '-c', |
| 597 | 'import sys; sys.exit(hasattr(sys, "__interactivehook__"))']).wait() |
| 598 | self.assertFalse(r, "'__interactivehook__' added in isolated mode") |
| 599 | |
| 600 | def test_startup_interactivehook_isolated_explicit(self): |
| 601 | # issue28192 readline can be explicitly enabled in isolated mode |
| 602 | r = subprocess.Popen([sys.executable, '-I', '-c', |
| 603 | 'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))']).wait() |
| 604 | self.assertTrue(r, "'__interactivehook__' not added by enablerlcompleter()") |
| 605 | |
Zachary Ware | d48214f | 2017-05-14 15:49:46 -0500 | [diff] [blame] | 606 | @unittest.skipUnless(sys.platform == 'win32', "only supported on Windows") |
| 607 | class _pthFileTests(unittest.TestCase): |
| 608 | |
Steve Dower | 936a660 | 2020-07-15 22:56:49 +0100 | [diff] [blame] | 609 | def _create_underpth_exe(self, lines, exe_pth=True): |
| 610 | import _winapi |
Zachary Ware | d48214f | 2017-05-14 15:49:46 -0500 | [diff] [blame] | 611 | temp_dir = tempfile.mkdtemp() |
Hai Shi | 79bb2c9 | 2020-08-06 19:51:29 +0800 | [diff] [blame] | 612 | self.addCleanup(os_helper.rmtree, temp_dir) |
Zachary Ware | d48214f | 2017-05-14 15:49:46 -0500 | [diff] [blame] | 613 | exe_file = os.path.join(temp_dir, os.path.split(sys.executable)[1]) |
Steve Dower | 936a660 | 2020-07-15 22:56:49 +0100 | [diff] [blame] | 614 | dll_src_file = _winapi.GetModuleFileName(sys.dllhandle) |
| 615 | dll_file = os.path.join(temp_dir, os.path.split(dll_src_file)[1]) |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 616 | shutil.copy(sys.executable, exe_file) |
Steve Dower | 936a660 | 2020-07-15 22:56:49 +0100 | [diff] [blame] | 617 | shutil.copy(dll_src_file, dll_file) |
| 618 | if exe_pth: |
| 619 | _pth_file = os.path.splitext(exe_file)[0] + '._pth' |
| 620 | else: |
| 621 | _pth_file = os.path.splitext(dll_file)[0] + '._pth' |
Zachary Ware | d48214f | 2017-05-14 15:49:46 -0500 | [diff] [blame] | 622 | with open(_pth_file, 'w') as f: |
| 623 | for line in lines: |
| 624 | print(line, file=f) |
| 625 | return exe_file |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 626 | |
Steve Dower | 5f9193a | 2017-02-04 15:19:29 -0800 | [diff] [blame] | 627 | def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines): |
| 628 | sys_path = [] |
| 629 | for line in lines: |
| 630 | if not line or line[0] == '#': |
| 631 | continue |
| 632 | abs_path = os.path.abspath(os.path.join(sys_prefix, line)) |
| 633 | sys_path.append(abs_path) |
| 634 | return sys_path |
| 635 | |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 636 | def test_underpth_nosite_file(self): |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 637 | libpath = os.path.dirname(os.path.dirname(encodings.__file__)) |
| 638 | exe_prefix = os.path.dirname(sys.executable) |
Steve Dower | 5f9193a | 2017-02-04 15:19:29 -0800 | [diff] [blame] | 639 | pth_lines = [ |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 640 | 'fake-path-name', |
| 641 | *[libpath for _ in range(200)], |
Steve Dower | 5f9193a | 2017-02-04 15:19:29 -0800 | [diff] [blame] | 642 | '', |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 643 | '# comment', |
Steve Dower | 5f9193a | 2017-02-04 15:19:29 -0800 | [diff] [blame] | 644 | ] |
| 645 | exe_file = self._create_underpth_exe(pth_lines) |
| 646 | sys_path = self._calc_sys_path_for_underpth_nosite( |
| 647 | os.path.dirname(exe_file), |
| 648 | pth_lines) |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 649 | |
Zachary Ware | d48214f | 2017-05-14 15:49:46 -0500 | [diff] [blame] | 650 | env = os.environ.copy() |
| 651 | env['PYTHONPATH'] = 'from-env' |
| 652 | env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH')) |
Steve Dower | 9b33bf5 | 2017-05-23 16:25:25 -0700 | [diff] [blame] | 653 | output = subprocess.check_output([exe_file, '-c', |
| 654 | 'import sys; print("\\n".join(sys.path) if sys.flags.no_site else "")' |
| 655 | ], env=env, encoding='ansi') |
| 656 | actual_sys_path = output.rstrip().split('\n') |
Serhiy Storchaka | 95c3262 | 2018-02-04 18:14:47 +0200 | [diff] [blame] | 657 | self.assertTrue(actual_sys_path, "sys.flags.no_site was False") |
Steve Dower | 9b33bf5 | 2017-05-23 16:25:25 -0700 | [diff] [blame] | 658 | self.assertEqual( |
| 659 | actual_sys_path, |
| 660 | sys_path, |
| 661 | "sys.path is incorrect" |
| 662 | ) |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 663 | |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 664 | def test_underpth_file(self): |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 665 | libpath = os.path.dirname(os.path.dirname(encodings.__file__)) |
| 666 | exe_prefix = os.path.dirname(sys.executable) |
| 667 | exe_file = self._create_underpth_exe([ |
| 668 | 'fake-path-name', |
| 669 | *[libpath for _ in range(200)], |
Steve Dower | 5f9193a | 2017-02-04 15:19:29 -0800 | [diff] [blame] | 670 | '', |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 671 | '# comment', |
| 672 | 'import site' |
| 673 | ]) |
Steve Dower | 5f9193a | 2017-02-04 15:19:29 -0800 | [diff] [blame] | 674 | sys_prefix = os.path.dirname(exe_file) |
Zachary Ware | d48214f | 2017-05-14 15:49:46 -0500 | [diff] [blame] | 675 | env = os.environ.copy() |
| 676 | env['PYTHONPATH'] = 'from-env' |
| 677 | env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH')) |
| 678 | rc = subprocess.call([exe_file, '-c', |
| 679 | 'import sys; sys.exit(not sys.flags.no_site and ' |
| 680 | '%r in sys.path and %r in sys.path and %r not in sys.path and ' |
| 681 | 'all("\\r" not in p and "\\n" not in p for p in sys.path))' % ( |
| 682 | os.path.join(sys_prefix, 'fake-path-name'), |
| 683 | libpath, |
| 684 | os.path.join(sys_prefix, 'from-env'), |
| 685 | )], env=env) |
Steve Dower | 5f9193a | 2017-02-04 15:19:29 -0800 | [diff] [blame] | 686 | self.assertTrue(rc, "sys.path is incorrect") |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 687 | |
Steve Dower | 6dd8eca | 2016-09-17 14:35:32 -0700 | [diff] [blame] | 688 | |
Steve Dower | 936a660 | 2020-07-15 22:56:49 +0100 | [diff] [blame] | 689 | def test_underpth_dll_file(self): |
| 690 | libpath = os.path.dirname(os.path.dirname(encodings.__file__)) |
| 691 | exe_prefix = os.path.dirname(sys.executable) |
| 692 | exe_file = self._create_underpth_exe([ |
| 693 | 'fake-path-name', |
| 694 | *[libpath for _ in range(200)], |
| 695 | '', |
| 696 | '# comment', |
| 697 | 'import site' |
| 698 | ], exe_pth=False) |
| 699 | sys_prefix = os.path.dirname(exe_file) |
| 700 | env = os.environ.copy() |
| 701 | env['PYTHONPATH'] = 'from-env' |
| 702 | env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH')) |
| 703 | rc = subprocess.call([exe_file, '-c', |
| 704 | 'import sys; sys.exit(not sys.flags.no_site and ' |
| 705 | '%r in sys.path and %r in sys.path and %r not in sys.path and ' |
| 706 | 'all("\\r" not in p and "\\n" not in p for p in sys.path))' % ( |
| 707 | os.path.join(sys_prefix, 'fake-path-name'), |
| 708 | libpath, |
| 709 | os.path.join(sys_prefix, 'from-env'), |
| 710 | )], env=env) |
| 711 | self.assertTrue(rc, "sys.path is incorrect") |
| 712 | |
| 713 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 714 | if __name__ == "__main__": |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 715 | unittest.main() |