blob: 081173db025f276a99194f046e16ff7b3f144add [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
Walter Dörwaldb525e182009-04-26 21:39:21 +00008from test.support import run_unittest, TESTFN, EnvironmentVarGuard
R. David Murray6cb252f2010-12-26 22:24:54 +00009from test.support import captured_output
Georg Brandl1a3284e2007-12-02 09:40:06 +000010import builtins
Brett Cannon0096e262004-06-05 01:12:51 +000011import os
12import sys
R. David Murraya6405232010-12-27 00:07:32 +000013import re
Brett Cannon0096e262004-06-05 01:12:51 +000014import encodings
Christian Heimes8dc226f2008-05-06 23:45:46 +000015import subprocess
Brett Cannon0096e262004-06-05 01:12:51 +000016# Need to make sure to not import 'site' if someone specified ``-S`` at the
17# command-line. Detect this by just making sure 'site' has not been imported
18# already.
19if "site" in sys.modules:
20 import site
21else:
Benjamin Petersone549ead2009-03-28 21:42:05 +000022 raise unittest.SkipTest("importation of site.py suppressed")
Brett Cannon0096e262004-06-05 01:12:51 +000023
Christian Heimes8dc226f2008-05-06 23:45:46 +000024if not os.path.isdir(site.USER_SITE):
25 # need to add user site directory for tests
26 os.makedirs(site.USER_SITE)
27 site.addsitedir(site.USER_SITE)
28
Brett Cannon0096e262004-06-05 01:12:51 +000029class HelperFunctionsTests(unittest.TestCase):
30 """Tests for helper functions.
Raymond Hettingerebd95222004-06-27 03:02:18 +000031
Brett Cannon0096e262004-06-05 01:12:51 +000032 The setting of the encoding (set using sys.setdefaultencoding) used by
33 the Unicode implementation is not tested.
Raymond Hettingerebd95222004-06-27 03:02:18 +000034
Brett Cannon0096e262004-06-05 01:12:51 +000035 """
36
37 def setUp(self):
38 """Save a copy of sys.path"""
39 self.sys_path = sys.path[:]
40
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +000041 def tearDown(self):
Brett Cannon0096e262004-06-05 01:12:51 +000042 """Restore sys.path"""
43 sys.path = self.sys_path
Raymond Hettingerebd95222004-06-27 03:02:18 +000044
Brett Cannon0096e262004-06-05 01:12:51 +000045 def test_makepath(self):
46 # Test makepath() have an absolute path for its first return value
47 # and a case-normalized version of the absolute path for its
48 # second value.
49 path_parts = ("Beginning", "End")
50 original_dir = os.path.join(*path_parts)
51 abs_dir, norm_dir = site.makepath(*path_parts)
Georg Brandlab91fde2009-08-13 08:51:18 +000052 self.assertEqual(os.path.abspath(original_dir), abs_dir)
Brett Cannon0096e262004-06-05 01:12:51 +000053 if original_dir == os.path.normcase(original_dir):
Georg Brandlab91fde2009-08-13 08:51:18 +000054 self.assertEqual(abs_dir, norm_dir)
Brett Cannon0096e262004-06-05 01:12:51 +000055 else:
Georg Brandlab91fde2009-08-13 08:51:18 +000056 self.assertEqual(os.path.normcase(abs_dir), norm_dir)
Brett Cannon0096e262004-06-05 01:12:51 +000057
58 def test_init_pathinfo(self):
59 dir_set = site._init_pathinfo()
60 for entry in [site.makepath(path)[1] for path in sys.path
61 if path and os.path.isdir(path)]:
Georg Brandlab91fde2009-08-13 08:51:18 +000062 self.assertTrue(entry in dir_set,
Brett Cannon0096e262004-06-05 01:12:51 +000063 "%s from sys.path not found in set returned "
64 "by _init_pathinfo(): %s" % (entry, dir_set))
Raymond Hettingerebd95222004-06-27 03:02:18 +000065
Brett Cannonee86a662004-07-13 07:12:25 +000066 def pth_file_tests(self, pth_file):
67 """Contain common code for testing results of reading a .pth file"""
Georg Brandlab91fde2009-08-13 08:51:18 +000068 self.assertTrue(pth_file.imported in sys.modules,
Brett Cannonee86a662004-07-13 07:12:25 +000069 "%s not in sys.path" % pth_file.imported)
Georg Brandlab91fde2009-08-13 08:51:18 +000070 self.assertTrue(site.makepath(pth_file.good_dir_path)[0] in sys.path)
71 self.assertTrue(not os.path.exists(pth_file.bad_dir_path))
Brett Cannonee86a662004-07-13 07:12:25 +000072
Brett Cannon0096e262004-06-05 01:12:51 +000073 def test_addpackage(self):
74 # Make sure addpackage() imports if the line starts with 'import',
Brett Cannon64a84702004-07-10 02:10:45 +000075 # adds directories to sys.path for any line in the file that is not a
76 # comment or import that is a valid directory name for where the .pth
77 # file resides; invalid directories are not added
78 pth_file = PthFile()
Brett Cannonee86a662004-07-13 07:12:25 +000079 pth_file.cleanup(prep=True) # to make sure that nothing is
80 # pre-existing that shouldn't be
Brett Cannon0096e262004-06-05 01:12:51 +000081 try:
Brett Cannon64a84702004-07-10 02:10:45 +000082 pth_file.create()
83 site.addpackage(pth_file.base_dir, pth_file.filename, set())
Brett Cannonee86a662004-07-13 07:12:25 +000084 self.pth_file_tests(pth_file)
Brett Cannon0096e262004-06-05 01:12:51 +000085 finally:
Brett Cannon64a84702004-07-10 02:10:45 +000086 pth_file.cleanup()
Raymond Hettingerebd95222004-06-27 03:02:18 +000087
R. David Murray6cb252f2010-12-26 22:24:54 +000088 def make_pth(self, contents, pth_dir='.', pth_name=TESTFN):
89 # Create a .pth file and return its (abspath, basename).
90 pth_dir = os.path.abspath(pth_dir)
91 pth_basename = pth_name + '.pth'
92 pth_fn = os.path.join(pth_dir, pth_basename)
93 pth_file = open(pth_fn, 'w', encoding='utf-8')
94 self.addCleanup(lambda: os.remove(pth_fn))
95 pth_file.write(contents)
96 pth_file.close()
97 return pth_dir, pth_basename
98
99 def test_addpackage_import_bad_syntax(self):
100 # Issue 10642
101 pth_dir, pth_fn = self.make_pth("import bad)syntax\n")
102 with captured_output("stderr") as err_out:
103 site.addpackage(pth_dir, pth_fn, set())
104 self.assertRegexpMatches(err_out.getvalue(), "line 1")
R. David Murraya6405232010-12-27 00:07:32 +0000105 self.assertRegexpMatches(err_out.getvalue(),
106 re.escape(os.path.join(pth_dir, pth_fn)))
R. David Murray6cb252f2010-12-26 22:24:54 +0000107 # XXX: the previous two should be independent checks so that the
108 # order doesn't matter. The next three could be a single check
109 # but my regex foo isn't good enough to write it.
110 self.assertRegexpMatches(err_out.getvalue(), 'Traceback')
111 self.assertRegexpMatches(err_out.getvalue(), r'import bad\)syntax')
112 self.assertRegexpMatches(err_out.getvalue(), 'SyntaxError')
113
114 def test_addpackage_import_bad_exec(self):
115 # Issue 10642
116 pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n")
117 with captured_output("stderr") as err_out:
118 site.addpackage(pth_dir, pth_fn, set())
119 self.assertRegexpMatches(err_out.getvalue(), "line 2")
R. David Murraya6405232010-12-27 00:07:32 +0000120 self.assertRegexpMatches(err_out.getvalue(),
121 re.escape(os.path.join(pth_dir, pth_fn)))
R. David Murray6cb252f2010-12-26 22:24:54 +0000122 # XXX: ditto previous XXX comment.
123 self.assertRegexpMatches(err_out.getvalue(), 'Traceback')
124 self.assertRegexpMatches(err_out.getvalue(), 'ImportError')
125
R. David Murray17638d92010-12-27 04:36:07 +0000126 @unittest.skipIf(sys.platform == "win32", "Windows does not raise an "
127 "error for file paths containing null characters")
R. David Murray6cb252f2010-12-26 22:24:54 +0000128 def test_addpackage_import_bad_pth_file(self):
129 # Issue 5258
130 pth_dir, pth_fn = self.make_pth("abc\x00def\n")
131 with captured_output("stderr") as err_out:
132 site.addpackage(pth_dir, pth_fn, set())
133 self.assertRegexpMatches(err_out.getvalue(), "line 1")
R. David Murraya6405232010-12-27 00:07:32 +0000134 self.assertRegexpMatches(err_out.getvalue(),
135 re.escape(os.path.join(pth_dir, pth_fn)))
R. David Murray6cb252f2010-12-26 22:24:54 +0000136 # XXX: ditto previous XXX comment.
137 self.assertRegexpMatches(err_out.getvalue(), 'Traceback')
138 self.assertRegexpMatches(err_out.getvalue(), 'TypeError')
139
Brett Cannon0096e262004-06-05 01:12:51 +0000140 def test_addsitedir(self):
Brett Cannon64a84702004-07-10 02:10:45 +0000141 # Same tests for test_addpackage since addsitedir() essentially just
142 # calls addpackage() for every .pth file in the directory
143 pth_file = PthFile()
Brett Cannonee86a662004-07-13 07:12:25 +0000144 pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
145 # that is tested for
Brett Cannon0096e262004-06-05 01:12:51 +0000146 try:
Brett Cannonee86a662004-07-13 07:12:25 +0000147 pth_file.create()
Brett Cannon64a84702004-07-10 02:10:45 +0000148 site.addsitedir(pth_file.base_dir, set())
Brett Cannonee86a662004-07-13 07:12:25 +0000149 self.pth_file_tests(pth_file)
Brett Cannon0096e262004-06-05 01:12:51 +0000150 finally:
Brett Cannon64a84702004-07-10 02:10:45 +0000151 pth_file.cleanup()
Brett Cannon0096e262004-06-05 01:12:51 +0000152
Christian Heimes8dc226f2008-05-06 23:45:46 +0000153 def test_s_option(self):
154 usersite = site.USER_SITE
Georg Brandlab91fde2009-08-13 08:51:18 +0000155 self.assertTrue(usersite in sys.path)
Christian Heimes8dc226f2008-05-06 23:45:46 +0000156
157 rc = subprocess.call([sys.executable, '-c',
Benjamin Petersonfea6a942008-07-02 16:11:42 +0000158 'import sys; sys.exit(%r in sys.path)' % usersite])
Christian Heimes8dc226f2008-05-06 23:45:46 +0000159 self.assertEqual(rc, 1)
160
161 rc = subprocess.call([sys.executable, '-s', '-c',
Benjamin Petersonfea6a942008-07-02 16:11:42 +0000162 'import sys; sys.exit(%r in sys.path)' % usersite])
Christian Heimes8dc226f2008-05-06 23:45:46 +0000163 self.assertEqual(rc, 0)
164
165 env = os.environ.copy()
166 env["PYTHONNOUSERSITE"] = "1"
167 rc = subprocess.call([sys.executable, '-c',
Benjamin Petersonfea6a942008-07-02 16:11:42 +0000168 'import sys; sys.exit(%r in sys.path)' % usersite],
Christian Heimes8dc226f2008-05-06 23:45:46 +0000169 env=env)
170 self.assertEqual(rc, 0)
171
172 env = os.environ.copy()
173 env["PYTHONUSERBASE"] = "/tmp"
174 rc = subprocess.call([sys.executable, '-c',
175 'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'],
176 env=env)
177 self.assertEqual(rc, 1)
178
179
Brett Cannon64a84702004-07-10 02:10:45 +0000180class PthFile(object):
181 """Helper class for handling testing of .pth files"""
Brett Cannon0096e262004-06-05 01:12:51 +0000182
Brett Cannon64a84702004-07-10 02:10:45 +0000183 def __init__(self, filename_base=TESTFN, imported="time",
184 good_dirname="__testdir__", bad_dirname="__bad"):
185 """Initialize instance variables"""
186 self.filename = filename_base + ".pth"
187 self.base_dir = os.path.abspath('')
188 self.file_path = os.path.join(self.base_dir, self.filename)
Brett Cannonee86a662004-07-13 07:12:25 +0000189 self.imported = imported
Brett Cannon64a84702004-07-10 02:10:45 +0000190 self.good_dirname = good_dirname
191 self.bad_dirname = bad_dirname
192 self.good_dir_path = os.path.join(self.base_dir, self.good_dirname)
193 self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname)
Brett Cannon0096e262004-06-05 01:12:51 +0000194
Brett Cannon64a84702004-07-10 02:10:45 +0000195 def create(self):
196 """Create a .pth file with a comment, blank lines, an ``import
197 <self.imported>``, a line with self.good_dirname, and a line with
198 self.bad_dirname.
Tim Peters182b5ac2004-07-18 06:16:08 +0000199
Brett Cannon64a84702004-07-10 02:10:45 +0000200 Creation of the directory for self.good_dir_path (based off of
201 self.good_dirname) is also performed.
Brett Cannon0096e262004-06-05 01:12:51 +0000202
Brett Cannon64a84702004-07-10 02:10:45 +0000203 Make sure to call self.cleanup() to undo anything done by this method.
Tim Peters182b5ac2004-07-18 06:16:08 +0000204
Brett Cannon64a84702004-07-10 02:10:45 +0000205 """
Michael W. Hudsonff522862005-05-27 14:58:06 +0000206 FILE = open(self.file_path, 'w')
Brett Cannon64a84702004-07-10 02:10:45 +0000207 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000208 print("#import @bad module name", file=FILE)
209 print("\n", file=FILE)
210 print("import %s" % self.imported, file=FILE)
211 print(self.good_dirname, file=FILE)
212 print(self.bad_dirname, file=FILE)
Brett Cannon64a84702004-07-10 02:10:45 +0000213 finally:
214 FILE.close()
215 os.mkdir(self.good_dir_path)
216
Brett Cannonee86a662004-07-13 07:12:25 +0000217 def cleanup(self, prep=False):
Brett Cannon64a84702004-07-10 02:10:45 +0000218 """Make sure that the .pth file is deleted, self.imported is not in
219 sys.modules, and that both self.good_dirname and self.bad_dirname are
220 not existing directories."""
Brett Cannonee86a662004-07-13 07:12:25 +0000221 if os.path.exists(self.file_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000222 os.remove(self.file_path)
Brett Cannonee86a662004-07-13 07:12:25 +0000223 if prep:
224 self.imported_module = sys.modules.get(self.imported)
225 if self.imported_module:
226 del sys.modules[self.imported]
227 else:
228 if self.imported_module:
229 sys.modules[self.imported] = self.imported_module
230 if os.path.exists(self.good_dir_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000231 os.rmdir(self.good_dir_path)
Brett Cannonee86a662004-07-13 07:12:25 +0000232 if os.path.exists(self.bad_dir_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000233 os.rmdir(self.bad_dir_path)
Brett Cannon0096e262004-06-05 01:12:51 +0000234
235class ImportSideEffectTests(unittest.TestCase):
236 """Test side-effects from importing 'site'."""
237
238 def setUp(self):
239 """Make a copy of sys.path"""
240 self.sys_path = sys.path[:]
241
242 def tearDown(self):
243 """Restore sys.path"""
244 sys.path = self.sys_path
245
246 def test_abs__file__(self):
247 # Make sure all imported modules have their __file__ attribute
248 # as an absolute path.
249 # Handled by abs__file__()
250 site.abs__file__()
Georg Brandl1a3284e2007-12-02 09:40:06 +0000251 for module in (sys, os, builtins):
Brett Cannon0096e262004-06-05 01:12:51 +0000252 try:
Georg Brandlab91fde2009-08-13 08:51:18 +0000253 self.assertTrue(os.path.isabs(module.__file__), repr(module))
Brett Cannon0096e262004-06-05 01:12:51 +0000254 except AttributeError:
255 continue
Raymond Hettingerebd95222004-06-27 03:02:18 +0000256 # We could try everything in sys.modules; however, when regrtest.py
257 # runs something like test_frozen before test_site, then we will
258 # be testing things loaded *after* test_site did path normalization
Brett Cannon0096e262004-06-05 01:12:51 +0000259
260 def test_no_duplicate_paths(self):
261 # No duplicate paths should exist in sys.path
262 # Handled by removeduppaths()
263 site.removeduppaths()
264 seen_paths = set()
265 for path in sys.path:
Georg Brandlab91fde2009-08-13 08:51:18 +0000266 self.assertTrue(path not in seen_paths)
Brett Cannon0096e262004-06-05 01:12:51 +0000267 seen_paths.add(path)
268
269 def test_add_build_dir(self):
270 # Test that the build directory's Modules directory is used when it
271 # should be.
272 # XXX: implement
273 pass
274
Brett Cannon0096e262004-06-05 01:12:51 +0000275 def test_setting_quit(self):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000276 # 'quit' and 'exit' should be injected into builtins
Georg Brandlab91fde2009-08-13 08:51:18 +0000277 self.assertTrue(hasattr(builtins, "quit"))
278 self.assertTrue(hasattr(builtins, "exit"))
Brett Cannon0096e262004-06-05 01:12:51 +0000279
280 def test_setting_copyright(self):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000281 # 'copyright' and 'credits' should be in builtins
Georg Brandlab91fde2009-08-13 08:51:18 +0000282 self.assertTrue(hasattr(builtins, "copyright"))
283 self.assertTrue(hasattr(builtins, "credits"))
Brett Cannon0096e262004-06-05 01:12:51 +0000284
285 def test_setting_help(self):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000286 # 'help' should be set in builtins
Georg Brandlab91fde2009-08-13 08:51:18 +0000287 self.assertTrue(hasattr(builtins, "help"))
Brett Cannon0096e262004-06-05 01:12:51 +0000288
289 def test_aliasing_mbcs(self):
290 if sys.platform == "win32":
291 import locale
292 if locale.getdefaultlocale()[1].startswith('cp'):
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000293 for value in encodings.aliases.aliases.values():
Brett Cannon0096e262004-06-05 01:12:51 +0000294 if value == "mbcs":
295 break
296 else:
297 self.fail("did not alias mbcs")
298
299 def test_setdefaultencoding_removed(self):
300 # Make sure sys.setdefaultencoding is gone
Georg Brandlab91fde2009-08-13 08:51:18 +0000301 self.assertTrue(not hasattr(sys, "setdefaultencoding"))
Brett Cannon0096e262004-06-05 01:12:51 +0000302
303 def test_sitecustomize_executed(self):
304 # If sitecustomize is available, it should have been imported.
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000305 if "sitecustomize" not in sys.modules:
Brett Cannon0096e262004-06-05 01:12:51 +0000306 try:
307 import sitecustomize
308 except ImportError:
309 pass
310 else:
311 self.fail("sitecustomize not imported automatically")
312
Brett Cannon0096e262004-06-05 01:12:51 +0000313def test_main():
314 run_unittest(HelperFunctionsTests, ImportSideEffectTests)
315
Brett Cannon0096e262004-06-05 01:12:51 +0000316if __name__ == "__main__":
317 test_main()