blob: 0f8405a543eea024aa2dac5a2376b501c3696a16 [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örwald4b965f62009-04-26 20:51:44 +00008from test.test_support import run_unittest, TESTFN, EnvironmentVarGuard
R. David Murray5874ed62010-12-26 22:29:53 +00009from test.test_support import captured_output
Brett Cannon0096e262004-06-05 01:12:51 +000010import __builtin__
11import os
12import sys
13import encodings
Christian Heimesaf748c32008-05-06 22:41:46 +000014import subprocess
Tarek Ziadé5633a802010-01-23 09:23:15 +000015import sysconfig
16from copy import copy
17
Brett Cannon0096e262004-06-05 01:12:51 +000018# Need to make sure to not import 'site' if someone specified ``-S`` at the
19# command-line. Detect this by just making sure 'site' has not been imported
20# already.
21if "site" in sys.modules:
22 import site
23else:
Benjamin Petersonbec087f2009-03-26 21:10:30 +000024 raise unittest.SkipTest("importation of site.py suppressed")
Brett Cannon0096e262004-06-05 01:12:51 +000025
Christian Heimesaf748c32008-05-06 22:41:46 +000026if not os.path.isdir(site.USER_SITE):
27 # need to add user site directory for tests
28 os.makedirs(site.USER_SITE)
29 site.addsitedir(site.USER_SITE)
30
Brett Cannon0096e262004-06-05 01:12:51 +000031class HelperFunctionsTests(unittest.TestCase):
32 """Tests for helper functions.
Raymond Hettingerebd95222004-06-27 03:02:18 +000033
Brett Cannon0096e262004-06-05 01:12:51 +000034 The setting of the encoding (set using sys.setdefaultencoding) used by
35 the Unicode implementation is not tested.
Raymond Hettingerebd95222004-06-27 03:02:18 +000036
Brett Cannon0096e262004-06-05 01:12:51 +000037 """
38
39 def setUp(self):
40 """Save a copy of sys.path"""
41 self.sys_path = sys.path[:]
Tarek Ziadé764fc232009-08-20 21:23:13 +000042 self.old_base = site.USER_BASE
43 self.old_site = site.USER_SITE
44 self.old_prefixes = site.PREFIXES
Tarek Ziadé5633a802010-01-23 09:23:15 +000045 self.old_vars = copy(sysconfig._CONFIG_VARS)
Brett Cannon0096e262004-06-05 01:12:51 +000046
Neal Norwitz40388cc2008-05-14 06:47:56 +000047 def tearDown(self):
Brett Cannon0096e262004-06-05 01:12:51 +000048 """Restore sys.path"""
Nick Coghlana0e0f9e2009-10-17 16:19:51 +000049 sys.path[:] = self.sys_path
Tarek Ziadé764fc232009-08-20 21:23:13 +000050 site.USER_BASE = self.old_base
51 site.USER_SITE = self.old_site
52 site.PREFIXES = self.old_prefixes
Tarek Ziadé5633a802010-01-23 09:23:15 +000053 sysconfig._CONFIG_VARS = self.old_vars
Raymond Hettingerebd95222004-06-27 03:02:18 +000054
Brett Cannon0096e262004-06-05 01:12:51 +000055 def test_makepath(self):
56 # Test makepath() have an absolute path for its first return value
57 # and a case-normalized version of the absolute path for its
58 # second value.
59 path_parts = ("Beginning", "End")
60 original_dir = os.path.join(*path_parts)
61 abs_dir, norm_dir = site.makepath(*path_parts)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000062 self.assertEqual(os.path.abspath(original_dir), abs_dir)
Brett Cannon0096e262004-06-05 01:12:51 +000063 if original_dir == os.path.normcase(original_dir):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000064 self.assertEqual(abs_dir, norm_dir)
Brett Cannon0096e262004-06-05 01:12:51 +000065 else:
Benjamin Peterson5c8da862009-06-30 22:57:08 +000066 self.assertEqual(os.path.normcase(abs_dir), norm_dir)
Brett Cannon0096e262004-06-05 01:12:51 +000067
68 def test_init_pathinfo(self):
69 dir_set = site._init_pathinfo()
70 for entry in [site.makepath(path)[1] for path in sys.path
71 if path and os.path.isdir(path)]:
Ezio Melottiaa980582010-01-23 23:04:36 +000072 self.assertIn(entry, dir_set,
73 "%s from sys.path not found in set returned "
74 "by _init_pathinfo(): %s" % (entry, dir_set))
Raymond Hettingerebd95222004-06-27 03:02:18 +000075
Brett Cannonee86a662004-07-13 07:12:25 +000076 def pth_file_tests(self, pth_file):
77 """Contain common code for testing results of reading a .pth file"""
Ezio Melottiaa980582010-01-23 23:04:36 +000078 self.assertIn(pth_file.imported, sys.modules,
79 "%s not in sys.modules" % pth_file.imported)
Antoine Pitroud8b16ab2009-11-01 23:54:20 +000080 self.assertIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
81 self.assertFalse(os.path.exists(pth_file.bad_dir_path))
Brett Cannonee86a662004-07-13 07:12:25 +000082
Brett Cannon0096e262004-06-05 01:12:51 +000083 def test_addpackage(self):
84 # Make sure addpackage() imports if the line starts with 'import',
Brett Cannon64a84702004-07-10 02:10:45 +000085 # adds directories to sys.path for any line in the file that is not a
86 # comment or import that is a valid directory name for where the .pth
87 # file resides; invalid directories are not added
88 pth_file = PthFile()
Brett Cannonee86a662004-07-13 07:12:25 +000089 pth_file.cleanup(prep=True) # to make sure that nothing is
90 # pre-existing that shouldn't be
Brett Cannon0096e262004-06-05 01:12:51 +000091 try:
Brett Cannon64a84702004-07-10 02:10:45 +000092 pth_file.create()
93 site.addpackage(pth_file.base_dir, pth_file.filename, set())
Brett Cannonee86a662004-07-13 07:12:25 +000094 self.pth_file_tests(pth_file)
Brett Cannon0096e262004-06-05 01:12:51 +000095 finally:
Brett Cannon64a84702004-07-10 02:10:45 +000096 pth_file.cleanup()
Raymond Hettingerebd95222004-06-27 03:02:18 +000097
R. David Murray5874ed62010-12-26 22:29:53 +000098 def make_pth(self, contents, pth_dir='.', pth_name=TESTFN):
99 # Create a .pth file and return its (abspath, basename).
100 pth_dir = os.path.abspath(pth_dir)
101 pth_basename = pth_name + '.pth'
102 pth_fn = os.path.join(pth_dir, pth_basename)
103 pth_file = open(pth_fn, 'w')
104 self.addCleanup(lambda: os.remove(pth_fn))
105 pth_file.write(contents)
106 pth_file.close()
107 return pth_dir, pth_basename
108
109 def test_addpackage_import_bad_syntax(self):
110 # Issue 10642
111 pth_dir, pth_fn = self.make_pth("import bad)syntax\n")
112 with captured_output("stderr") as err_out:
113 site.addpackage(pth_dir, pth_fn, set())
114 self.assertRegexpMatches(err_out.getvalue(), "line 1")
115 self.assertRegexpMatches(err_out.getvalue(), os.path.join(pth_dir, pth_fn))
116 # XXX: the previous two should be independent checks so that the
117 # order doesn't matter. The next three could be a single check
118 # but my regex foo isn't good enough to write it.
119 self.assertRegexpMatches(err_out.getvalue(), 'Traceback')
120 self.assertRegexpMatches(err_out.getvalue(), r'import bad\)syntax')
121 self.assertRegexpMatches(err_out.getvalue(), 'SyntaxError')
122
123 def test_addpackage_import_bad_exec(self):
124 # Issue 10642
125 pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n")
126 with captured_output("stderr") as err_out:
127 site.addpackage(pth_dir, pth_fn, set())
128 self.assertRegexpMatches(err_out.getvalue(), "line 2")
129 self.assertRegexpMatches(err_out.getvalue(), os.path.join(pth_dir, pth_fn))
130 # XXX: ditto previous XXX comment.
131 self.assertRegexpMatches(err_out.getvalue(), 'Traceback')
132 self.assertRegexpMatches(err_out.getvalue(), 'ImportError')
133
134 def test_addpackage_import_bad_pth_file(self):
135 # Issue 5258
136 pth_dir, pth_fn = self.make_pth("abc\x00def\n")
137 with captured_output("stderr") as err_out:
138 site.addpackage(pth_dir, pth_fn, set())
139 self.assertRegexpMatches(err_out.getvalue(), "line 1")
140 self.assertRegexpMatches(err_out.getvalue(), os.path.join(pth_dir, pth_fn))
141 # XXX: ditto previous XXX comment.
142 self.assertRegexpMatches(err_out.getvalue(), 'Traceback')
143 self.assertRegexpMatches(err_out.getvalue(), 'TypeError')
144
Brett Cannon0096e262004-06-05 01:12:51 +0000145 def test_addsitedir(self):
Brett Cannon64a84702004-07-10 02:10:45 +0000146 # Same tests for test_addpackage since addsitedir() essentially just
147 # calls addpackage() for every .pth file in the directory
148 pth_file = PthFile()
Brett Cannonee86a662004-07-13 07:12:25 +0000149 pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
150 # that is tested for
Brett Cannon0096e262004-06-05 01:12:51 +0000151 try:
Brett Cannonee86a662004-07-13 07:12:25 +0000152 pth_file.create()
Brett Cannon64a84702004-07-10 02:10:45 +0000153 site.addsitedir(pth_file.base_dir, set())
Brett Cannonee86a662004-07-13 07:12:25 +0000154 self.pth_file_tests(pth_file)
Brett Cannon0096e262004-06-05 01:12:51 +0000155 finally:
Brett Cannon64a84702004-07-10 02:10:45 +0000156 pth_file.cleanup()
Brett Cannon0096e262004-06-05 01:12:51 +0000157
Christian Heimesaf748c32008-05-06 22:41:46 +0000158 def test_s_option(self):
159 usersite = site.USER_SITE
Antoine Pitroud8b16ab2009-11-01 23:54:20 +0000160 self.assertIn(usersite, sys.path)
Christian Heimesaf748c32008-05-06 22:41:46 +0000161
162 rc = subprocess.call([sys.executable, '-c',
Antoine Pitroub03f5322009-02-22 18:20:46 +0000163 'import sys; sys.exit(%r in sys.path)' % usersite])
Brett Cannonb7019d82009-02-24 22:01:02 +0000164 self.assertEqual(rc, 1, "%r is not in sys.path (sys.exit returned %r)"
165 % (usersite, rc))
Christian Heimesaf748c32008-05-06 22:41:46 +0000166
167 rc = subprocess.call([sys.executable, '-s', '-c',
Amaury Forgeot d'Arc9b69ed92008-06-19 21:17:12 +0000168 'import sys; sys.exit(%r in sys.path)' % usersite])
Christian Heimesaf748c32008-05-06 22:41:46 +0000169 self.assertEqual(rc, 0)
170
171 env = os.environ.copy()
172 env["PYTHONNOUSERSITE"] = "1"
173 rc = subprocess.call([sys.executable, '-c',
Amaury Forgeot d'Arc9b69ed92008-06-19 21:17:12 +0000174 'import sys; sys.exit(%r in sys.path)' % usersite],
Christian Heimesaf748c32008-05-06 22:41:46 +0000175 env=env)
176 self.assertEqual(rc, 0)
177
178 env = os.environ.copy()
179 env["PYTHONUSERBASE"] = "/tmp"
180 rc = subprocess.call([sys.executable, '-c',
181 'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'],
182 env=env)
183 self.assertEqual(rc, 1)
184
Tarek Ziadé764fc232009-08-20 21:23:13 +0000185 def test_getuserbase(self):
186 site.USER_BASE = None
187 user_base = site.getuserbase()
188
189 # the call sets site.USER_BASE
Ezio Melotti2623a372010-11-21 13:34:58 +0000190 self.assertEqual(site.USER_BASE, user_base)
Tarek Ziadé764fc232009-08-20 21:23:13 +0000191
192 # let's set PYTHONUSERBASE and see if it uses it
193 site.USER_BASE = None
Tarek Ziadé5633a802010-01-23 09:23:15 +0000194 import sysconfig
195 sysconfig._CONFIG_VARS = None
196
Tarek Ziadé764fc232009-08-20 21:23:13 +0000197 with EnvironmentVarGuard() as environ:
198 environ['PYTHONUSERBASE'] = 'xoxo'
Antoine Pitroud8b16ab2009-11-01 23:54:20 +0000199 self.assertTrue(site.getuserbase().startswith('xoxo'),
200 site.getuserbase())
Tarek Ziadé764fc232009-08-20 21:23:13 +0000201
202 def test_getusersitepackages(self):
203 site.USER_SITE = None
204 site.USER_BASE = None
205 user_site = site.getusersitepackages()
206
207 # the call sets USER_BASE *and* USER_SITE
Ezio Melotti2623a372010-11-21 13:34:58 +0000208 self.assertEqual(site.USER_SITE, user_site)
Antoine Pitroud8b16ab2009-11-01 23:54:20 +0000209 self.assertTrue(user_site.startswith(site.USER_BASE), user_site)
Tarek Ziadé764fc232009-08-20 21:23:13 +0000210
211 def test_getsitepackages(self):
212 site.PREFIXES = ['xoxo']
213 dirs = site.getsitepackages()
214
215 if sys.platform in ('os2emx', 'riscos'):
Antoine Pitroud8b16ab2009-11-01 23:54:20 +0000216 self.assertEqual(len(dirs), 1)
Tarek Ziadé764fc232009-08-20 21:23:13 +0000217 wanted = os.path.join('xoxo', 'Lib', 'site-packages')
Ezio Melotti2623a372010-11-21 13:34:58 +0000218 self.assertEqual(dirs[0], wanted)
Tarek Ziadé764fc232009-08-20 21:23:13 +0000219 elif os.sep == '/':
Ezio Melottid9ed62c2010-08-17 08:38:05 +0000220 self.assertEqual(len(dirs), 2)
Tarek Ziadé764fc232009-08-20 21:23:13 +0000221 wanted = os.path.join('xoxo', 'lib', 'python' + sys.version[:3],
222 'site-packages')
Ezio Melotti2623a372010-11-21 13:34:58 +0000223 self.assertEqual(dirs[0], wanted)
Tarek Ziadé764fc232009-08-20 21:23:13 +0000224 wanted = os.path.join('xoxo', 'lib', 'site-python')
Ezio Melotti2623a372010-11-21 13:34:58 +0000225 self.assertEqual(dirs[1], wanted)
Tarek Ziadé764fc232009-08-20 21:23:13 +0000226 else:
Ezio Melottid9ed62c2010-08-17 08:38:05 +0000227 self.assertEqual(len(dirs), 2)
Ezio Melotti2623a372010-11-21 13:34:58 +0000228 self.assertEqual(dirs[0], 'xoxo')
Tarek Ziadéd24cab82009-10-27 21:20:27 +0000229 wanted = os.path.join('xoxo', 'lib', 'site-packages')
Ezio Melotti2623a372010-11-21 13:34:58 +0000230 self.assertEqual(dirs[1], wanted)
Tarek Ziadé764fc232009-08-20 21:23:13 +0000231
232 # let's try the specific Apple location
Brett Cannonda9af752010-05-13 23:59:41 +0000233 if (sys.platform == "darwin" and
234 sysconfig.get_config_var("PYTHONFRAMEWORK")):
Tarek Ziadé764fc232009-08-20 21:23:13 +0000235 site.PREFIXES = ['Python.framework']
236 dirs = site.getsitepackages()
Antoine Pitroud8b16ab2009-11-01 23:54:20 +0000237 self.assertEqual(len(dirs), 4)
Tarek Ziadé764fc232009-08-20 21:23:13 +0000238 wanted = os.path.join('~', 'Library', 'Python',
239 sys.version[:3], 'site-packages')
Ezio Melotti2623a372010-11-21 13:34:58 +0000240 self.assertEqual(dirs[2], os.path.expanduser(wanted))
Tarek Ziadé764fc232009-08-20 21:23:13 +0000241 wanted = os.path.join('/Library', 'Python', sys.version[:3],
242 'site-packages')
Ezio Melotti2623a372010-11-21 13:34:58 +0000243 self.assertEqual(dirs[3], wanted)
Christian Heimesaf748c32008-05-06 22:41:46 +0000244
Brett Cannon64a84702004-07-10 02:10:45 +0000245class PthFile(object):
246 """Helper class for handling testing of .pth files"""
Brett Cannon0096e262004-06-05 01:12:51 +0000247
Brett Cannon64a84702004-07-10 02:10:45 +0000248 def __init__(self, filename_base=TESTFN, imported="time",
249 good_dirname="__testdir__", bad_dirname="__bad"):
250 """Initialize instance variables"""
251 self.filename = filename_base + ".pth"
252 self.base_dir = os.path.abspath('')
253 self.file_path = os.path.join(self.base_dir, self.filename)
Brett Cannonee86a662004-07-13 07:12:25 +0000254 self.imported = imported
Brett Cannon64a84702004-07-10 02:10:45 +0000255 self.good_dirname = good_dirname
256 self.bad_dirname = bad_dirname
257 self.good_dir_path = os.path.join(self.base_dir, self.good_dirname)
258 self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname)
Brett Cannon0096e262004-06-05 01:12:51 +0000259
Brett Cannon64a84702004-07-10 02:10:45 +0000260 def create(self):
261 """Create a .pth file with a comment, blank lines, an ``import
262 <self.imported>``, a line with self.good_dirname, and a line with
263 self.bad_dirname.
Tim Peters182b5ac2004-07-18 06:16:08 +0000264
Brett Cannon64a84702004-07-10 02:10:45 +0000265 Creation of the directory for self.good_dir_path (based off of
266 self.good_dirname) is also performed.
Brett Cannon0096e262004-06-05 01:12:51 +0000267
Brett Cannon64a84702004-07-10 02:10:45 +0000268 Make sure to call self.cleanup() to undo anything done by this method.
Tim Peters182b5ac2004-07-18 06:16:08 +0000269
Brett Cannon64a84702004-07-10 02:10:45 +0000270 """
Michael W. Hudsonff522862005-05-27 14:58:06 +0000271 FILE = open(self.file_path, 'w')
Brett Cannon64a84702004-07-10 02:10:45 +0000272 try:
273 print>>FILE, "#import @bad module name"
274 print>>FILE, "\n"
275 print>>FILE, "import %s" % self.imported
276 print>>FILE, self.good_dirname
277 print>>FILE, self.bad_dirname
278 finally:
279 FILE.close()
280 os.mkdir(self.good_dir_path)
281
Brett Cannonee86a662004-07-13 07:12:25 +0000282 def cleanup(self, prep=False):
Brett Cannon64a84702004-07-10 02:10:45 +0000283 """Make sure that the .pth file is deleted, self.imported is not in
284 sys.modules, and that both self.good_dirname and self.bad_dirname are
285 not existing directories."""
Brett Cannonee86a662004-07-13 07:12:25 +0000286 if os.path.exists(self.file_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000287 os.remove(self.file_path)
Brett Cannonee86a662004-07-13 07:12:25 +0000288 if prep:
289 self.imported_module = sys.modules.get(self.imported)
290 if self.imported_module:
291 del sys.modules[self.imported]
292 else:
293 if self.imported_module:
294 sys.modules[self.imported] = self.imported_module
295 if os.path.exists(self.good_dir_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000296 os.rmdir(self.good_dir_path)
Brett Cannonee86a662004-07-13 07:12:25 +0000297 if os.path.exists(self.bad_dir_path):
Brett Cannon64a84702004-07-10 02:10:45 +0000298 os.rmdir(self.bad_dir_path)
Brett Cannon0096e262004-06-05 01:12:51 +0000299
300class ImportSideEffectTests(unittest.TestCase):
301 """Test side-effects from importing 'site'."""
302
303 def setUp(self):
304 """Make a copy of sys.path"""
305 self.sys_path = sys.path[:]
306
307 def tearDown(self):
308 """Restore sys.path"""
Nick Coghlana0e0f9e2009-10-17 16:19:51 +0000309 sys.path[:] = self.sys_path
Brett Cannon0096e262004-06-05 01:12:51 +0000310
311 def test_abs__file__(self):
312 # Make sure all imported modules have their __file__ attribute
313 # as an absolute path.
314 # Handled by abs__file__()
315 site.abs__file__()
Raymond Hettingerebd95222004-06-27 03:02:18 +0000316 for module in (sys, os, __builtin__):
Brett Cannon0096e262004-06-05 01:12:51 +0000317 try:
Ezio Melottidde5b942010-02-03 05:37:26 +0000318 self.assertTrue(os.path.isabs(module.__file__), repr(module))
Brett Cannon0096e262004-06-05 01:12:51 +0000319 except AttributeError:
320 continue
Raymond Hettingerebd95222004-06-27 03:02:18 +0000321 # We could try everything in sys.modules; however, when regrtest.py
322 # runs something like test_frozen before test_site, then we will
323 # be testing things loaded *after* test_site did path normalization
Brett Cannon0096e262004-06-05 01:12:51 +0000324
325 def test_no_duplicate_paths(self):
326 # No duplicate paths should exist in sys.path
327 # Handled by removeduppaths()
328 site.removeduppaths()
329 seen_paths = set()
330 for path in sys.path:
Ezio Melottiaa980582010-01-23 23:04:36 +0000331 self.assertNotIn(path, seen_paths)
Brett Cannon0096e262004-06-05 01:12:51 +0000332 seen_paths.add(path)
333
334 def test_add_build_dir(self):
335 # Test that the build directory's Modules directory is used when it
336 # should be.
337 # XXX: implement
338 pass
339
Brett Cannon0096e262004-06-05 01:12:51 +0000340 def test_setting_quit(self):
341 # 'quit' and 'exit' should be injected into __builtin__
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000342 self.assertTrue(hasattr(__builtin__, "quit"))
343 self.assertTrue(hasattr(__builtin__, "exit"))
Brett Cannon0096e262004-06-05 01:12:51 +0000344
345 def test_setting_copyright(self):
346 # 'copyright' and 'credits' should be in __builtin__
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000347 self.assertTrue(hasattr(__builtin__, "copyright"))
348 self.assertTrue(hasattr(__builtin__, "credits"))
Brett Cannon0096e262004-06-05 01:12:51 +0000349
350 def test_setting_help(self):
351 # 'help' should be set in __builtin__
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000352 self.assertTrue(hasattr(__builtin__, "help"))
Brett Cannon0096e262004-06-05 01:12:51 +0000353
354 def test_aliasing_mbcs(self):
355 if sys.platform == "win32":
356 import locale
357 if locale.getdefaultlocale()[1].startswith('cp'):
358 for value in encodings.aliases.aliases.itervalues():
359 if value == "mbcs":
360 break
361 else:
362 self.fail("did not alias mbcs")
363
364 def test_setdefaultencoding_removed(self):
365 # Make sure sys.setdefaultencoding is gone
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000366 self.assertTrue(not hasattr(sys, "setdefaultencoding"))
Brett Cannon0096e262004-06-05 01:12:51 +0000367
368 def test_sitecustomize_executed(self):
369 # If sitecustomize is available, it should have been imported.
Ezio Melottidde5b942010-02-03 05:37:26 +0000370 if "sitecustomize" not in sys.modules:
Brett Cannon0096e262004-06-05 01:12:51 +0000371 try:
372 import sitecustomize
373 except ImportError:
374 pass
375 else:
376 self.fail("sitecustomize not imported automatically")
377
Brett Cannon0096e262004-06-05 01:12:51 +0000378def test_main():
379 run_unittest(HelperFunctionsTests, ImportSideEffectTests)
380
Brett Cannon0096e262004-06-05 01:12:51 +0000381if __name__ == "__main__":
382 test_main()