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 |
Zachary Ware | 36193e7 | 2013-12-11 16:59:44 -0600 | [diff] [blame] | 9 | from test.support import captured_stderr, TESTFN, EnvironmentVarGuard |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 10 | import builtins |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 11 | import os |
| 12 | import sys |
R. David Murray | ab9d8d6 | 2010-12-27 00:03:13 +0000 | [diff] [blame] | 13 | import re |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 14 | import encodings |
R David Murray | 1bc6ceb | 2013-09-14 13:28:37 -0400 | [diff] [blame] | 15 | import urllib.request |
| 16 | import urllib.error |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 17 | import shutil |
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 | |
Zachary Ware | 36193e7 | 2013-12-11 16:59:44 -0600 | [diff] [blame] | 22 | # These tests are not particularly useful if Python was invoked with -S. |
| 23 | # If you add tests that are useful under -S, this skip should be moved |
| 24 | # to the class level. |
| 25 | if sys.flags.no_site: |
| 26 | raise unittest.SkipTest("Python was invoked with -S") |
| 27 | |
| 28 | import site |
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 |
Victor Stinner | 21d0e1b | 2016-03-14 17:47:03 +0100 | [diff] [blame] | 32 | try: |
| 33 | os.makedirs(site.USER_SITE) |
| 34 | site.addsitedir(site.USER_SITE) |
| 35 | except PermissionError as exc: |
| 36 | raise unittest.SkipTest('unable to create user site directory (%r): %s' |
| 37 | % (site.USER_SITE, exc)) |
| 38 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 39 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 40 | class HelperFunctionsTests(unittest.TestCase): |
| 41 | """Tests for helper functions. |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 42 | """ |
| 43 | |
| 44 | def setUp(self): |
| 45 | """Save a copy of sys.path""" |
| 46 | self.sys_path = sys.path[:] |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 47 | self.old_base = site.USER_BASE |
| 48 | self.old_site = site.USER_SITE |
| 49 | self.old_prefixes = site.PREFIXES |
Brett Cannon | 8ac95ee | 2012-04-04 17:31:16 -0400 | [diff] [blame] | 50 | self.original_vars = sysconfig._CONFIG_VARS |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 51 | self.old_vars = copy(sysconfig._CONFIG_VARS) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 52 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 53 | def tearDown(self): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 54 | """Restore sys.path""" |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 55 | sys.path[:] = self.sys_path |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 56 | site.USER_BASE = self.old_base |
| 57 | site.USER_SITE = self.old_site |
| 58 | site.PREFIXES = self.old_prefixes |
Brett Cannon | 8ac95ee | 2012-04-04 17:31:16 -0400 | [diff] [blame] | 59 | sysconfig._CONFIG_VARS = self.original_vars |
| 60 | sysconfig._CONFIG_VARS.clear() |
| 61 | sysconfig._CONFIG_VARS.update(self.old_vars) |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 62 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 63 | def test_makepath(self): |
| 64 | # Test makepath() have an absolute path for its first return value |
| 65 | # and a case-normalized version of the absolute path for its |
| 66 | # second value. |
| 67 | path_parts = ("Beginning", "End") |
| 68 | original_dir = os.path.join(*path_parts) |
| 69 | abs_dir, norm_dir = site.makepath(*path_parts) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 70 | self.assertEqual(os.path.abspath(original_dir), abs_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 71 | if original_dir == os.path.normcase(original_dir): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 72 | self.assertEqual(abs_dir, norm_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 73 | else: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 74 | self.assertEqual(os.path.normcase(abs_dir), norm_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 75 | |
| 76 | def test_init_pathinfo(self): |
| 77 | dir_set = site._init_pathinfo() |
| 78 | for entry in [site.makepath(path)[1] for path in sys.path |
Brett Cannon | 5f0507d | 2016-04-08 15:04:28 -0700 | [diff] [blame] | 79 | if path and os.path.exists(path)]: |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 80 | self.assertIn(entry, dir_set, |
| 81 | "%s from sys.path not found in set returned " |
| 82 | "by _init_pathinfo(): %s" % (entry, dir_set)) |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 83 | |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 84 | def pth_file_tests(self, pth_file): |
| 85 | """Contain common code for testing results of reading a .pth file""" |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 86 | self.assertIn(pth_file.imported, sys.modules, |
| 87 | "%s not in sys.modules" % pth_file.imported) |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 88 | self.assertIn(site.makepath(pth_file.good_dir_path)[0], sys.path) |
| 89 | self.assertFalse(os.path.exists(pth_file.bad_dir_path)) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 90 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 91 | def test_addpackage(self): |
| 92 | # Make sure addpackage() imports if the line starts with 'import', |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 93 | # adds directories to sys.path for any line in the file that is not a |
| 94 | # comment or import that is a valid directory name for where the .pth |
| 95 | # file resides; invalid directories are not added |
| 96 | pth_file = PthFile() |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 97 | pth_file.cleanup(prep=True) # to make sure that nothing is |
| 98 | # pre-existing that shouldn't be |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 99 | try: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 100 | pth_file.create() |
| 101 | site.addpackage(pth_file.base_dir, pth_file.filename, set()) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 102 | self.pth_file_tests(pth_file) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 103 | finally: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 104 | pth_file.cleanup() |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 105 | |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 106 | def make_pth(self, contents, pth_dir='.', pth_name=TESTFN): |
| 107 | # Create a .pth file and return its (abspath, basename). |
| 108 | pth_dir = os.path.abspath(pth_dir) |
| 109 | pth_basename = pth_name + '.pth' |
| 110 | pth_fn = os.path.join(pth_dir, pth_basename) |
| 111 | pth_file = open(pth_fn, 'w', encoding='utf-8') |
| 112 | self.addCleanup(lambda: os.remove(pth_fn)) |
| 113 | pth_file.write(contents) |
| 114 | pth_file.close() |
| 115 | return pth_dir, pth_basename |
| 116 | |
| 117 | def test_addpackage_import_bad_syntax(self): |
| 118 | # Issue 10642 |
| 119 | pth_dir, pth_fn = self.make_pth("import bad)syntax\n") |
| 120 | with captured_stderr() as err_out: |
| 121 | site.addpackage(pth_dir, pth_fn, set()) |
| 122 | self.assertRegex(err_out.getvalue(), "line 1") |
R. David Murray | ab9d8d6 | 2010-12-27 00:03:13 +0000 | [diff] [blame] | 123 | self.assertRegex(err_out.getvalue(), |
| 124 | re.escape(os.path.join(pth_dir, pth_fn))) |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 125 | # XXX: the previous two should be independent checks so that the |
| 126 | # order doesn't matter. The next three could be a single check |
| 127 | # but my regex foo isn't good enough to write it. |
| 128 | self.assertRegex(err_out.getvalue(), 'Traceback') |
| 129 | self.assertRegex(err_out.getvalue(), r'import bad\)syntax') |
| 130 | self.assertRegex(err_out.getvalue(), 'SyntaxError') |
| 131 | |
| 132 | def test_addpackage_import_bad_exec(self): |
| 133 | # Issue 10642 |
| 134 | pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n") |
| 135 | with captured_stderr() as err_out: |
| 136 | site.addpackage(pth_dir, pth_fn, set()) |
| 137 | self.assertRegex(err_out.getvalue(), "line 2") |
R. David Murray | ab9d8d6 | 2010-12-27 00:03:13 +0000 | [diff] [blame] | 138 | self.assertRegex(err_out.getvalue(), |
| 139 | re.escape(os.path.join(pth_dir, pth_fn))) |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 140 | # XXX: ditto previous XXX comment. |
| 141 | self.assertRegex(err_out.getvalue(), 'Traceback') |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 142 | self.assertRegex(err_out.getvalue(), 'ModuleNotFoundError') |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 143 | |
| 144 | def test_addpackage_import_bad_pth_file(self): |
| 145 | # Issue 5258 |
| 146 | pth_dir, pth_fn = self.make_pth("abc\x00def\n") |
| 147 | with captured_stderr() as err_out: |
| 148 | site.addpackage(pth_dir, pth_fn, set()) |
| 149 | self.assertRegex(err_out.getvalue(), "line 1") |
R. David Murray | ab9d8d6 | 2010-12-27 00:03:13 +0000 | [diff] [blame] | 150 | self.assertRegex(err_out.getvalue(), |
| 151 | re.escape(os.path.join(pth_dir, pth_fn))) |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 152 | # XXX: ditto previous XXX comment. |
| 153 | self.assertRegex(err_out.getvalue(), 'Traceback') |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 154 | self.assertRegex(err_out.getvalue(), 'ValueError') |
R. David Murray | b4ca59b | 2010-12-26 19:54:29 +0000 | [diff] [blame] | 155 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 156 | def test_addsitedir(self): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 157 | # Same tests for test_addpackage since addsitedir() essentially just |
| 158 | # calls addpackage() for every .pth file in the directory |
| 159 | pth_file = PthFile() |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 160 | pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing |
| 161 | # that is tested for |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 162 | try: |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 163 | pth_file.create() |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 164 | site.addsitedir(pth_file.base_dir, set()) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 165 | self.pth_file_tests(pth_file) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 166 | finally: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 167 | pth_file.cleanup() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 168 | |
Ned Deily | 316f573 | 2011-10-31 16:16:35 -0700 | [diff] [blame] | 169 | @unittest.skipUnless(site.ENABLE_USER_SITE, "requires access to PEP 370 " |
| 170 | "user-site (site.ENABLE_USER_SITE)") |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 171 | def test_s_option(self): |
| 172 | usersite = site.USER_SITE |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 173 | self.assertIn(usersite, sys.path) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 174 | |
Éric Araujo | 63ebe1c | 2011-01-03 17:51:11 +0000 | [diff] [blame] | 175 | env = os.environ.copy() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 176 | rc = subprocess.call([sys.executable, '-c', |
Éric Araujo | 63ebe1c | 2011-01-03 17:51:11 +0000 | [diff] [blame] | 177 | 'import sys; sys.exit(%r in sys.path)' % usersite], |
| 178 | env=env) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 179 | self.assertEqual(rc, 1) |
| 180 | |
Éric Araujo | 63ebe1c | 2011-01-03 17:51:11 +0000 | [diff] [blame] | 181 | env = os.environ.copy() |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 182 | rc = subprocess.call([sys.executable, '-s', '-c', |
Éric Araujo | 63ebe1c | 2011-01-03 17:51:11 +0000 | [diff] [blame] | 183 | 'import sys; sys.exit(%r in sys.path)' % usersite], |
| 184 | env=env) |
Antoine Pitrou | a1782e1 | 2013-10-23 22:03:22 +0200 | [diff] [blame] | 185 | if usersite == site.getsitepackages()[0]: |
| 186 | self.assertEqual(rc, 1) |
| 187 | else: |
| 188 | self.assertEqual(rc, 0) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 189 | |
| 190 | env = os.environ.copy() |
| 191 | env["PYTHONNOUSERSITE"] = "1" |
| 192 | rc = subprocess.call([sys.executable, '-c', |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 193 | 'import sys; sys.exit(%r in sys.path)' % usersite], |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 194 | env=env) |
Antoine Pitrou | a1782e1 | 2013-10-23 22:03:22 +0200 | [diff] [blame] | 195 | if usersite == site.getsitepackages()[0]: |
| 196 | self.assertEqual(rc, 1) |
| 197 | else: |
| 198 | self.assertEqual(rc, 0) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 199 | |
| 200 | env = os.environ.copy() |
| 201 | env["PYTHONUSERBASE"] = "/tmp" |
| 202 | rc = subprocess.call([sys.executable, '-c', |
| 203 | 'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'], |
| 204 | env=env) |
| 205 | self.assertEqual(rc, 1) |
| 206 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 207 | def test_getuserbase(self): |
| 208 | site.USER_BASE = None |
| 209 | user_base = site.getuserbase() |
| 210 | |
| 211 | # the call sets site.USER_BASE |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 212 | self.assertEqual(site.USER_BASE, user_base) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 213 | |
| 214 | # let's set PYTHONUSERBASE and see if it uses it |
| 215 | site.USER_BASE = None |
Tarek Ziadé | edacea3 | 2010-01-29 11:41:03 +0000 | [diff] [blame] | 216 | import sysconfig |
| 217 | sysconfig._CONFIG_VARS = None |
| 218 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 219 | with EnvironmentVarGuard() as environ: |
| 220 | environ['PYTHONUSERBASE'] = 'xoxo' |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 221 | self.assertTrue(site.getuserbase().startswith('xoxo'), |
| 222 | site.getuserbase()) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 223 | |
| 224 | def test_getusersitepackages(self): |
| 225 | site.USER_SITE = None |
| 226 | site.USER_BASE = None |
| 227 | user_site = site.getusersitepackages() |
| 228 | |
| 229 | # the call sets USER_BASE *and* USER_SITE |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 230 | self.assertEqual(site.USER_SITE, user_site) |
Antoine Pitrou | 9166e6a | 2009-11-01 23:55:40 +0000 | [diff] [blame] | 231 | self.assertTrue(user_site.startswith(site.USER_BASE), user_site) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 232 | |
| 233 | def test_getsitepackages(self): |
| 234 | site.PREFIXES = ['xoxo'] |
| 235 | dirs = site.getsitepackages() |
| 236 | |
Christian Heimes | de0b962 | 2012-11-19 00:59:39 +0100 | [diff] [blame] | 237 | if (sys.platform == "darwin" and |
Ned Deily | d531b29 | 2012-02-06 00:58:18 +0100 | [diff] [blame] | 238 | sysconfig.get_config_var("PYTHONFRAMEWORK")): |
| 239 | # OS X framework builds |
| 240 | site.PREFIXES = ['Python.framework'] |
| 241 | dirs = site.getsitepackages() |
Antoine Pitrou | 9e82b17 | 2014-06-12 19:41:30 -0400 | [diff] [blame] | 242 | self.assertEqual(len(dirs), 2) |
Ned Deily | d531b29 | 2012-02-06 00:58:18 +0100 | [diff] [blame] | 243 | wanted = os.path.join('/Library', |
| 244 | sysconfig.get_config_var("PYTHONFRAMEWORK"), |
Serhiy Storchaka | 885bdc4 | 2016-02-11 13:10:36 +0200 | [diff] [blame] | 245 | '%d.%d' % sys.version_info[:2], |
Ned Deily | d531b29 | 2012-02-06 00:58:18 +0100 | [diff] [blame] | 246 | 'site-packages') |
Antoine Pitrou | 9e82b17 | 2014-06-12 19:41:30 -0400 | [diff] [blame] | 247 | self.assertEqual(dirs[1], wanted) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 248 | elif os.sep == '/': |
Ned Deily | d531b29 | 2012-02-06 00:58:18 +0100 | [diff] [blame] | 249 | # OS X non-framwework builds, Linux, FreeBSD, etc |
Antoine Pitrou | 9e82b17 | 2014-06-12 19:41:30 -0400 | [diff] [blame] | 250 | self.assertEqual(len(dirs), 1) |
Serhiy Storchaka | 885bdc4 | 2016-02-11 13:10:36 +0200 | [diff] [blame] | 251 | wanted = os.path.join('xoxo', 'lib', |
| 252 | 'python%d.%d' % sys.version_info[:2], |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 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 | else: |
Ned Deily | d531b29 | 2012-02-06 00:58:18 +0100 | [diff] [blame] | 256 | # other platforms |
Ezio Melotti | fc8b205 | 2010-08-17 08:35:41 +0000 | [diff] [blame] | 257 | self.assertEqual(len(dirs), 2) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 258 | self.assertEqual(dirs[0], 'xoxo') |
Tarek Ziadé | 8c0e217 | 2009-10-27 21:24:21 +0000 | [diff] [blame] | 259 | wanted = os.path.join('xoxo', 'lib', 'site-packages') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 260 | self.assertEqual(dirs[1], wanted) |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 261 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 262 | class PthFile(object): |
| 263 | """Helper class for handling testing of .pth files""" |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 264 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 265 | def __init__(self, filename_base=TESTFN, imported="time", |
| 266 | good_dirname="__testdir__", bad_dirname="__bad"): |
| 267 | """Initialize instance variables""" |
| 268 | self.filename = filename_base + ".pth" |
| 269 | self.base_dir = os.path.abspath('') |
| 270 | self.file_path = os.path.join(self.base_dir, self.filename) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 271 | self.imported = imported |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 272 | self.good_dirname = good_dirname |
| 273 | self.bad_dirname = bad_dirname |
| 274 | self.good_dir_path = os.path.join(self.base_dir, self.good_dirname) |
| 275 | 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] | 276 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 277 | def create(self): |
| 278 | """Create a .pth file with a comment, blank lines, an ``import |
| 279 | <self.imported>``, a line with self.good_dirname, and a line with |
| 280 | self.bad_dirname. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 281 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 282 | Creation of the directory for self.good_dir_path (based off of |
| 283 | self.good_dirname) is also performed. |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 284 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 285 | 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] | 286 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 287 | """ |
Michael W. Hudson | ff52286 | 2005-05-27 14:58:06 +0000 | [diff] [blame] | 288 | FILE = open(self.file_path, 'w') |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 289 | try: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 290 | print("#import @bad module name", file=FILE) |
| 291 | print("\n", file=FILE) |
| 292 | print("import %s" % self.imported, file=FILE) |
| 293 | print(self.good_dirname, file=FILE) |
| 294 | print(self.bad_dirname, file=FILE) |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 295 | finally: |
| 296 | FILE.close() |
| 297 | os.mkdir(self.good_dir_path) |
| 298 | |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 299 | def cleanup(self, prep=False): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 300 | """Make sure that the .pth file is deleted, self.imported is not in |
| 301 | sys.modules, and that both self.good_dirname and self.bad_dirname are |
| 302 | not existing directories.""" |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 303 | if os.path.exists(self.file_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 304 | os.remove(self.file_path) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 305 | if prep: |
| 306 | self.imported_module = sys.modules.get(self.imported) |
| 307 | if self.imported_module: |
| 308 | del sys.modules[self.imported] |
| 309 | else: |
| 310 | if self.imported_module: |
| 311 | sys.modules[self.imported] = self.imported_module |
| 312 | if os.path.exists(self.good_dir_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 313 | os.rmdir(self.good_dir_path) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 314 | if os.path.exists(self.bad_dir_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 315 | os.rmdir(self.bad_dir_path) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 316 | |
| 317 | class ImportSideEffectTests(unittest.TestCase): |
| 318 | """Test side-effects from importing 'site'.""" |
| 319 | |
| 320 | def setUp(self): |
| 321 | """Make a copy of sys.path""" |
| 322 | self.sys_path = sys.path[:] |
| 323 | |
| 324 | def tearDown(self): |
| 325 | """Restore sys.path""" |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 326 | sys.path[:] = self.sys_path |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 327 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 328 | def test_abs_paths(self): |
| 329 | # Make sure all imported modules have their __file__ and __cached__ |
| 330 | # attributes as absolute paths. Arranging to put the Lib directory on |
| 331 | # PYTHONPATH would cause the os module to have a relative path for |
| 332 | # __file__ if abs_paths() does not get run. sys and builtins (the |
| 333 | # only other modules imported before site.py runs) do not have |
| 334 | # __file__ or __cached__ because they are built-in. |
| 335 | parent = os.path.relpath(os.path.dirname(os.__file__)) |
| 336 | env = os.environ.copy() |
| 337 | env['PYTHONPATH'] = parent |
Victor Stinner | f3bc258 | 2010-04-18 07:59:53 +0000 | [diff] [blame] | 338 | code = ('import os, sys', |
| 339 | # use ASCII to avoid locale issues with non-ASCII directories |
| 340 | 'os_file = os.__file__.encode("ascii", "backslashreplace")', |
| 341 | r'sys.stdout.buffer.write(os_file + b"\n")', |
| 342 | 'os_cached = os.__cached__.encode("ascii", "backslashreplace")', |
| 343 | r'sys.stdout.buffer.write(os_cached + b"\n")') |
| 344 | command = '\n'.join(code) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 345 | # First, prove that with -S (no 'import site'), the paths are |
| 346 | # relative. |
| 347 | proc = subprocess.Popen([sys.executable, '-S', '-c', command], |
| 348 | env=env, |
Victor Stinner | f3bc258 | 2010-04-18 07:59:53 +0000 | [diff] [blame] | 349 | stdout=subprocess.PIPE) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 350 | stdout, stderr = proc.communicate() |
Victor Stinner | f3bc258 | 2010-04-18 07:59:53 +0000 | [diff] [blame] | 351 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 352 | self.assertEqual(proc.returncode, 0) |
Victor Stinner | f3bc258 | 2010-04-18 07:59:53 +0000 | [diff] [blame] | 353 | os__file__, os__cached__ = stdout.splitlines()[:2] |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 354 | self.assertFalse(os.path.isabs(os__file__)) |
| 355 | self.assertFalse(os.path.isabs(os__cached__)) |
| 356 | # Now, with 'import site', it works. |
| 357 | proc = subprocess.Popen([sys.executable, '-c', command], |
| 358 | env=env, |
Victor Stinner | f3bc258 | 2010-04-18 07:59:53 +0000 | [diff] [blame] | 359 | stdout=subprocess.PIPE) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 360 | stdout, stderr = proc.communicate() |
| 361 | self.assertEqual(proc.returncode, 0) |
Victor Stinner | f3bc258 | 2010-04-18 07:59:53 +0000 | [diff] [blame] | 362 | os__file__, os__cached__ = stdout.splitlines()[:2] |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 363 | self.assertTrue(os.path.isabs(os__file__), |
Eric Snow | 00607e9 | 2015-05-04 11:48:39 -0600 | [diff] [blame] | 364 | "expected absolute path, got {}" |
| 365 | .format(os__file__.decode('ascii'))) |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 366 | self.assertTrue(os.path.isabs(os__cached__), |
Eric Snow | 00607e9 | 2015-05-04 11:48:39 -0600 | [diff] [blame] | 367 | "expected absolute path, got {}" |
| 368 | .format(os__cached__.decode('ascii'))) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 369 | |
| 370 | def test_no_duplicate_paths(self): |
| 371 | # No duplicate paths should exist in sys.path |
| 372 | # Handled by removeduppaths() |
| 373 | site.removeduppaths() |
| 374 | seen_paths = set() |
| 375 | for path in sys.path: |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 376 | self.assertNotIn(path, seen_paths) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 377 | seen_paths.add(path) |
| 378 | |
Zachary Ware | 9fe6d86 | 2013-12-08 00:20:35 -0600 | [diff] [blame] | 379 | @unittest.skip('test not implemented') |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 380 | def test_add_build_dir(self): |
| 381 | # Test that the build directory's Modules directory is used when it |
| 382 | # should be. |
| 383 | # XXX: implement |
| 384 | pass |
| 385 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 386 | def test_setting_quit(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 387 | # 'quit' and 'exit' should be injected into builtins |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 388 | self.assertTrue(hasattr(builtins, "quit")) |
| 389 | self.assertTrue(hasattr(builtins, "exit")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 390 | |
| 391 | def test_setting_copyright(self): |
Senthil Kumaran | 8ef519b | 2013-09-07 13:59:17 -0700 | [diff] [blame] | 392 | # 'copyright', 'credits', and 'license' should be in builtins |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 393 | self.assertTrue(hasattr(builtins, "copyright")) |
| 394 | self.assertTrue(hasattr(builtins, "credits")) |
Senthil Kumaran | 8ef519b | 2013-09-07 13:59:17 -0700 | [diff] [blame] | 395 | self.assertTrue(hasattr(builtins, "license")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 396 | |
| 397 | def test_setting_help(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 398 | # 'help' should be set in builtins |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 399 | self.assertTrue(hasattr(builtins, "help")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 400 | |
| 401 | def test_aliasing_mbcs(self): |
| 402 | if sys.platform == "win32": |
| 403 | import locale |
| 404 | if locale.getdefaultlocale()[1].startswith('cp'): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 405 | for value in encodings.aliases.aliases.values(): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 406 | if value == "mbcs": |
| 407 | break |
| 408 | else: |
| 409 | self.fail("did not alias mbcs") |
| 410 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 411 | def test_sitecustomize_executed(self): |
| 412 | # If sitecustomize is available, it should have been imported. |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 413 | if "sitecustomize" not in sys.modules: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 414 | try: |
| 415 | import sitecustomize |
| 416 | except ImportError: |
| 417 | pass |
| 418 | else: |
| 419 | self.fail("sitecustomize not imported automatically") |
| 420 | |
R David Murray | 1bc6ceb | 2013-09-14 13:28:37 -0400 | [diff] [blame] | 421 | @test.support.requires_resource('network') |
Benjamin Peterson | 337578b | 2015-02-01 20:16:59 -0500 | [diff] [blame] | 422 | @test.support.system_must_validate_cert |
Georg Brandl | 78abc9d | 2013-10-27 09:41:57 +0100 | [diff] [blame] | 423 | @unittest.skipUnless(sys.version_info[3] == 'final', |
| 424 | 'only for released versions') |
Ned Deily | 5a507f0 | 2014-03-26 23:31:39 -0700 | [diff] [blame] | 425 | @unittest.skipUnless(hasattr(urllib.request, "HTTPSHandler"), |
| 426 | 'need SSL support to download license') |
R David Murray | 1bc6ceb | 2013-09-14 13:28:37 -0400 | [diff] [blame] | 427 | def test_license_exists_at_url(self): |
Ned Deily | 944d597 | 2014-03-26 23:43:26 -0700 | [diff] [blame] | 428 | # This test is a bit fragile since it depends on the format of the |
R David Murray | 1bc6ceb | 2013-09-14 13:28:37 -0400 | [diff] [blame] | 429 | # string displayed by license in the absence of a LICENSE file. |
| 430 | url = license._Printer__data.split()[1] |
| 431 | req = urllib.request.Request(url, method='HEAD') |
| 432 | try: |
Senthil Kumaran | 8ef519b | 2013-09-07 13:59:17 -0700 | [diff] [blame] | 433 | with test.support.transient_internet(url): |
R David Murray | 1bc6ceb | 2013-09-14 13:28:37 -0400 | [diff] [blame] | 434 | with urllib.request.urlopen(req) as data: |
| 435 | code = data.getcode() |
| 436 | except urllib.error.HTTPError as e: |
| 437 | code = e.code |
| 438 | self.assertEqual(code, 200, msg="Can't find " + url) |
Senthil Kumaran | 8ef519b | 2013-09-07 13:59:17 -0700 | [diff] [blame] | 439 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 440 | |
Christian Heimes | 8c9cd5a | 2013-10-12 00:24:55 +0200 | [diff] [blame] | 441 | class StartupImportTests(unittest.TestCase): |
| 442 | |
| 443 | def test_startup_imports(self): |
| 444 | # This tests checks which modules are loaded by Python when it |
| 445 | # initially starts upon startup. |
Christian Heimes | 179a3db | 2013-10-12 12:32:21 +0200 | [diff] [blame] | 446 | popen = subprocess.Popen([sys.executable, '-I', '-v', '-c', |
| 447 | 'import sys; print(set(sys.modules))'], |
| 448 | stdout=subprocess.PIPE, |
Steve Dower | 313523c | 2016-09-17 12:22:41 -0700 | [diff] [blame] | 449 | stderr=subprocess.PIPE, |
| 450 | encoding='utf-8') |
Christian Heimes | 179a3db | 2013-10-12 12:32:21 +0200 | [diff] [blame] | 451 | stdout, stderr = popen.communicate() |
Christian Heimes | 179a3db | 2013-10-12 12:32:21 +0200 | [diff] [blame] | 452 | modules = eval(stdout) |
| 453 | |
Christian Heimes | 8c9cd5a | 2013-10-12 00:24:55 +0200 | [diff] [blame] | 454 | self.assertIn('site', modules) |
| 455 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 456 | # http://bugs.python.org/issue19205 |
Christian Heimes | 8c9cd5a | 2013-10-12 00:24:55 +0200 | [diff] [blame] | 457 | re_mods = {'re', '_sre', 'sre_compile', 'sre_constants', 'sre_parse'} |
Christian Heimes | f403f50 | 2013-10-12 15:08:42 +0200 | [diff] [blame] | 458 | # _osx_support uses the re module in many placs |
| 459 | if sys.platform != 'darwin': |
| 460 | self.assertFalse(modules.intersection(re_mods), stderr) |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 461 | # http://bugs.python.org/issue9548 |
Christian Heimes | 179a3db | 2013-10-12 12:32:21 +0200 | [diff] [blame] | 462 | self.assertNotIn('locale', modules, stderr) |
Christian Heimes | 86823a5 | 2013-10-17 13:40:00 +0200 | [diff] [blame] | 463 | if sys.platform != 'darwin': |
| 464 | # http://bugs.python.org/issue19209 |
| 465 | self.assertNotIn('copyreg', modules, stderr) |
Christian Heimes | f1dc3ee | 2013-10-13 02:04:20 +0200 | [diff] [blame] | 466 | # http://bugs.python.org/issue19218> |
| 467 | collection_mods = {'_collections', 'collections', 'functools', |
| 468 | 'heapq', 'itertools', 'keyword', 'operator', |
doko@ubuntu.com | 9574355 | 2014-04-15 20:37:54 +0200 | [diff] [blame] | 469 | 'reprlib', 'types', 'weakref' |
| 470 | }.difference(sys.builtin_module_names) |
Ned Deily | 8a2150a | 2016-09-12 00:26:20 -0400 | [diff] [blame] | 471 | # http://bugs.python.org/issue28095 |
| 472 | if sys.platform != 'darwin': |
| 473 | self.assertFalse(modules.intersection(collection_mods), stderr) |
Christian Heimes | 1a5fb4e | 2013-10-12 01:00:51 +0200 | [diff] [blame] | 474 | |
Steve Dower | 6dd8eca | 2016-09-17 14:35:32 -0700 | [diff] [blame] | 475 | def test_startup_interactivehook(self): |
| 476 | r = subprocess.Popen([sys.executable, '-c', |
| 477 | 'import sys; sys.exit(hasattr(sys, "__interactivehook__"))']).wait() |
| 478 | self.assertTrue(r, "'__interactivehook__' not added by site") |
| 479 | |
| 480 | def test_startup_interactivehook_isolated(self): |
| 481 | # issue28192 readline is not automatically enabled in isolated mode |
| 482 | r = subprocess.Popen([sys.executable, '-I', '-c', |
| 483 | 'import sys; sys.exit(hasattr(sys, "__interactivehook__"))']).wait() |
| 484 | self.assertFalse(r, "'__interactivehook__' added in isolated mode") |
| 485 | |
| 486 | def test_startup_interactivehook_isolated_explicit(self): |
| 487 | # issue28192 readline can be explicitly enabled in isolated mode |
| 488 | r = subprocess.Popen([sys.executable, '-I', '-c', |
| 489 | 'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))']).wait() |
| 490 | self.assertTrue(r, "'__interactivehook__' not added by enablerlcompleter()") |
| 491 | |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 492 | @classmethod |
| 493 | def _create_underpth_exe(self, lines): |
| 494 | exe_file = os.path.join(os.getenv('TEMP'), os.path.split(sys.executable)[1]) |
| 495 | shutil.copy(sys.executable, exe_file) |
| 496 | |
| 497 | _pth_file = os.path.splitext(exe_file)[0] + '._pth' |
| 498 | try: |
| 499 | with open(_pth_file, 'w') as f: |
| 500 | for line in lines: |
| 501 | print(line, file=f) |
| 502 | return exe_file |
| 503 | except: |
| 504 | os.unlink(_pth_file) |
| 505 | os.unlink(exe_file) |
| 506 | raise |
| 507 | |
| 508 | @classmethod |
| 509 | def _cleanup_underpth_exe(self, exe_file): |
| 510 | _pth_file = os.path.splitext(exe_file)[0] + '._pth' |
| 511 | os.unlink(_pth_file) |
| 512 | os.unlink(exe_file) |
| 513 | |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 514 | @unittest.skipUnless(sys.platform == 'win32', "only supported on Windows") |
| 515 | def test_underpth_nosite_file(self): |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 516 | libpath = os.path.dirname(os.path.dirname(encodings.__file__)) |
| 517 | exe_prefix = os.path.dirname(sys.executable) |
| 518 | exe_file = self._create_underpth_exe([ |
| 519 | 'fake-path-name', |
| 520 | *[libpath for _ in range(200)], |
| 521 | '# comment', |
| 522 | 'import site' |
| 523 | ]) |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 524 | |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 525 | try: |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 526 | env = os.environ.copy() |
| 527 | env['PYTHONPATH'] = 'from-env' |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 528 | env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH')) |
| 529 | rc = subprocess.call([exe_file, '-c', |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 530 | 'import sys; sys.exit(sys.flags.no_site and ' |
| 531 | 'len(sys.path) > 200 and ' |
| 532 | '%r in sys.path and %r in sys.path and %r not in sys.path)' % ( |
| 533 | os.path.join(sys.prefix, 'fake-path-name'), |
| 534 | libpath, |
| 535 | os.path.join(sys.prefix, 'from-env'), |
| 536 | )], env=env) |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 537 | finally: |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 538 | self._cleanup_underpth_exe(exe_file) |
| 539 | self.assertEqual(rc, 0) |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 540 | |
| 541 | @unittest.skipUnless(sys.platform == 'win32', "only supported on Windows") |
| 542 | def test_underpth_file(self): |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 543 | libpath = os.path.dirname(os.path.dirname(encodings.__file__)) |
| 544 | exe_prefix = os.path.dirname(sys.executable) |
| 545 | exe_file = self._create_underpth_exe([ |
| 546 | 'fake-path-name', |
| 547 | *[libpath for _ in range(200)], |
| 548 | '# comment', |
| 549 | 'import site' |
| 550 | ]) |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 551 | try: |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 552 | env = os.environ.copy() |
| 553 | env['PYTHONPATH'] = 'from-env' |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 554 | env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH')) |
| 555 | rc = subprocess.call([exe_file, '-c', |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 556 | 'import sys; sys.exit(not sys.flags.no_site and ' |
| 557 | '%r in sys.path and %r in sys.path and %r not in sys.path)' % ( |
| 558 | os.path.join(sys.prefix, 'fake-path-name'), |
| 559 | libpath, |
| 560 | os.path.join(sys.prefix, 'from-env'), |
| 561 | )], env=env) |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 562 | finally: |
Steve Dower | 1da055e | 2016-10-29 08:50:31 -0700 | [diff] [blame] | 563 | self._cleanup_underpth_exe(exe_file) |
| 564 | self.assertEqual(rc, 0) |
Steve Dower | c6dd415 | 2016-10-27 14:28:07 -0700 | [diff] [blame] | 565 | |
Steve Dower | 6dd8eca | 2016-09-17 14:35:32 -0700 | [diff] [blame] | 566 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 567 | if __name__ == "__main__": |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 568 | unittest.main() |