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 |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 9 | from test.support import run_unittest, TESTFN, EnvironmentVarGuard |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 10 | from test.support import captured_stderr |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 11 | import builtins |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 12 | import os |
| 13 | import sys |
R. David Murray | ab9d8d6 | 2010-12-27 00:03:13 +0000 | [diff] [blame] | 14 | import re |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 15 | import encodings |
R David Murray | 1bc6ceb | 2013-09-14 13:28:37 -0400 | [diff] [blame] | 16 | import urllib.request |
| 17 | import urllib.error |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 18 | import subprocess |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 19 | import sysconfig |
| 20 | from copy import copy |
| 21 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 22 | # Need to make sure to not import 'site' if someone specified ``-S`` at the |
| 23 | # command-line. Detect this by just making sure 'site' has not been imported |
| 24 | # already. |
| 25 | if "site" in sys.modules: |
| 26 | import site |
| 27 | else: |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 28 | raise unittest.SkipTest("importation of site.py suppressed") |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 29 | |
Ned Deily | 316f573 | 2011-10-31 16:16:35 -0700 | [diff] [blame] | 30 | if site.ENABLE_USER_SITE and not os.path.isdir(site.USER_SITE): |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 31 | # need to add user site directory for tests |
| 32 | os.makedirs(site.USER_SITE) |
| 33 | site.addsitedir(site.USER_SITE) |
| 34 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 35 | class HelperFunctionsTests(unittest.TestCase): |
| 36 | """Tests for helper functions. |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 37 | """ |
| 38 | |
| 39 | def setUp(self): |
| 40 | """Save a copy of sys.path""" |
| 41 | self.sys_path = sys.path[:] |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 42 | self.old_base = site.USER_BASE |
| 43 | self.old_site = site.USER_SITE |
| 44 | self.old_prefixes = site.PREFIXES |
Brett Cannon | 8ac95ee | 2012-04-04 17:31:16 -0400 | [diff] [blame] | 45 | self.original_vars = sysconfig._CONFIG_VARS |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 46 | self.old_vars = copy(sysconfig._CONFIG_VARS) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 47 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 48 | def tearDown(self): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 49 | """Restore sys.path""" |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 50 | sys.path[:] = self.sys_path |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 51 | site.USER_BASE = self.old_base |
| 52 | site.USER_SITE = self.old_site |
| 53 | site.PREFIXES = self.old_prefixes |
Brett Cannon | 8ac95ee | 2012-04-04 17:31:16 -0400 | [diff] [blame] | 54 | sysconfig._CONFIG_VARS = self.original_vars |
| 55 | sysconfig._CONFIG_VARS.clear() |
| 56 | sysconfig._CONFIG_VARS.update(self.old_vars) |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 57 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 58 | def test_makepath(self): |
| 59 | # Test makepath() have an absolute path for its first return value |
| 60 | # and a case-normalized version of the absolute path for its |
| 61 | # second value. |
| 62 | path_parts = ("Beginning", "End") |
| 63 | original_dir = os.path.join(*path_parts) |
| 64 | abs_dir, norm_dir = site.makepath(*path_parts) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 65 | self.assertEqual(os.path.abspath(original_dir), abs_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 66 | if original_dir == os.path.normcase(original_dir): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 67 | self.assertEqual(abs_dir, norm_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 68 | else: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 69 | self.assertEqual(os.path.normcase(abs_dir), norm_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 70 | |
| 71 | def test_init_pathinfo(self): |
| 72 | dir_set = site._init_pathinfo() |
| 73 | for entry in [site.makepath(path)[1] for path in sys.path |
| 74 | if path and os.path.isdir(path)]: |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 75 | self.assertIn(entry, dir_set, |
| 76 | "%s from sys.path not found in set returned " |
| 77 | "by _init_pathinfo(): %s" % (entry, dir_set)) |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 78 | |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 79 | def pth_file_tests(self, pth_file): |
| 80 | """Contain common code for testing results of reading a .pth file""" |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 81 | self.assertIn(pth_file.imported, sys.modules, |
| 82 | "%s not in sys.modules" % pth_file.imported) |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 83 | self.assertIn(site.makepath(pth_file.good_dir_path)[0], sys.path) |
| 84 | self.assertFalse(os.path.exists(pth_file.bad_dir_path)) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 85 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 86 | def test_addpackage(self): |
| 87 | # Make sure addpackage() imports if the line starts with 'import', |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 88 | # adds directories to sys.path for any line in the file that is not a |
| 89 | # comment or import that is a valid directory name for where the .pth |
| 90 | # file resides; invalid directories are not added |
| 91 | pth_file = PthFile() |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 92 | pth_file.cleanup(prep=True) # to make sure that nothing is |
| 93 | # pre-existing that shouldn't be |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 94 | try: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 95 | pth_file.create() |
| 96 | site.addpackage(pth_file.base_dir, pth_file.filename, set()) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 97 | self.pth_file_tests(pth_file) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 98 | finally: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 99 | pth_file.cleanup() |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 100 | |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 101 | def make_pth(self, contents, pth_dir='.', pth_name=TESTFN): |
| 102 | # Create a .pth file and return its (abspath, basename). |
| 103 | pth_dir = os.path.abspath(pth_dir) |
| 104 | pth_basename = pth_name + '.pth' |
| 105 | pth_fn = os.path.join(pth_dir, pth_basename) |
| 106 | pth_file = open(pth_fn, 'w', encoding='utf-8') |
| 107 | self.addCleanup(lambda: os.remove(pth_fn)) |
| 108 | pth_file.write(contents) |
| 109 | pth_file.close() |
| 110 | return pth_dir, pth_basename |
| 111 | |
| 112 | def test_addpackage_import_bad_syntax(self): |
| 113 | # Issue 10642 |
| 114 | pth_dir, pth_fn = self.make_pth("import bad)syntax\n") |
| 115 | with captured_stderr() as err_out: |
| 116 | site.addpackage(pth_dir, pth_fn, set()) |
| 117 | self.assertRegex(err_out.getvalue(), "line 1") |
R. David Murray | ab9d8d6 | 2010-12-27 00:03:13 +0000 | [diff] [blame] | 118 | self.assertRegex(err_out.getvalue(), |
| 119 | re.escape(os.path.join(pth_dir, pth_fn))) |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 120 | # XXX: the previous two should be independent checks so that the |
| 121 | # order doesn't matter. The next three could be a single check |
| 122 | # but my regex foo isn't good enough to write it. |
| 123 | self.assertRegex(err_out.getvalue(), 'Traceback') |
| 124 | self.assertRegex(err_out.getvalue(), r'import bad\)syntax') |
| 125 | self.assertRegex(err_out.getvalue(), 'SyntaxError') |
| 126 | |
| 127 | def test_addpackage_import_bad_exec(self): |
| 128 | # Issue 10642 |
| 129 | pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n") |
| 130 | with captured_stderr() as err_out: |
| 131 | site.addpackage(pth_dir, pth_fn, set()) |
| 132 | self.assertRegex(err_out.getvalue(), "line 2") |
R. David Murray | ab9d8d6 | 2010-12-27 00:03:13 +0000 | [diff] [blame] | 133 | self.assertRegex(err_out.getvalue(), |
| 134 | re.escape(os.path.join(pth_dir, pth_fn))) |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 135 | # XXX: ditto previous XXX comment. |
| 136 | self.assertRegex(err_out.getvalue(), 'Traceback') |
| 137 | self.assertRegex(err_out.getvalue(), 'ImportError') |
| 138 | |
R. David Murray | ad4ccfd | 2010-12-27 04:31:48 +0000 | [diff] [blame] | 139 | @unittest.skipIf(sys.platform == "win32", "Windows does not raise an " |
| 140 | "error for file paths containing null characters") |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 141 | def test_addpackage_import_bad_pth_file(self): |
| 142 | # Issue 5258 |
| 143 | pth_dir, pth_fn = self.make_pth("abc\x00def\n") |
| 144 | with captured_stderr() as err_out: |
| 145 | site.addpackage(pth_dir, pth_fn, set()) |
| 146 | self.assertRegex(err_out.getvalue(), "line 1") |
R. David Murray | ab9d8d6 | 2010-12-27 00:03:13 +0000 | [diff] [blame] | 147 | self.assertRegex(err_out.getvalue(), |
| 148 | re.escape(os.path.join(pth_dir, pth_fn))) |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 149 | # XXX: ditto previous XXX comment. |
| 150 | self.assertRegex(err_out.getvalue(), 'Traceback') |
| 151 | self.assertRegex(err_out.getvalue(), 'TypeError') |
| 152 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 153 | def test_addsitedir(self): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 154 | # Same tests for test_addpackage since addsitedir() essentially just |
| 155 | # calls addpackage() for every .pth file in the directory |
| 156 | pth_file = PthFile() |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 157 | pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing |
| 158 | # that is tested for |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 159 | try: |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 160 | pth_file.create() |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 161 | site.addsitedir(pth_file.base_dir, set()) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 162 | self.pth_file_tests(pth_file) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 163 | finally: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 164 | pth_file.cleanup() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 165 | |
Ned Deily | 316f573 | 2011-10-31 16:16:35 -0700 | [diff] [blame] | 166 | @unittest.skipUnless(site.ENABLE_USER_SITE, "requires access to PEP 370 " |
| 167 | "user-site (site.ENABLE_USER_SITE)") |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 168 | def test_s_option(self): |
| 169 | usersite = site.USER_SITE |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 170 | self.assertIn(usersite, sys.path) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 171 | |
Éric Araujo | 63ebe1c | 2011-01-03 17:51:11 +0000 | [diff] [blame] | 172 | env = os.environ.copy() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 173 | rc = subprocess.call([sys.executable, '-c', |
Éric Araujo | 63ebe1c | 2011-01-03 17:51:11 +0000 | [diff] [blame] | 174 | 'import sys; sys.exit(%r in sys.path)' % usersite], |
| 175 | env=env) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 176 | self.assertEqual(rc, 1) |
| 177 | |
Éric Araujo | 63ebe1c | 2011-01-03 17:51:11 +0000 | [diff] [blame] | 178 | env = os.environ.copy() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 179 | rc = subprocess.call([sys.executable, '-s', '-c', |
Éric Araujo | 63ebe1c | 2011-01-03 17:51:11 +0000 | [diff] [blame] | 180 | 'import sys; sys.exit(%r in sys.path)' % usersite], |
| 181 | env=env) |
Antoine Pitrou | a1782e1 | 2013-10-23 22:03:22 +0200 | [diff] [blame] | 182 | if usersite == site.getsitepackages()[0]: |
| 183 | self.assertEqual(rc, 1) |
| 184 | else: |
| 185 | self.assertEqual(rc, 0) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 186 | |
| 187 | env = os.environ.copy() |
| 188 | env["PYTHONNOUSERSITE"] = "1" |
| 189 | rc = subprocess.call([sys.executable, '-c', |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 190 | 'import sys; sys.exit(%r in sys.path)' % usersite], |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 191 | env=env) |
Antoine Pitrou | a1782e1 | 2013-10-23 22:03:22 +0200 | [diff] [blame] | 192 | if usersite == site.getsitepackages()[0]: |
| 193 | self.assertEqual(rc, 1) |
| 194 | else: |
| 195 | self.assertEqual(rc, 0) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 196 | |
| 197 | env = os.environ.copy() |
| 198 | env["PYTHONUSERBASE"] = "/tmp" |
| 199 | rc = subprocess.call([sys.executable, '-c', |
| 200 | 'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'], |
| 201 | env=env) |
| 202 | self.assertEqual(rc, 1) |
| 203 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 204 | def test_getuserbase(self): |
| 205 | site.USER_BASE = None |
| 206 | user_base = site.getuserbase() |
| 207 | |
| 208 | # the call sets site.USER_BASE |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 209 | self.assertEqual(site.USER_BASE, user_base) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 210 | |
| 211 | # let's set PYTHONUSERBASE and see if it uses it |
| 212 | site.USER_BASE = None |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 213 | import sysconfig |
| 214 | sysconfig._CONFIG_VARS = None |
| 215 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 216 | with EnvironmentVarGuard() as environ: |
| 217 | environ['PYTHONUSERBASE'] = 'xoxo' |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 218 | self.assertTrue(site.getuserbase().startswith('xoxo'), |
| 219 | site.getuserbase()) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 220 | |
| 221 | def test_getusersitepackages(self): |
| 222 | site.USER_SITE = None |
| 223 | site.USER_BASE = None |
| 224 | user_site = site.getusersitepackages() |
| 225 | |
| 226 | # the call sets USER_BASE *and* USER_SITE |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 227 | self.assertEqual(site.USER_SITE, user_site) |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 228 | self.assertTrue(user_site.startswith(site.USER_BASE), user_site) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 229 | |
| 230 | def test_getsitepackages(self): |
| 231 | site.PREFIXES = ['xoxo'] |
| 232 | dirs = site.getsitepackages() |
| 233 | |
| 234 | if sys.platform in ('os2emx', 'riscos'): |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 235 | self.assertEqual(len(dirs), 1) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 236 | wanted = os.path.join('xoxo', 'Lib', 'site-packages') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 237 | self.assertEqual(dirs[0], wanted) |
Ned Deily | d531b29 | 2012-02-06 00:58:18 +0100 | [diff] [blame] | 238 | elif (sys.platform == "darwin" and |
| 239 | sysconfig.get_config_var("PYTHONFRAMEWORK")): |
| 240 | # OS X framework builds |
| 241 | site.PREFIXES = ['Python.framework'] |
| 242 | dirs = site.getsitepackages() |
| 243 | self.assertEqual(len(dirs), 3) |
| 244 | wanted = os.path.join('/Library', |
| 245 | sysconfig.get_config_var("PYTHONFRAMEWORK"), |
| 246 | sys.version[:3], |
| 247 | 'site-packages') |
| 248 | self.assertEqual(dirs[2], wanted) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 249 | elif os.sep == '/': |
Ned Deily | d531b29 | 2012-02-06 00:58:18 +0100 | [diff] [blame] | 250 | # OS X non-framwework builds, Linux, FreeBSD, etc |
Ezio Melotti | fc8b205 | 2010-08-17 08:35:41 +0000 | [diff] [blame] | 251 | self.assertEqual(len(dirs), 2) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 252 | wanted = os.path.join('xoxo', 'lib', 'python' + sys.version[:3], |
| 253 | 'site-packages') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 254 | self.assertEqual(dirs[0], wanted) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 255 | wanted = os.path.join('xoxo', 'lib', 'site-python') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 256 | self.assertEqual(dirs[1], wanted) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 257 | else: |
Ned Deily | d531b29 | 2012-02-06 00:58:18 +0100 | [diff] [blame] | 258 | # other platforms |
Ezio Melotti | fc8b205 | 2010-08-17 08:35:41 +0000 | [diff] [blame] | 259 | self.assertEqual(len(dirs), 2) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 260 | self.assertEqual(dirs[0], 'xoxo') |
Tarek Ziadé | 8c0e217 | 2009-10-27 21:24:21 +0000 | [diff] [blame] | 261 | wanted = os.path.join('xoxo', 'lib', 'site-packages') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 262 | self.assertEqual(dirs[1], wanted) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 263 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 264 | class PthFile(object): |
| 265 | """Helper class for handling testing of .pth files""" |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 266 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 267 | def __init__(self, filename_base=TESTFN, imported="time", |
| 268 | good_dirname="__testdir__", bad_dirname="__bad"): |
| 269 | """Initialize instance variables""" |
| 270 | self.filename = filename_base + ".pth" |
| 271 | self.base_dir = os.path.abspath('') |
| 272 | self.file_path = os.path.join(self.base_dir, self.filename) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 273 | self.imported = imported |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 274 | self.good_dirname = good_dirname |
| 275 | self.bad_dirname = bad_dirname |
| 276 | self.good_dir_path = os.path.join(self.base_dir, self.good_dirname) |
| 277 | 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] | 278 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 279 | def create(self): |
| 280 | """Create a .pth file with a comment, blank lines, an ``import |
| 281 | <self.imported>``, a line with self.good_dirname, and a line with |
| 282 | self.bad_dirname. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 283 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 284 | Creation of the directory for self.good_dir_path (based off of |
| 285 | self.good_dirname) is also performed. |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 286 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 287 | 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] | 288 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 289 | """ |
Michael W. Hudson | ff52286 | 2005-05-27 14:58:06 +0000 | [diff] [blame] | 290 | FILE = open(self.file_path, 'w') |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 291 | try: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 292 | print("#import @bad module name", file=FILE) |
| 293 | print("\n", file=FILE) |
| 294 | print("import %s" % self.imported, file=FILE) |
| 295 | print(self.good_dirname, file=FILE) |
| 296 | print(self.bad_dirname, file=FILE) |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 297 | finally: |
| 298 | FILE.close() |
| 299 | os.mkdir(self.good_dir_path) |
| 300 | |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 301 | def cleanup(self, prep=False): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 302 | """Make sure that the .pth file is deleted, self.imported is not in |
| 303 | sys.modules, and that both self.good_dirname and self.bad_dirname are |
| 304 | not existing directories.""" |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 305 | if os.path.exists(self.file_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 306 | os.remove(self.file_path) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 307 | if prep: |
| 308 | self.imported_module = sys.modules.get(self.imported) |
| 309 | if self.imported_module: |
| 310 | del sys.modules[self.imported] |
| 311 | else: |
| 312 | if self.imported_module: |
| 313 | sys.modules[self.imported] = self.imported_module |
| 314 | if os.path.exists(self.good_dir_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 315 | os.rmdir(self.good_dir_path) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 316 | if os.path.exists(self.bad_dir_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 317 | os.rmdir(self.bad_dir_path) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 318 | |
| 319 | class ImportSideEffectTests(unittest.TestCase): |
| 320 | """Test side-effects from importing 'site'.""" |
| 321 | |
| 322 | def setUp(self): |
| 323 | """Make a copy of sys.path""" |
| 324 | self.sys_path = sys.path[:] |
| 325 | |
| 326 | def tearDown(self): |
| 327 | """Restore sys.path""" |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 328 | sys.path[:] = self.sys_path |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 329 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 330 | def test_abs_paths(self): |
| 331 | # Make sure all imported modules have their __file__ and __cached__ |
| 332 | # attributes as absolute paths. Arranging to put the Lib directory on |
| 333 | # PYTHONPATH would cause the os module to have a relative path for |
| 334 | # __file__ if abs_paths() does not get run. sys and builtins (the |
| 335 | # only other modules imported before site.py runs) do not have |
| 336 | # __file__ or __cached__ because they are built-in. |
| 337 | parent = os.path.relpath(os.path.dirname(os.__file__)) |
| 338 | env = os.environ.copy() |
| 339 | env['PYTHONPATH'] = parent |
Victor Stinner | f3bc258 | 2010-04-18 07:59:53 +0000 | [diff] [blame] | 340 | code = ('import os, sys', |
| 341 | # use ASCII to avoid locale issues with non-ASCII directories |
| 342 | 'os_file = os.__file__.encode("ascii", "backslashreplace")', |
| 343 | r'sys.stdout.buffer.write(os_file + b"\n")', |
| 344 | 'os_cached = os.__cached__.encode("ascii", "backslashreplace")', |
| 345 | r'sys.stdout.buffer.write(os_cached + b"\n")') |
| 346 | command = '\n'.join(code) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 347 | # First, prove that with -S (no 'import site'), the paths are |
| 348 | # relative. |
| 349 | proc = subprocess.Popen([sys.executable, '-S', '-c', command], |
| 350 | env=env, |
Victor Stinner | f3bc258 | 2010-04-18 07:59:53 +0000 | [diff] [blame] | 351 | stdout=subprocess.PIPE) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 352 | stdout, stderr = proc.communicate() |
Victor Stinner | f3bc258 | 2010-04-18 07:59:53 +0000 | [diff] [blame] | 353 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 354 | self.assertEqual(proc.returncode, 0) |
Victor Stinner | f3bc258 | 2010-04-18 07:59:53 +0000 | [diff] [blame] | 355 | os__file__, os__cached__ = stdout.splitlines()[:2] |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 356 | self.assertFalse(os.path.isabs(os__file__)) |
| 357 | self.assertFalse(os.path.isabs(os__cached__)) |
| 358 | # Now, with 'import site', it works. |
| 359 | proc = subprocess.Popen([sys.executable, '-c', command], |
| 360 | env=env, |
Victor Stinner | f3bc258 | 2010-04-18 07:59:53 +0000 | [diff] [blame] | 361 | stdout=subprocess.PIPE) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 362 | stdout, stderr = proc.communicate() |
| 363 | self.assertEqual(proc.returncode, 0) |
Victor Stinner | f3bc258 | 2010-04-18 07:59:53 +0000 | [diff] [blame] | 364 | os__file__, os__cached__ = stdout.splitlines()[:2] |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 365 | self.assertTrue(os.path.isabs(os__file__)) |
| 366 | self.assertTrue(os.path.isabs(os__cached__)) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 367 | |
| 368 | def test_no_duplicate_paths(self): |
| 369 | # No duplicate paths should exist in sys.path |
| 370 | # Handled by removeduppaths() |
| 371 | site.removeduppaths() |
| 372 | seen_paths = set() |
| 373 | for path in sys.path: |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 374 | self.assertNotIn(path, seen_paths) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 375 | seen_paths.add(path) |
| 376 | |
| 377 | def test_add_build_dir(self): |
| 378 | # Test that the build directory's Modules directory is used when it |
| 379 | # should be. |
| 380 | # XXX: implement |
| 381 | pass |
| 382 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 383 | def test_setting_quit(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 384 | # 'quit' and 'exit' should be injected into builtins |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 385 | self.assertTrue(hasattr(builtins, "quit")) |
| 386 | self.assertTrue(hasattr(builtins, "exit")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 387 | |
| 388 | def test_setting_copyright(self): |
Senthil Kumaran | 8ef519b | 2013-09-07 13:59:17 -0700 | [diff] [blame] | 389 | # 'copyright', 'credits', and 'license' should be in builtins |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 390 | self.assertTrue(hasattr(builtins, "copyright")) |
| 391 | self.assertTrue(hasattr(builtins, "credits")) |
Senthil Kumaran | 8ef519b | 2013-09-07 13:59:17 -0700 | [diff] [blame] | 392 | self.assertTrue(hasattr(builtins, "license")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 393 | |
| 394 | def test_setting_help(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 395 | # 'help' should be set in builtins |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 396 | self.assertTrue(hasattr(builtins, "help")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 397 | |
| 398 | def test_aliasing_mbcs(self): |
| 399 | if sys.platform == "win32": |
| 400 | import locale |
| 401 | if locale.getdefaultlocale()[1].startswith('cp'): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 402 | for value in encodings.aliases.aliases.values(): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 403 | if value == "mbcs": |
| 404 | break |
| 405 | else: |
| 406 | self.fail("did not alias mbcs") |
| 407 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 408 | def test_sitecustomize_executed(self): |
| 409 | # If sitecustomize is available, it should have been imported. |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 410 | if "sitecustomize" not in sys.modules: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 411 | try: |
| 412 | import sitecustomize |
| 413 | except ImportError: |
| 414 | pass |
| 415 | else: |
| 416 | self.fail("sitecustomize not imported automatically") |
| 417 | |
R David Murray | 1bc6ceb | 2013-09-14 13:28:37 -0400 | [diff] [blame] | 418 | @test.support.requires_resource('network') |
| 419 | def test_license_exists_at_url(self): |
| 420 | # This test is a bit fragile since it depends on the format of the |
| 421 | # string displayed by license in the absence of a LICENSE file. |
| 422 | url = license._Printer__data.split()[1] |
| 423 | req = urllib.request.Request(url, method='HEAD') |
| 424 | try: |
Senthil Kumaran | 8ef519b | 2013-09-07 13:59:17 -0700 | [diff] [blame] | 425 | with test.support.transient_internet(url): |
R David Murray | 1bc6ceb | 2013-09-14 13:28:37 -0400 | [diff] [blame] | 426 | with urllib.request.urlopen(req) as data: |
| 427 | code = data.getcode() |
| 428 | except urllib.error.HTTPError as e: |
| 429 | code = e.code |
| 430 | self.assertEqual(code, 200, msg="Can't find " + url) |
Senthil Kumaran | 8ef519b | 2013-09-07 13:59:17 -0700 | [diff] [blame] | 431 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 432 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 433 | if __name__ == "__main__": |
R David Murray | 1bc6ceb | 2013-09-14 13:28:37 -0400 | [diff] [blame] | 434 | unittest.main() |