blob: 9b4ab42a727b4fce606c17f909b22247bb6e6010 [file] [log] [blame]
Brett Cannon0096e262004-06-05 01:12:51 +00001"""Tests for 'site'.
2
3Tests assume the initial paths in sys.path once the interpreter has begun
4executing have not been removed.
5
6"""
7import unittest
Senthil Kumaran8ef519b2013-09-07 13:59:17 -07008import test.support
Victor Stinnerf2f45552018-12-05 16:49:35 +01009from test import support
Hai Shi79bb2c92020-08-06 19:51:29 +080010from test.support import os_helper
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +030011from test.support import socket_helper
Hai Shic7decc22020-08-04 23:53:12 +080012from test.support import captured_stderr
13from test.support.os_helper import TESTFN, EnvironmentVarGuard, change_cwd
Georg Brandl1a3284e2007-12-02 09:40:06 +000014import builtins
Brett Cannon0096e262004-06-05 01:12:51 +000015import encodings
Victor Stinnerd18de462020-03-18 18:27:32 +010016import glob
native-api2145c8c2020-06-12 09:20:11 +030017import io
Victor Stinnerd18de462020-03-18 18:27:32 +010018import os
19import re
Steve Dower1da055e2016-10-29 08:50:31 -070020import shutil
Christian Heimes8dc226f2008-05-06 23:45:46 +000021import subprocess
Victor Stinnerd18de462020-03-18 18:27:32 +010022import sys
Tarek Ziadéedacea32010-01-29 11:41:03 +000023import sysconfig
Zachary Wared48214f2017-05-14 15:49:46 -050024import tempfile
Victor Stinnerd18de462020-03-18 18:27:32 +010025import urllib.error
26import urllib.request
Victor Stinnerf2f45552018-12-05 16:49:35 +010027from unittest import mock
Tarek Ziadéedacea32010-01-29 11:41:03 +000028from copy import copy
29
Zachary Ware36193e72013-12-11 16:59:44 -060030# 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.
33if sys.flags.no_site:
34 raise unittest.SkipTest("Python was invoked with -S")
35
36import site
Brett Cannon0096e262004-06-05 01:12:51 +000037
Victor Stinnerb85c1362017-04-20 13:39:39 +020038
pxinwrab74c012020-12-21 06:27:42 +080039HAS_USER_SITE = (site.USER_SITE is not None)
Victor Stinnerb85c1362017-04-20 13:39:39 +020040OLD_SYS_PATH = None
41
42
43def setUpModule():
44 global OLD_SYS_PATH
45 OLD_SYS_PATH = sys.path[:]
46
47 if site.ENABLE_USER_SITE and not os.path.isdir(site.USER_SITE):
48 # need to add user site directory for tests
49 try:
50 os.makedirs(site.USER_SITE)
51 # modify sys.path: will be restored by tearDownModule()
52 site.addsitedir(site.USER_SITE)
53 except PermissionError as exc:
54 raise unittest.SkipTest('unable to create user site directory (%r): %s'
55 % (site.USER_SITE, exc))
56
57
58def tearDownModule():
59 sys.path[:] = OLD_SYS_PATH
Victor Stinner21d0e1b2016-03-14 17:47:03 +010060
Christian Heimes8dc226f2008-05-06 23:45:46 +000061
Brett Cannon0096e262004-06-05 01:12:51 +000062class HelperFunctionsTests(unittest.TestCase):
63 """Tests for helper functions.
Brett Cannon0096e262004-06-05 01:12:51 +000064 """
65
66 def setUp(self):
67 """Save a copy of sys.path"""
68 self.sys_path = sys.path[:]
Tarek Ziadé4a608c02009-08-20 21:28:05 +000069 self.old_base = site.USER_BASE
70 self.old_site = site.USER_SITE
71 self.old_prefixes = site.PREFIXES
Brett Cannon8ac95ee2012-04-04 17:31:16 -040072 self.original_vars = sysconfig._CONFIG_VARS
Tarek Ziadéedacea32010-01-29 11:41:03 +000073 self.old_vars = copy(sysconfig._CONFIG_VARS)
Brett Cannon0096e262004-06-05 01:12:51 +000074
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +000075 def tearDown(self):
Brett Cannon0096e262004-06-05 01:12:51 +000076 """Restore sys.path"""
Nick Coghlan6ead5522009-10-18 13:19:33 +000077 sys.path[:] = self.sys_path
Tarek Ziadé4a608c02009-08-20 21:28:05 +000078 site.USER_BASE = self.old_base
79 site.USER_SITE = self.old_site
80 site.PREFIXES = self.old_prefixes
Brett Cannon8ac95ee2012-04-04 17:31:16 -040081 sysconfig._CONFIG_VARS = self.original_vars
82 sysconfig._CONFIG_VARS.clear()
83 sysconfig._CONFIG_VARS.update(self.old_vars)
Raymond Hettingerebd95222004-06-27 03:02:18 +000084
Brett Cannon0096e262004-06-05 01:12:51 +000085 def test_makepath(self):
86 # Test makepath() have an absolute path for its first return value
87 # and a case-normalized version of the absolute path for its
88 # second value.
89 path_parts = ("Beginning", "End")
90 original_dir = os.path.join(*path_parts)
91 abs_dir, norm_dir = site.makepath(*path_parts)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000092 self.assertEqual(os.path.abspath(original_dir), abs_dir)
Brett Cannon0096e262004-06-05 01:12:51 +000093 if original_dir == os.path.normcase(original_dir):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000094 self.assertEqual(abs_dir, norm_dir)
Brett Cannon0096e262004-06-05 01:12:51 +000095 else:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000096 self.assertEqual(os.path.normcase(abs_dir), norm_dir)
Brett Cannon0096e262004-06-05 01:12:51 +000097
98 def test_init_pathinfo(self):
99 dir_set = site._init_pathinfo()
100 for entry in [site.makepath(path)[1] for path in sys.path
Brett Cannon5f0507d2016-04-08 15:04:28 -0700101 if path and os.path.exists(path)]:
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000102 self.assertIn(entry, dir_set,
103 "%s from sys.path not found in set returned "
104 "by _init_pathinfo(): %s" % (entry, dir_set))
Raymond Hettingerebd95222004-06-27 03:02:18 +0000105
Brett Cannonee86a662004-07-13 07:12:25 +0000106 def pth_file_tests(self, pth_file):
107 """Contain common code for testing results of reading a .pth file"""
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000108 self.assertIn(pth_file.imported, sys.modules,
109 "%s not in sys.modules" % pth_file.imported)
Antoine Pitrou9166e6a2009-11-01 23:55:40 +0000110 self.assertIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
111 self.assertFalse(os.path.exists(pth_file.bad_dir_path))
Brett Cannonee86a662004-07-13 07:12:25 +0000112
Brett Cannon0096e262004-06-05 01:12:51 +0000113 def test_addpackage(self):
114 # Make sure addpackage() imports if the line starts with 'import',
Brett Cannon64a84702004-07-10 02:10:45 +0000115 # adds directories to sys.path for any line in the file that is not a
116 # comment or import that is a valid directory name for where the .pth
117 # file resides; invalid directories are not added
118 pth_file = PthFile()
Brett Cannonee86a662004-07-13 07:12:25 +0000119 pth_file.cleanup(prep=True) # to make sure that nothing is
120 # pre-existing that shouldn't be
Brett Cannon0096e262004-06-05 01:12:51 +0000121 try:
Brett Cannon64a84702004-07-10 02:10:45 +0000122 pth_file.create()
123 site.addpackage(pth_file.base_dir, pth_file.filename, set())
Brett Cannonee86a662004-07-13 07:12:25 +0000124 self.pth_file_tests(pth_file)
Brett Cannon0096e262004-06-05 01:12:51 +0000125 finally:
Brett Cannon64a84702004-07-10 02:10:45 +0000126 pth_file.cleanup()
Raymond Hettingerebd95222004-06-27 03:02:18 +0000127
R. David Murrayb4ca59b2010-12-26 19:54:29 +0000128 def make_pth(self, contents, pth_dir='.', pth_name=TESTFN):
129 # Create a .pth file and return its (abspath, basename).
130 pth_dir = os.path.abspath(pth_dir)
131 pth_basename = pth_name + '.pth'
132 pth_fn = os.path.join(pth_dir, pth_basename)
Serhiy Storchaka5b10b982019-03-05 10:06:26 +0200133 with open(pth_fn, 'w', encoding='utf-8') as pth_file:
134 self.addCleanup(lambda: os.remove(pth_fn))
135 pth_file.write(contents)
R. David Murrayb4ca59b2010-12-26 19:54:29 +0000136 return pth_dir, pth_basename
137
138 def test_addpackage_import_bad_syntax(self):
139 # Issue 10642
Serhiy Storchaka94cf3082018-12-17 17:34:14 +0200140 pth_dir, pth_fn = self.make_pth("import bad-syntax\n")
R. David Murrayb4ca59b2010-12-26 19:54:29 +0000141 with captured_stderr() as err_out:
142 site.addpackage(pth_dir, pth_fn, set())
143 self.assertRegex(err_out.getvalue(), "line 1")
R. David Murrayab9d8d62010-12-27 00:03:13 +0000144 self.assertRegex(err_out.getvalue(),
145 re.escape(os.path.join(pth_dir, pth_fn)))
R. David Murrayb4ca59b2010-12-26 19:54:29 +0000146 # XXX: the previous two should be independent checks so that the
147 # order doesn't matter. The next three could be a single check
148 # but my regex foo isn't good enough to write it.
149 self.assertRegex(err_out.getvalue(), 'Traceback')
Serhiy Storchaka94cf3082018-12-17 17:34:14 +0200150 self.assertRegex(err_out.getvalue(), r'import bad-syntax')
R. David Murrayb4ca59b2010-12-26 19:54:29 +0000151 self.assertRegex(err_out.getvalue(), 'SyntaxError')
152
153 def test_addpackage_import_bad_exec(self):
154 # Issue 10642
155 pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n")
156 with captured_stderr() as err_out:
157 site.addpackage(pth_dir, pth_fn, set())
158 self.assertRegex(err_out.getvalue(), "line 2")
R. David Murrayab9d8d62010-12-27 00:03:13 +0000159 self.assertRegex(err_out.getvalue(),
160 re.escape(os.path.join(pth_dir, pth_fn)))
R. David Murrayb4ca59b2010-12-26 19:54:29 +0000161 # XXX: ditto previous XXX comment.
162 self.assertRegex(err_out.getvalue(), 'Traceback')
Eric Snow46f97b82016-09-07 16:56:15 -0700163 self.assertRegex(err_out.getvalue(), 'ModuleNotFoundError')
R. David Murrayb4ca59b2010-12-26 19:54:29 +0000164
idomic0c71a662020-09-19 15:13:29 -0400165 def test_addpackage_empty_lines(self):
166 # Issue 33689
167 pth_dir, pth_fn = self.make_pth("\n\n \n\n")
168 known_paths = site.addpackage(pth_dir, pth_fn, set())
169 self.assertEqual(known_paths, set())
170
R. David Murrayb4ca59b2010-12-26 19:54:29 +0000171 def test_addpackage_import_bad_pth_file(self):
172 # Issue 5258
173 pth_dir, pth_fn = self.make_pth("abc\x00def\n")
174 with captured_stderr() as err_out:
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300175 self.assertFalse(site.addpackage(pth_dir, pth_fn, set()))
Steve Dower04732ca2021-04-07 01:02:07 +0100176 self.maxDiff = None
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300177 self.assertEqual(err_out.getvalue(), "")
178 for path in sys.path:
179 if isinstance(path, str):
180 self.assertNotIn("abc\x00def", path)
R. David Murrayb4ca59b2010-12-26 19:54:29 +0000181
Brett Cannon0096e262004-06-05 01:12:51 +0000182 def test_addsitedir(self):
Brett Cannon64a84702004-07-10 02:10:45 +0000183 # Same tests for test_addpackage since addsitedir() essentially just
184 # calls addpackage() for every .pth file in the directory
185 pth_file = PthFile()
Brett Cannonee86a662004-07-13 07:12:25 +0000186 pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
187 # that is tested for
Brett Cannon0096e262004-06-05 01:12:51 +0000188 try:
Brett Cannonee86a662004-07-13 07:12:25 +0000189 pth_file.create()
Brett Cannon64a84702004-07-10 02:10:45 +0000190 site.addsitedir(pth_file.base_dir, set())
Brett Cannonee86a662004-07-13 07:12:25 +0000191 self.pth_file_tests(pth_file)
Brett Cannon0096e262004-06-05 01:12:51 +0000192 finally:
Brett Cannon64a84702004-07-10 02:10:45 +0000193 pth_file.cleanup()
Brett Cannon0096e262004-06-05 01:12:51 +0000194
native-api45d8d242019-03-03 19:05:19 +0300195 # This tests _getuserbase, hence the double underline
196 # to distinguish from a test for getuserbase
197 def test__getuserbase(self):
INADA Naokia8f8d5b2017-06-29 00:31:53 +0900198 self.assertEqual(site._getuserbase(), sysconfig._getuserbase())
199
pxinwrab74c012020-12-21 06:27:42 +0800200 @unittest.skipUnless(HAS_USER_SITE, 'need user site')
INADA Naokia8f8d5b2017-06-29 00:31:53 +0900201 def test_get_path(self):
INADA Naokiba9ddb72017-07-28 21:28:19 +0900202 if sys.platform == 'darwin' and sys._framework:
203 scheme = 'osx_framework_user'
204 else:
205 scheme = os.name + '_user'
INADA Naokia8f8d5b2017-06-29 00:31:53 +0900206 self.assertEqual(site._get_path(site._getuserbase()),
INADA Naokiba9ddb72017-07-28 21:28:19 +0900207 sysconfig.get_path('purelib', scheme))
INADA Naokia8f8d5b2017-06-29 00:31:53 +0900208
Ned Deily316f5732011-10-31 16:16:35 -0700209 @unittest.skipUnless(site.ENABLE_USER_SITE, "requires access to PEP 370 "
210 "user-site (site.ENABLE_USER_SITE)")
Christian Heimes8dc226f2008-05-06 23:45:46 +0000211 def test_s_option(self):
Eric Snow6b4be192017-05-22 21:36:03 -0700212 # (ncoghlan) Change this to use script_helper...
Christian Heimes8dc226f2008-05-06 23:45:46 +0000213 usersite = site.USER_SITE
Antoine Pitrou9166e6a2009-11-01 23:55:40 +0000214 self.assertIn(usersite, sys.path)
Christian Heimes8dc226f2008-05-06 23:45:46 +0000215
Éric Araujo63ebe1c2011-01-03 17:51:11 +0000216 env = os.environ.copy()
Christian Heimes8dc226f2008-05-06 23:45:46 +0000217 rc = subprocess.call([sys.executable, '-c',
Éric Araujo63ebe1c2011-01-03 17:51:11 +0000218 'import sys; sys.exit(%r in sys.path)' % usersite],
219 env=env)
Christian Heimes8dc226f2008-05-06 23:45:46 +0000220 self.assertEqual(rc, 1)
221
Éric Araujo63ebe1c2011-01-03 17:51:11 +0000222 env = os.environ.copy()
Christian Heimes8dc226f2008-05-06 23:45:46 +0000223 rc = subprocess.call([sys.executable, '-s', '-c',
Éric Araujo63ebe1c2011-01-03 17:51:11 +0000224 'import sys; sys.exit(%r in sys.path)' % usersite],
225 env=env)
Antoine Pitroua1782e12013-10-23 22:03:22 +0200226 if usersite == site.getsitepackages()[0]:
227 self.assertEqual(rc, 1)
228 else:
Eric Snow6b4be192017-05-22 21:36:03 -0700229 self.assertEqual(rc, 0, "User site still added to path with -s")
Christian Heimes8dc226f2008-05-06 23:45:46 +0000230
231 env = os.environ.copy()
232 env["PYTHONNOUSERSITE"] = "1"
233 rc = subprocess.call([sys.executable, '-c',
Benjamin Petersonfea6a942008-07-02 16:11:42 +0000234 'import sys; sys.exit(%r in sys.path)' % usersite],
Christian Heimes8dc226f2008-05-06 23:45:46 +0000235 env=env)
Antoine Pitroua1782e12013-10-23 22:03:22 +0200236 if usersite == site.getsitepackages()[0]:
237 self.assertEqual(rc, 1)
238 else:
Eric Snow6b4be192017-05-22 21:36:03 -0700239 self.assertEqual(rc, 0,
240 "User site still added to path with PYTHONNOUSERSITE")
Christian Heimes8dc226f2008-05-06 23:45:46 +0000241
242 env = os.environ.copy()
243 env["PYTHONUSERBASE"] = "/tmp"
244 rc = subprocess.call([sys.executable, '-c',
245 'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'],
246 env=env)
Eric Snow6b4be192017-05-22 21:36:03 -0700247 self.assertEqual(rc, 1,
248 "User base not set by PYTHONUSERBASE")
Christian Heimes8dc226f2008-05-06 23:45:46 +0000249
pxinwrab74c012020-12-21 06:27:42 +0800250 @unittest.skipUnless(HAS_USER_SITE, 'need user site')
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000251 def test_getuserbase(self):
252 site.USER_BASE = None
253 user_base = site.getuserbase()
254
255 # the call sets site.USER_BASE
Ezio Melottib3aedd42010-11-20 19:04:17 +0000256 self.assertEqual(site.USER_BASE, user_base)
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000257
258 # let's set PYTHONUSERBASE and see if it uses it
259 site.USER_BASE = None
Tarek Ziadéedacea32010-01-29 11:41:03 +0000260 import sysconfig
261 sysconfig._CONFIG_VARS = None
262
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000263 with EnvironmentVarGuard() as environ:
264 environ['PYTHONUSERBASE'] = 'xoxo'
Antoine Pitrou9166e6a2009-11-01 23:55:40 +0000265 self.assertTrue(site.getuserbase().startswith('xoxo'),
266 site.getuserbase())
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000267
pxinwrab74c012020-12-21 06:27:42 +0800268 @unittest.skipUnless(HAS_USER_SITE, 'need user site')
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000269 def test_getusersitepackages(self):
270 site.USER_SITE = None
271 site.USER_BASE = None
272 user_site = site.getusersitepackages()
273
274 # the call sets USER_BASE *and* USER_SITE
Ezio Melottib3aedd42010-11-20 19:04:17 +0000275 self.assertEqual(site.USER_SITE, user_site)
Antoine Pitrou9166e6a2009-11-01 23:55:40 +0000276 self.assertTrue(user_site.startswith(site.USER_BASE), user_site)
Victor Stinnerf2f45552018-12-05 16:49:35 +0100277 self.assertEqual(site.USER_BASE, site.getuserbase())
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000278
279 def test_getsitepackages(self):
280 site.PREFIXES = ['xoxo']
281 dirs = site.getsitepackages()
Ned Deily763f0942018-01-30 05:14:09 -0500282 if os.sep == '/':
283 # OS X, Linux, FreeBSD, etc
Victor Stinner8510f432020-03-10 09:53:09 +0100284 if sys.platlibdir != "lib":
285 self.assertEqual(len(dirs), 2)
286 wanted = os.path.join('xoxo', sys.platlibdir,
287 'python%d.%d' % sys.version_info[:2],
288 'site-packages')
289 self.assertEqual(dirs[0], wanted)
290 else:
291 self.assertEqual(len(dirs), 1)
Serhiy Storchaka885bdc42016-02-11 13:10:36 +0200292 wanted = os.path.join('xoxo', 'lib',
293 'python%d.%d' % sys.version_info[:2],
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000294 'site-packages')
Victor Stinner8510f432020-03-10 09:53:09 +0100295 self.assertEqual(dirs[-1], wanted)
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000296 else:
Ned Deilyd531b292012-02-06 00:58:18 +0100297 # other platforms
Ezio Melottifc8b2052010-08-17 08:35:41 +0000298 self.assertEqual(len(dirs), 2)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000299 self.assertEqual(dirs[0], 'xoxo')
Tarek Ziadé8c0e2172009-10-27 21:24:21 +0000300 wanted = os.path.join('xoxo', 'lib', 'site-packages')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000301 self.assertEqual(dirs[1], wanted)
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000302
pxinwrab74c012020-12-21 06:27:42 +0800303 @unittest.skipUnless(HAS_USER_SITE, 'need user site')
Victor Stinnerf2f45552018-12-05 16:49:35 +0100304 def test_no_home_directory(self):
305 # bpo-10496: getuserbase() and getusersitepackages() must not fail if
306 # the current user has no home directory (if expanduser() returns the
307 # path unchanged).
308 site.USER_SITE = None
309 site.USER_BASE = None
310
311 with EnvironmentVarGuard() as environ, \
312 mock.patch('os.path.expanduser', lambda path: path):
313
314 del environ['PYTHONUSERBASE']
315 del environ['APPDATA']
316
317 user_base = site.getuserbase()
318 self.assertTrue(user_base.startswith('~' + os.sep),
319 user_base)
320
321 user_site = site.getusersitepackages()
322 self.assertTrue(user_site.startswith(user_base), user_site)
323
324 with mock.patch('os.path.isdir', return_value=False) as mock_isdir, \
325 mock.patch.object(site, 'addsitedir') as mock_addsitedir, \
326 support.swap_attr(site, 'ENABLE_USER_SITE', True):
327
328 # addusersitepackages() must not add user_site to sys.path
329 # if it is not an existing directory
330 known_paths = set()
331 site.addusersitepackages(known_paths)
332
333 mock_isdir.assert_called_once_with(user_site)
334 mock_addsitedir.assert_not_called()
335 self.assertFalse(known_paths)
336
native-api2145c8c2020-06-12 09:20:11 +0300337 def test_trace(self):
338 message = "bla-bla-bla"
339 for verbose, out in (True, message + "\n"), (False, ""):
340 with mock.patch('sys.flags', mock.Mock(verbose=verbose)), \
341 mock.patch('sys.stderr', io.StringIO()):
342 site._trace(message)
343 self.assertEqual(sys.stderr.getvalue(), out)
344
Victor Stinnerf2f45552018-12-05 16:49:35 +0100345
Brett Cannon64a84702004-07-10 02:10:45 +0000346class PthFile(object):
347 """Helper class for handling testing of .pth files"""
Brett Cannon0096e262004-06-05 01:12:51 +0000348
Brett Cannon64a84702004-07-10 02:10:45 +0000349 def __init__(self, filename_base=TESTFN, imported="time",
350 good_dirname="__testdir__", bad_dirname="__bad"):
351 """Initialize instance variables"""
352 self.filename = filename_base + ".pth"
353 self.base_dir = os.path.abspath('')
354 self.file_path = os.path.join(self.base_dir, self.filename)
Brett Cannonee86a662004-07-13 07:12:25 +0000355 self.imported = imported
Brett Cannon64a84702004-07-10 02:10:45 +0000356 self.good_dirname = good_dirname
357 self.bad_dirname = bad_dirname
358 self.good_dir_path = os.path.join(self.base_dir, self.good_dirname)
359 self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname)
Brett Cannon0096e262004-06-05 01:12:51 +0000360
Brett Cannon64a84702004-07-10 02:10:45 +0000361 def create(self):
362 """Create a .pth file with a comment, blank lines, an ``import
363 <self.imported>``, a line with self.good_dirname, and a line with
364 self.bad_dirname.
Tim Peters182b5ac2004-07-18 06:16:08 +0000365
Brett Cannon64a84702004-07-10 02:10:45 +0000366 Creation of the directory for self.good_dir_path (based off of
367 self.good_dirname) is also performed.
Brett Cannon0096e262004-06-05 01:12:51 +0000368
Brett Cannon64a84702004-07-10 02:10:45 +0000369 Make sure to call self.cleanup() to undo anything done by this method.
Tim Peters182b5ac2004-07-18 06:16:08 +0000370
Brett Cannon64a84702004-07-10 02:10:45 +0000371 """
Michael W. Hudsonff522862005-05-27 14:58:06 +0000372 FILE = open(self.file_path, 'w')
Brett Cannon64a84702004-07-10 02:10:45 +0000373 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000374 print("#import @bad module name", file=FILE)
375 print("\n", file=FILE)
376 print("import %s" % self.imported, file=FILE)
377 print(self.good_dirname, file=FILE)
378 print(self.bad_dirname, file=FILE)
Brett Cannon64a84702004-07-10 02:10:45 +0000379 finally:
380 FILE.close()
381 os.mkdir(self.good_dir_path)
382
Brett Cannonee86a662004-07-13 07:12:25 +0000383 def cleanup(self, prep=False):
Brett Cannon64a84702004-07-10 02:10:45 +0000384 """Make sure that the .pth file is deleted, self.imported is not in
385 sys.modules, and that both self.good_dirname and self.bad_dirname are
386 not existing directories."""
Brett Cannonee86a662004-07-13 07:12:25 +0000387 if os.path.exists(self.file_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000388 os.remove(self.file_path)
Brett Cannonee86a662004-07-13 07:12:25 +0000389 if prep:
390 self.imported_module = sys.modules.get(self.imported)
391 if self.imported_module:
392 del sys.modules[self.imported]
393 else:
394 if self.imported_module:
395 sys.modules[self.imported] = self.imported_module
396 if os.path.exists(self.good_dir_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000397 os.rmdir(self.good_dir_path)
Brett Cannonee86a662004-07-13 07:12:25 +0000398 if os.path.exists(self.bad_dir_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000399 os.rmdir(self.bad_dir_path)
Brett Cannon0096e262004-06-05 01:12:51 +0000400
401class ImportSideEffectTests(unittest.TestCase):
402 """Test side-effects from importing 'site'."""
403
404 def setUp(self):
405 """Make a copy of sys.path"""
406 self.sys_path = sys.path[:]
407
408 def tearDown(self):
409 """Restore sys.path"""
Nick Coghlan6ead5522009-10-18 13:19:33 +0000410 sys.path[:] = self.sys_path
Brett Cannon0096e262004-06-05 01:12:51 +0000411
INADA Naokid4c76d92018-10-01 21:10:37 +0900412 def test_abs_paths_cached_None(self):
413 """Test for __cached__ is None.
414
415 Regarding to PEP 3147, __cached__ can be None.
416
417 See also: https://bugs.python.org/issue30167
418 """
419 sys.modules['test'].__cached__ = None
420 site.abs_paths()
421 self.assertIsNone(sys.modules['test'].__cached__)
422
Brett Cannon0096e262004-06-05 01:12:51 +0000423 def test_no_duplicate_paths(self):
424 # No duplicate paths should exist in sys.path
425 # Handled by removeduppaths()
426 site.removeduppaths()
427 seen_paths = set()
428 for path in sys.path:
Benjamin Peterson577473f2010-01-19 00:09:57 +0000429 self.assertNotIn(path, seen_paths)
Brett Cannon0096e262004-06-05 01:12:51 +0000430 seen_paths.add(path)
431
Zachary Ware9fe6d862013-12-08 00:20:35 -0600432 @unittest.skip('test not implemented')
Brett Cannon0096e262004-06-05 01:12:51 +0000433 def test_add_build_dir(self):
434 # Test that the build directory's Modules directory is used when it
435 # should be.
436 # XXX: implement
437 pass
438
Brett Cannon0096e262004-06-05 01:12:51 +0000439 def test_setting_quit(self):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000440 # 'quit' and 'exit' should be injected into builtins
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000441 self.assertTrue(hasattr(builtins, "quit"))
442 self.assertTrue(hasattr(builtins, "exit"))
Brett Cannon0096e262004-06-05 01:12:51 +0000443
444 def test_setting_copyright(self):
Senthil Kumaran8ef519b2013-09-07 13:59:17 -0700445 # 'copyright', 'credits', and 'license' should be in builtins
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000446 self.assertTrue(hasattr(builtins, "copyright"))
447 self.assertTrue(hasattr(builtins, "credits"))
Senthil Kumaran8ef519b2013-09-07 13:59:17 -0700448 self.assertTrue(hasattr(builtins, "license"))
Brett Cannon0096e262004-06-05 01:12:51 +0000449
450 def test_setting_help(self):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000451 # 'help' should be set in builtins
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000452 self.assertTrue(hasattr(builtins, "help"))
Brett Cannon0096e262004-06-05 01:12:51 +0000453
454 def test_aliasing_mbcs(self):
455 if sys.platform == "win32":
456 import locale
457 if locale.getdefaultlocale()[1].startswith('cp'):
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000458 for value in encodings.aliases.aliases.values():
Brett Cannon0096e262004-06-05 01:12:51 +0000459 if value == "mbcs":
460 break
461 else:
462 self.fail("did not alias mbcs")
463
Brett Cannon0096e262004-06-05 01:12:51 +0000464 def test_sitecustomize_executed(self):
465 # If sitecustomize is available, it should have been imported.
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000466 if "sitecustomize" not in sys.modules:
Brett Cannon0096e262004-06-05 01:12:51 +0000467 try:
468 import sitecustomize
469 except ImportError:
470 pass
471 else:
472 self.fail("sitecustomize not imported automatically")
473
R David Murray1bc6ceb2013-09-14 13:28:37 -0400474 @test.support.requires_resource('network')
Benjamin Peterson337578b2015-02-01 20:16:59 -0500475 @test.support.system_must_validate_cert
Ned Deily5a507f02014-03-26 23:31:39 -0700476 @unittest.skipUnless(hasattr(urllib.request, "HTTPSHandler"),
477 'need SSL support to download license')
R David Murray1bc6ceb2013-09-14 13:28:37 -0400478 def test_license_exists_at_url(self):
Ned Deily944d5972014-03-26 23:43:26 -0700479 # This test is a bit fragile since it depends on the format of the
R David Murray1bc6ceb2013-09-14 13:28:37 -0400480 # string displayed by license in the absence of a LICENSE file.
481 url = license._Printer__data.split()[1]
482 req = urllib.request.Request(url, method='HEAD')
Victor Stinner1fce2402020-10-05 18:24:00 +0200483 # Reset global urllib.request._opener
484 self.addCleanup(urllib.request.urlcleanup)
R David Murray1bc6ceb2013-09-14 13:28:37 -0400485 try:
Serhiy Storchakabfb1cf42020-04-29 10:36:20 +0300486 with socket_helper.transient_internet(url):
R David Murray1bc6ceb2013-09-14 13:28:37 -0400487 with urllib.request.urlopen(req) as data:
488 code = data.getcode()
489 except urllib.error.HTTPError as e:
490 code = e.code
491 self.assertEqual(code, 200, msg="Can't find " + url)
Senthil Kumaran8ef519b2013-09-07 13:59:17 -0700492
Brett Cannon0096e262004-06-05 01:12:51 +0000493
Christian Heimes8c9cd5a2013-10-12 00:24:55 +0200494class StartupImportTests(unittest.TestCase):
495
496 def test_startup_imports(self):
Victor Stinnerd18de462020-03-18 18:27:32 +0100497 # Get sys.path in isolated mode (python3 -I)
498 popen = subprocess.Popen([sys.executable, '-I', '-c',
499 'import sys; print(repr(sys.path))'],
500 stdout=subprocess.PIPE,
501 encoding='utf-8')
502 stdout = popen.communicate()[0]
503 self.assertEqual(popen.returncode, 0, repr(stdout))
504 isolated_paths = eval(stdout)
505
506 # bpo-27807: Even with -I, the site module executes all .pth files
507 # found in sys.path (see site.addpackage()). Skip the test if at least
508 # one .pth file is found.
509 for path in isolated_paths:
Serhiy Storchaka93558682020-06-20 11:10:31 +0300510 pth_files = glob.glob(os.path.join(glob.escape(path), "*.pth"))
Victor Stinnerd18de462020-03-18 18:27:32 +0100511 if pth_files:
512 self.skipTest(f"found {len(pth_files)} .pth files in: {path}")
513
Christian Heimes8c9cd5a2013-10-12 00:24:55 +0200514 # This tests checks which modules are loaded by Python when it
515 # initially starts upon startup.
Christian Heimes179a3db2013-10-12 12:32:21 +0200516 popen = subprocess.Popen([sys.executable, '-I', '-v', '-c',
517 'import sys; print(set(sys.modules))'],
518 stdout=subprocess.PIPE,
Steve Dower313523c2016-09-17 12:22:41 -0700519 stderr=subprocess.PIPE,
520 encoding='utf-8')
Christian Heimes179a3db2013-10-12 12:32:21 +0200521 stdout, stderr = popen.communicate()
Victor Stinnerd18de462020-03-18 18:27:32 +0100522 self.assertEqual(popen.returncode, 0, (stdout, stderr))
Christian Heimes179a3db2013-10-12 12:32:21 +0200523 modules = eval(stdout)
524
Christian Heimes8c9cd5a2013-10-12 00:24:55 +0200525 self.assertIn('site', modules)
526
Christian Heimes25827622013-10-12 01:27:08 +0200527 # http://bugs.python.org/issue19205
Christian Heimes8c9cd5a2013-10-12 00:24:55 +0200528 re_mods = {'re', '_sre', 'sre_compile', 'sre_constants', 'sre_parse'}
Inada Naokic4d92c82019-05-05 18:06:30 +0900529 self.assertFalse(modules.intersection(re_mods), stderr)
530
Christian Heimes25827622013-10-12 01:27:08 +0200531 # http://bugs.python.org/issue9548
Christian Heimes179a3db2013-10-12 12:32:21 +0200532 self.assertNotIn('locale', modules, stderr)
Inada Naokic4d92c82019-05-05 18:06:30 +0900533
534 # http://bugs.python.org/issue19209
535 self.assertNotIn('copyreg', modules, stderr)
536
537 # http://bugs.python.org/issue19218
Christian Heimesf1dc3ee2013-10-13 02:04:20 +0200538 collection_mods = {'_collections', 'collections', 'functools',
539 'heapq', 'itertools', 'keyword', 'operator',
doko@ubuntu.com95743552014-04-15 20:37:54 +0200540 'reprlib', 'types', 'weakref'
541 }.difference(sys.builtin_module_names)
Ned Deilyc22bd582017-07-28 03:02:10 -0400542 self.assertFalse(modules.intersection(collection_mods), stderr)
Christian Heimes1a5fb4e2013-10-12 01:00:51 +0200543
Steve Dower6dd8eca2016-09-17 14:35:32 -0700544 def test_startup_interactivehook(self):
545 r = subprocess.Popen([sys.executable, '-c',
546 'import sys; sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
547 self.assertTrue(r, "'__interactivehook__' not added by site")
548
549 def test_startup_interactivehook_isolated(self):
550 # issue28192 readline is not automatically enabled in isolated mode
551 r = subprocess.Popen([sys.executable, '-I', '-c',
552 'import sys; sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
553 self.assertFalse(r, "'__interactivehook__' added in isolated mode")
554
555 def test_startup_interactivehook_isolated_explicit(self):
556 # issue28192 readline can be explicitly enabled in isolated mode
557 r = subprocess.Popen([sys.executable, '-I', '-c',
558 'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
559 self.assertTrue(r, "'__interactivehook__' not added by enablerlcompleter()")
560
Zachary Wared48214f2017-05-14 15:49:46 -0500561@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
562class _pthFileTests(unittest.TestCase):
563
Steve Dower936a6602020-07-15 22:56:49 +0100564 def _create_underpth_exe(self, lines, exe_pth=True):
565 import _winapi
Zachary Wared48214f2017-05-14 15:49:46 -0500566 temp_dir = tempfile.mkdtemp()
Hai Shi79bb2c92020-08-06 19:51:29 +0800567 self.addCleanup(os_helper.rmtree, temp_dir)
Zachary Wared48214f2017-05-14 15:49:46 -0500568 exe_file = os.path.join(temp_dir, os.path.split(sys.executable)[1])
Steve Dower936a6602020-07-15 22:56:49 +0100569 dll_src_file = _winapi.GetModuleFileName(sys.dllhandle)
570 dll_file = os.path.join(temp_dir, os.path.split(dll_src_file)[1])
Steve Dower1da055e2016-10-29 08:50:31 -0700571 shutil.copy(sys.executable, exe_file)
Steve Dower936a6602020-07-15 22:56:49 +0100572 shutil.copy(dll_src_file, dll_file)
573 if exe_pth:
574 _pth_file = os.path.splitext(exe_file)[0] + '._pth'
575 else:
576 _pth_file = os.path.splitext(dll_file)[0] + '._pth'
Zachary Wared48214f2017-05-14 15:49:46 -0500577 with open(_pth_file, 'w') as f:
578 for line in lines:
579 print(line, file=f)
580 return exe_file
Steve Dower1da055e2016-10-29 08:50:31 -0700581
Steve Dower5f9193a2017-02-04 15:19:29 -0800582 def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):
583 sys_path = []
584 for line in lines:
585 if not line or line[0] == '#':
586 continue
587 abs_path = os.path.abspath(os.path.join(sys_prefix, line))
588 sys_path.append(abs_path)
589 return sys_path
590
Steve Dowerc6dd4152016-10-27 14:28:07 -0700591 def test_underpth_nosite_file(self):
Steve Dower1da055e2016-10-29 08:50:31 -0700592 libpath = os.path.dirname(os.path.dirname(encodings.__file__))
593 exe_prefix = os.path.dirname(sys.executable)
Steve Dower5f9193a2017-02-04 15:19:29 -0800594 pth_lines = [
Steve Dower1da055e2016-10-29 08:50:31 -0700595 'fake-path-name',
596 *[libpath for _ in range(200)],
Steve Dower5f9193a2017-02-04 15:19:29 -0800597 '',
Steve Dower1da055e2016-10-29 08:50:31 -0700598 '# comment',
Steve Dower5f9193a2017-02-04 15:19:29 -0800599 ]
600 exe_file = self._create_underpth_exe(pth_lines)
601 sys_path = self._calc_sys_path_for_underpth_nosite(
602 os.path.dirname(exe_file),
603 pth_lines)
Steve Dowerc6dd4152016-10-27 14:28:07 -0700604
Zachary Wared48214f2017-05-14 15:49:46 -0500605 env = os.environ.copy()
606 env['PYTHONPATH'] = 'from-env'
607 env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
Steve Dower9b33bf52017-05-23 16:25:25 -0700608 output = subprocess.check_output([exe_file, '-c',
609 'import sys; print("\\n".join(sys.path) if sys.flags.no_site else "")'
610 ], env=env, encoding='ansi')
611 actual_sys_path = output.rstrip().split('\n')
Serhiy Storchaka95c32622018-02-04 18:14:47 +0200612 self.assertTrue(actual_sys_path, "sys.flags.no_site was False")
Steve Dower9b33bf52017-05-23 16:25:25 -0700613 self.assertEqual(
614 actual_sys_path,
615 sys_path,
616 "sys.path is incorrect"
617 )
Steve Dowerc6dd4152016-10-27 14:28:07 -0700618
Steve Dowerc6dd4152016-10-27 14:28:07 -0700619 def test_underpth_file(self):
Steve Dower1da055e2016-10-29 08:50:31 -0700620 libpath = os.path.dirname(os.path.dirname(encodings.__file__))
621 exe_prefix = os.path.dirname(sys.executable)
622 exe_file = self._create_underpth_exe([
623 'fake-path-name',
624 *[libpath for _ in range(200)],
Steve Dower5f9193a2017-02-04 15:19:29 -0800625 '',
Steve Dower1da055e2016-10-29 08:50:31 -0700626 '# comment',
627 'import site'
628 ])
Steve Dower5f9193a2017-02-04 15:19:29 -0800629 sys_prefix = os.path.dirname(exe_file)
Zachary Wared48214f2017-05-14 15:49:46 -0500630 env = os.environ.copy()
631 env['PYTHONPATH'] = 'from-env'
632 env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
633 rc = subprocess.call([exe_file, '-c',
634 'import sys; sys.exit(not sys.flags.no_site and '
635 '%r in sys.path and %r in sys.path and %r not in sys.path and '
636 'all("\\r" not in p and "\\n" not in p for p in sys.path))' % (
637 os.path.join(sys_prefix, 'fake-path-name'),
638 libpath,
639 os.path.join(sys_prefix, 'from-env'),
640 )], env=env)
Steve Dower5f9193a2017-02-04 15:19:29 -0800641 self.assertTrue(rc, "sys.path is incorrect")
Steve Dowerc6dd4152016-10-27 14:28:07 -0700642
Steve Dower6dd8eca2016-09-17 14:35:32 -0700643
Steve Dower936a6602020-07-15 22:56:49 +0100644 def test_underpth_dll_file(self):
645 libpath = os.path.dirname(os.path.dirname(encodings.__file__))
646 exe_prefix = os.path.dirname(sys.executable)
647 exe_file = self._create_underpth_exe([
648 'fake-path-name',
649 *[libpath for _ in range(200)],
650 '',
651 '# comment',
652 'import site'
653 ], exe_pth=False)
654 sys_prefix = os.path.dirname(exe_file)
655 env = os.environ.copy()
656 env['PYTHONPATH'] = 'from-env'
657 env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
658 rc = subprocess.call([exe_file, '-c',
659 'import sys; sys.exit(not sys.flags.no_site and '
660 '%r in sys.path and %r in sys.path and %r not in sys.path and '
661 'all("\\r" not in p and "\\n" not in p for p in sys.path))' % (
662 os.path.join(sys_prefix, 'fake-path-name'),
663 libpath,
664 os.path.join(sys_prefix, 'from-env'),
665 )], env=env)
666 self.assertTrue(rc, "sys.path is incorrect")
667
668
Brett Cannon0096e262004-06-05 01:12:51 +0000669if __name__ == "__main__":
Brett Cannon3e9a9ae2013-06-12 21:25:59 -0400670 unittest.main()