blob: 857cfbf03b17d3edded375b6fcb4d4e5883858c1 [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
126 def test_addpackage_import_bad_pth_file(self):
127 # Issue 5258
128 pth_dir, pth_fn = self.make_pth("abc\x00def\n")
129 with captured_output("stderr") as err_out:
130 site.addpackage(pth_dir, pth_fn, set())
131 self.assertRegexpMatches(err_out.getvalue(), "line 1")
R. David Murraya6405232010-12-27 00:07:32 +0000132 self.assertRegexpMatches(err_out.getvalue(),
133 re.escape(os.path.join(pth_dir, pth_fn)))
R. David Murray6cb252f2010-12-26 22:24:54 +0000134 # XXX: ditto previous XXX comment.
135 self.assertRegexpMatches(err_out.getvalue(), 'Traceback')
136 self.assertRegexpMatches(err_out.getvalue(), 'TypeError')
137
Brett Cannon0096e262004-06-05 01:12:51 +0000138 def test_addsitedir(self):
Brett Cannon64a84702004-07-10 02:10:45 +0000139 # Same tests for test_addpackage since addsitedir() essentially just
140 # calls addpackage() for every .pth file in the directory
141 pth_file = PthFile()
Brett Cannonee86a662004-07-13 07:12:25 +0000142 pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
143 # that is tested for
Brett Cannon0096e262004-06-05 01:12:51 +0000144 try:
Brett Cannonee86a662004-07-13 07:12:25 +0000145 pth_file.create()
Brett Cannon64a84702004-07-10 02:10:45 +0000146 site.addsitedir(pth_file.base_dir, set())
Brett Cannonee86a662004-07-13 07:12:25 +0000147 self.pth_file_tests(pth_file)
Brett Cannon0096e262004-06-05 01:12:51 +0000148 finally:
Brett Cannon64a84702004-07-10 02:10:45 +0000149 pth_file.cleanup()
Brett Cannon0096e262004-06-05 01:12:51 +0000150
Christian Heimes8dc226f2008-05-06 23:45:46 +0000151 def test_s_option(self):
152 usersite = site.USER_SITE
Georg Brandlab91fde2009-08-13 08:51:18 +0000153 self.assertTrue(usersite in sys.path)
Christian Heimes8dc226f2008-05-06 23:45:46 +0000154
155 rc = subprocess.call([sys.executable, '-c',
Benjamin Petersonfea6a942008-07-02 16:11:42 +0000156 'import sys; sys.exit(%r in sys.path)' % usersite])
Christian Heimes8dc226f2008-05-06 23:45:46 +0000157 self.assertEqual(rc, 1)
158
159 rc = subprocess.call([sys.executable, '-s', '-c',
Benjamin Petersonfea6a942008-07-02 16:11:42 +0000160 'import sys; sys.exit(%r in sys.path)' % usersite])
Christian Heimes8dc226f2008-05-06 23:45:46 +0000161 self.assertEqual(rc, 0)
162
163 env = os.environ.copy()
164 env["PYTHONNOUSERSITE"] = "1"
165 rc = subprocess.call([sys.executable, '-c',
Benjamin Petersonfea6a942008-07-02 16:11:42 +0000166 'import sys; sys.exit(%r in sys.path)' % usersite],
Christian Heimes8dc226f2008-05-06 23:45:46 +0000167 env=env)
168 self.assertEqual(rc, 0)
169
170 env = os.environ.copy()
171 env["PYTHONUSERBASE"] = "/tmp"
172 rc = subprocess.call([sys.executable, '-c',
173 'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'],
174 env=env)
175 self.assertEqual(rc, 1)
176
177
Brett Cannon64a84702004-07-10 02:10:45 +0000178class PthFile(object):
179 """Helper class for handling testing of .pth files"""
Brett Cannon0096e262004-06-05 01:12:51 +0000180
Brett Cannon64a84702004-07-10 02:10:45 +0000181 def __init__(self, filename_base=TESTFN, imported="time",
182 good_dirname="__testdir__", bad_dirname="__bad"):
183 """Initialize instance variables"""
184 self.filename = filename_base + ".pth"
185 self.base_dir = os.path.abspath('')
186 self.file_path = os.path.join(self.base_dir, self.filename)
Brett Cannonee86a662004-07-13 07:12:25 +0000187 self.imported = imported
Brett Cannon64a84702004-07-10 02:10:45 +0000188 self.good_dirname = good_dirname
189 self.bad_dirname = bad_dirname
190 self.good_dir_path = os.path.join(self.base_dir, self.good_dirname)
191 self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname)
Brett Cannon0096e262004-06-05 01:12:51 +0000192
Brett Cannon64a84702004-07-10 02:10:45 +0000193 def create(self):
194 """Create a .pth file with a comment, blank lines, an ``import
195 <self.imported>``, a line with self.good_dirname, and a line with
196 self.bad_dirname.
Tim Peters182b5ac2004-07-18 06:16:08 +0000197
Brett Cannon64a84702004-07-10 02:10:45 +0000198 Creation of the directory for self.good_dir_path (based off of
199 self.good_dirname) is also performed.
Brett Cannon0096e262004-06-05 01:12:51 +0000200
Brett Cannon64a84702004-07-10 02:10:45 +0000201 Make sure to call self.cleanup() to undo anything done by this method.
Tim Peters182b5ac2004-07-18 06:16:08 +0000202
Brett Cannon64a84702004-07-10 02:10:45 +0000203 """
Michael W. Hudsonff522862005-05-27 14:58:06 +0000204 FILE = open(self.file_path, 'w')
Brett Cannon64a84702004-07-10 02:10:45 +0000205 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000206 print("#import @bad module name", file=FILE)
207 print("\n", file=FILE)
208 print("import %s" % self.imported, file=FILE)
209 print(self.good_dirname, file=FILE)
210 print(self.bad_dirname, file=FILE)
Brett Cannon64a84702004-07-10 02:10:45 +0000211 finally:
212 FILE.close()
213 os.mkdir(self.good_dir_path)
214
Brett Cannonee86a662004-07-13 07:12:25 +0000215 def cleanup(self, prep=False):
Brett Cannon64a84702004-07-10 02:10:45 +0000216 """Make sure that the .pth file is deleted, self.imported is not in
217 sys.modules, and that both self.good_dirname and self.bad_dirname are
218 not existing directories."""
Brett Cannonee86a662004-07-13 07:12:25 +0000219 if os.path.exists(self.file_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000220 os.remove(self.file_path)
Brett Cannonee86a662004-07-13 07:12:25 +0000221 if prep:
222 self.imported_module = sys.modules.get(self.imported)
223 if self.imported_module:
224 del sys.modules[self.imported]
225 else:
226 if self.imported_module:
227 sys.modules[self.imported] = self.imported_module
228 if os.path.exists(self.good_dir_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000229 os.rmdir(self.good_dir_path)
Brett Cannonee86a662004-07-13 07:12:25 +0000230 if os.path.exists(self.bad_dir_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000231 os.rmdir(self.bad_dir_path)
Brett Cannon0096e262004-06-05 01:12:51 +0000232
233class ImportSideEffectTests(unittest.TestCase):
234 """Test side-effects from importing 'site'."""
235
236 def setUp(self):
237 """Make a copy of sys.path"""
238 self.sys_path = sys.path[:]
239
240 def tearDown(self):
241 """Restore sys.path"""
242 sys.path = self.sys_path
243
244 def test_abs__file__(self):
245 # Make sure all imported modules have their __file__ attribute
246 # as an absolute path.
247 # Handled by abs__file__()
248 site.abs__file__()
Georg Brandl1a3284e2007-12-02 09:40:06 +0000249 for module in (sys, os, builtins):
Brett Cannon0096e262004-06-05 01:12:51 +0000250 try:
Georg Brandlab91fde2009-08-13 08:51:18 +0000251 self.assertTrue(os.path.isabs(module.__file__), repr(module))
Brett Cannon0096e262004-06-05 01:12:51 +0000252 except AttributeError:
253 continue
Raymond Hettingerebd95222004-06-27 03:02:18 +0000254 # We could try everything in sys.modules; however, when regrtest.py
255 # runs something like test_frozen before test_site, then we will
256 # be testing things loaded *after* test_site did path normalization
Brett Cannon0096e262004-06-05 01:12:51 +0000257
258 def test_no_duplicate_paths(self):
259 # No duplicate paths should exist in sys.path
260 # Handled by removeduppaths()
261 site.removeduppaths()
262 seen_paths = set()
263 for path in sys.path:
Georg Brandlab91fde2009-08-13 08:51:18 +0000264 self.assertTrue(path not in seen_paths)
Brett Cannon0096e262004-06-05 01:12:51 +0000265 seen_paths.add(path)
266
267 def test_add_build_dir(self):
268 # Test that the build directory's Modules directory is used when it
269 # should be.
270 # XXX: implement
271 pass
272
Brett Cannon0096e262004-06-05 01:12:51 +0000273 def test_setting_quit(self):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000274 # 'quit' and 'exit' should be injected into builtins
Georg Brandlab91fde2009-08-13 08:51:18 +0000275 self.assertTrue(hasattr(builtins, "quit"))
276 self.assertTrue(hasattr(builtins, "exit"))
Brett Cannon0096e262004-06-05 01:12:51 +0000277
278 def test_setting_copyright(self):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000279 # 'copyright' and 'credits' should be in builtins
Georg Brandlab91fde2009-08-13 08:51:18 +0000280 self.assertTrue(hasattr(builtins, "copyright"))
281 self.assertTrue(hasattr(builtins, "credits"))
Brett Cannon0096e262004-06-05 01:12:51 +0000282
283 def test_setting_help(self):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000284 # 'help' should be set in builtins
Georg Brandlab91fde2009-08-13 08:51:18 +0000285 self.assertTrue(hasattr(builtins, "help"))
Brett Cannon0096e262004-06-05 01:12:51 +0000286
287 def test_aliasing_mbcs(self):
288 if sys.platform == "win32":
289 import locale
290 if locale.getdefaultlocale()[1].startswith('cp'):
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000291 for value in encodings.aliases.aliases.values():
Brett Cannon0096e262004-06-05 01:12:51 +0000292 if value == "mbcs":
293 break
294 else:
295 self.fail("did not alias mbcs")
296
297 def test_setdefaultencoding_removed(self):
298 # Make sure sys.setdefaultencoding is gone
Georg Brandlab91fde2009-08-13 08:51:18 +0000299 self.assertTrue(not hasattr(sys, "setdefaultencoding"))
Brett Cannon0096e262004-06-05 01:12:51 +0000300
301 def test_sitecustomize_executed(self):
302 # If sitecustomize is available, it should have been imported.
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000303 if "sitecustomize" not in sys.modules:
Brett Cannon0096e262004-06-05 01:12:51 +0000304 try:
305 import sitecustomize
306 except ImportError:
307 pass
308 else:
309 self.fail("sitecustomize not imported automatically")
310
Brett Cannon0096e262004-06-05 01:12:51 +0000311def test_main():
312 run_unittest(HelperFunctionsTests, ImportSideEffectTests)
313
Brett Cannon0096e262004-06-05 01:12:51 +0000314if __name__ == "__main__":
315 test_main()