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 |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 8 | from test.support import run_unittest, TESTFN, EnvironmentVarGuard |
R. David Murray | 6cb252f | 2010-12-26 22:24:54 +0000 | [diff] [blame] | 9 | from test.support import captured_output |
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 | a640523 | 2010-12-27 00:07:32 +0000 | [diff] [blame] | 13 | import re |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 14 | import encodings |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 15 | import subprocess |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 16 | # 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. |
| 19 | if "site" in sys.modules: |
| 20 | import site |
| 21 | else: |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 22 | raise unittest.SkipTest("importation of site.py suppressed") |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 23 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 24 | if 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 Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 29 | class HelperFunctionsTests(unittest.TestCase): |
| 30 | """Tests for helper functions. |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 31 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 32 | The setting of the encoding (set using sys.setdefaultencoding) used by |
| 33 | the Unicode implementation is not tested. |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 34 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 35 | """ |
| 36 | |
| 37 | def setUp(self): |
| 38 | """Save a copy of sys.path""" |
| 39 | self.sys_path = sys.path[:] |
| 40 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 41 | def tearDown(self): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 42 | """Restore sys.path""" |
| 43 | sys.path = self.sys_path |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 44 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 45 | 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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 52 | self.assertEqual(os.path.abspath(original_dir), abs_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 53 | if original_dir == os.path.normcase(original_dir): |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 54 | self.assertEqual(abs_dir, norm_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 55 | else: |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 56 | self.assertEqual(os.path.normcase(abs_dir), norm_dir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 57 | |
| 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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 62 | self.assertTrue(entry in dir_set, |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 63 | "%s from sys.path not found in set returned " |
| 64 | "by _init_pathinfo(): %s" % (entry, dir_set)) |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 65 | |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 66 | def pth_file_tests(self, pth_file): |
| 67 | """Contain common code for testing results of reading a .pth file""" |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 68 | self.assertTrue(pth_file.imported in sys.modules, |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 69 | "%s not in sys.path" % pth_file.imported) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 70 | 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 Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 72 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 73 | def test_addpackage(self): |
| 74 | # Make sure addpackage() imports if the line starts with 'import', |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 75 | # 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 Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 79 | pth_file.cleanup(prep=True) # to make sure that nothing is |
| 80 | # pre-existing that shouldn't be |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 81 | try: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 82 | pth_file.create() |
| 83 | site.addpackage(pth_file.base_dir, pth_file.filename, set()) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 84 | self.pth_file_tests(pth_file) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 85 | finally: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 86 | pth_file.cleanup() |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 87 | |
R. David Murray | 6cb252f | 2010-12-26 22:24:54 +0000 | [diff] [blame] | 88 | 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 Murray | a640523 | 2010-12-27 00:07:32 +0000 | [diff] [blame] | 105 | self.assertRegexpMatches(err_out.getvalue(), |
| 106 | re.escape(os.path.join(pth_dir, pth_fn))) |
R. David Murray | 6cb252f | 2010-12-26 22:24:54 +0000 | [diff] [blame] | 107 | # 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 Murray | a640523 | 2010-12-27 00:07:32 +0000 | [diff] [blame] | 120 | self.assertRegexpMatches(err_out.getvalue(), |
| 121 | re.escape(os.path.join(pth_dir, pth_fn))) |
R. David Murray | 6cb252f | 2010-12-26 22:24:54 +0000 | [diff] [blame] | 122 | # XXX: ditto previous XXX comment. |
| 123 | self.assertRegexpMatches(err_out.getvalue(), 'Traceback') |
| 124 | self.assertRegexpMatches(err_out.getvalue(), 'ImportError') |
| 125 | |
R. David Murray | 17638d9 | 2010-12-27 04:36:07 +0000 | [diff] [blame^] | 126 | @unittest.skipIf(sys.platform == "win32", "Windows does not raise an " |
| 127 | "error for file paths containing null characters") |
R. David Murray | 6cb252f | 2010-12-26 22:24:54 +0000 | [diff] [blame] | 128 | def test_addpackage_import_bad_pth_file(self): |
| 129 | # Issue 5258 |
| 130 | pth_dir, pth_fn = self.make_pth("abc\x00def\n") |
| 131 | with captured_output("stderr") as err_out: |
| 132 | site.addpackage(pth_dir, pth_fn, set()) |
| 133 | self.assertRegexpMatches(err_out.getvalue(), "line 1") |
R. David Murray | a640523 | 2010-12-27 00:07:32 +0000 | [diff] [blame] | 134 | self.assertRegexpMatches(err_out.getvalue(), |
| 135 | re.escape(os.path.join(pth_dir, pth_fn))) |
R. David Murray | 6cb252f | 2010-12-26 22:24:54 +0000 | [diff] [blame] | 136 | # XXX: ditto previous XXX comment. |
| 137 | self.assertRegexpMatches(err_out.getvalue(), 'Traceback') |
| 138 | self.assertRegexpMatches(err_out.getvalue(), 'TypeError') |
| 139 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 140 | def test_addsitedir(self): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 141 | # Same tests for test_addpackage since addsitedir() essentially just |
| 142 | # calls addpackage() for every .pth file in the directory |
| 143 | pth_file = PthFile() |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 144 | pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing |
| 145 | # that is tested for |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 146 | try: |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 147 | pth_file.create() |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 148 | site.addsitedir(pth_file.base_dir, set()) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 149 | self.pth_file_tests(pth_file) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 150 | finally: |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 151 | pth_file.cleanup() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 152 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 153 | def test_s_option(self): |
| 154 | usersite = site.USER_SITE |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 155 | self.assertTrue(usersite in sys.path) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 156 | |
| 157 | rc = subprocess.call([sys.executable, '-c', |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 158 | 'import sys; sys.exit(%r in sys.path)' % usersite]) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 159 | self.assertEqual(rc, 1) |
| 160 | |
| 161 | rc = subprocess.call([sys.executable, '-s', '-c', |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 162 | 'import sys; sys.exit(%r in sys.path)' % usersite]) |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 163 | self.assertEqual(rc, 0) |
| 164 | |
| 165 | env = os.environ.copy() |
| 166 | env["PYTHONNOUSERSITE"] = "1" |
| 167 | rc = subprocess.call([sys.executable, '-c', |
Benjamin Peterson | fea6a94 | 2008-07-02 16:11:42 +0000 | [diff] [blame] | 168 | 'import sys; sys.exit(%r in sys.path)' % usersite], |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 169 | env=env) |
| 170 | self.assertEqual(rc, 0) |
| 171 | |
| 172 | env = os.environ.copy() |
| 173 | env["PYTHONUSERBASE"] = "/tmp" |
| 174 | rc = subprocess.call([sys.executable, '-c', |
| 175 | 'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'], |
| 176 | env=env) |
| 177 | self.assertEqual(rc, 1) |
| 178 | |
| 179 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 180 | class PthFile(object): |
| 181 | """Helper class for handling testing of .pth files""" |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 182 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 183 | def __init__(self, filename_base=TESTFN, imported="time", |
| 184 | good_dirname="__testdir__", bad_dirname="__bad"): |
| 185 | """Initialize instance variables""" |
| 186 | self.filename = filename_base + ".pth" |
| 187 | self.base_dir = os.path.abspath('') |
| 188 | self.file_path = os.path.join(self.base_dir, self.filename) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 189 | self.imported = imported |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 190 | self.good_dirname = good_dirname |
| 191 | self.bad_dirname = bad_dirname |
| 192 | self.good_dir_path = os.path.join(self.base_dir, self.good_dirname) |
| 193 | self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 194 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 195 | def create(self): |
| 196 | """Create a .pth file with a comment, blank lines, an ``import |
| 197 | <self.imported>``, a line with self.good_dirname, and a line with |
| 198 | self.bad_dirname. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 199 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 200 | Creation of the directory for self.good_dir_path (based off of |
| 201 | self.good_dirname) is also performed. |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 202 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 203 | 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] | 204 | |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 205 | """ |
Michael W. Hudson | ff52286 | 2005-05-27 14:58:06 +0000 | [diff] [blame] | 206 | FILE = open(self.file_path, 'w') |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 207 | try: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 208 | print("#import @bad module name", file=FILE) |
| 209 | print("\n", file=FILE) |
| 210 | print("import %s" % self.imported, file=FILE) |
| 211 | print(self.good_dirname, file=FILE) |
| 212 | print(self.bad_dirname, file=FILE) |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 213 | finally: |
| 214 | FILE.close() |
| 215 | os.mkdir(self.good_dir_path) |
| 216 | |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 217 | def cleanup(self, prep=False): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 218 | """Make sure that the .pth file is deleted, self.imported is not in |
| 219 | sys.modules, and that both self.good_dirname and self.bad_dirname are |
| 220 | not existing directories.""" |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 221 | if os.path.exists(self.file_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 222 | os.remove(self.file_path) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 223 | if prep: |
| 224 | self.imported_module = sys.modules.get(self.imported) |
| 225 | if self.imported_module: |
| 226 | del sys.modules[self.imported] |
| 227 | else: |
| 228 | if self.imported_module: |
| 229 | sys.modules[self.imported] = self.imported_module |
| 230 | if os.path.exists(self.good_dir_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 231 | os.rmdir(self.good_dir_path) |
Brett Cannon | ee86a66 | 2004-07-13 07:12:25 +0000 | [diff] [blame] | 232 | if os.path.exists(self.bad_dir_path): |
Brett Cannon | 64a8470 | 2004-07-10 02:10:45 +0000 | [diff] [blame] | 233 | os.rmdir(self.bad_dir_path) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 234 | |
| 235 | class ImportSideEffectTests(unittest.TestCase): |
| 236 | """Test side-effects from importing 'site'.""" |
| 237 | |
| 238 | def setUp(self): |
| 239 | """Make a copy of sys.path""" |
| 240 | self.sys_path = sys.path[:] |
| 241 | |
| 242 | def tearDown(self): |
| 243 | """Restore sys.path""" |
| 244 | sys.path = self.sys_path |
| 245 | |
| 246 | def test_abs__file__(self): |
| 247 | # Make sure all imported modules have their __file__ attribute |
| 248 | # as an absolute path. |
| 249 | # Handled by abs__file__() |
| 250 | site.abs__file__() |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 251 | for module in (sys, os, builtins): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 252 | try: |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 253 | self.assertTrue(os.path.isabs(module.__file__), repr(module)) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 254 | except AttributeError: |
| 255 | continue |
Raymond Hettinger | ebd9522 | 2004-06-27 03:02:18 +0000 | [diff] [blame] | 256 | # We could try everything in sys.modules; however, when regrtest.py |
| 257 | # runs something like test_frozen before test_site, then we will |
| 258 | # be testing things loaded *after* test_site did path normalization |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 259 | |
| 260 | def test_no_duplicate_paths(self): |
| 261 | # No duplicate paths should exist in sys.path |
| 262 | # Handled by removeduppaths() |
| 263 | site.removeduppaths() |
| 264 | seen_paths = set() |
| 265 | for path in sys.path: |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 266 | self.assertTrue(path not in seen_paths) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 267 | seen_paths.add(path) |
| 268 | |
| 269 | def test_add_build_dir(self): |
| 270 | # Test that the build directory's Modules directory is used when it |
| 271 | # should be. |
| 272 | # XXX: implement |
| 273 | pass |
| 274 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 275 | def test_setting_quit(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 276 | # 'quit' and 'exit' should be injected into builtins |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 277 | self.assertTrue(hasattr(builtins, "quit")) |
| 278 | self.assertTrue(hasattr(builtins, "exit")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 279 | |
| 280 | def test_setting_copyright(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 281 | # 'copyright' and 'credits' should be in builtins |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 282 | self.assertTrue(hasattr(builtins, "copyright")) |
| 283 | self.assertTrue(hasattr(builtins, "credits")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 284 | |
| 285 | def test_setting_help(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 286 | # 'help' should be set in builtins |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 287 | self.assertTrue(hasattr(builtins, "help")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 288 | |
| 289 | def test_aliasing_mbcs(self): |
| 290 | if sys.platform == "win32": |
| 291 | import locale |
| 292 | if locale.getdefaultlocale()[1].startswith('cp'): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 293 | for value in encodings.aliases.aliases.values(): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 294 | if value == "mbcs": |
| 295 | break |
| 296 | else: |
| 297 | self.fail("did not alias mbcs") |
| 298 | |
| 299 | def test_setdefaultencoding_removed(self): |
| 300 | # Make sure sys.setdefaultencoding is gone |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 301 | self.assertTrue(not hasattr(sys, "setdefaultencoding")) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 302 | |
| 303 | def test_sitecustomize_executed(self): |
| 304 | # If sitecustomize is available, it should have been imported. |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 305 | if "sitecustomize" not in sys.modules: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 306 | try: |
| 307 | import sitecustomize |
| 308 | except ImportError: |
| 309 | pass |
| 310 | else: |
| 311 | self.fail("sitecustomize not imported automatically") |
| 312 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 313 | def test_main(): |
| 314 | run_unittest(HelperFunctionsTests, ImportSideEffectTests) |
| 315 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 316 | if __name__ == "__main__": |
| 317 | test_main() |