Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 1 | import imp |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 2 | import os |
| 3 | import os.path |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 4 | import shutil |
Brett Cannon | 8a9583e | 2008-09-04 05:04:25 +0000 | [diff] [blame] | 5 | import sys |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Alexander Belopolsky | e8f5832 | 2010-10-15 16:28:20 +0000 | [diff] [blame] | 8 | import importlib |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 9 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 10 | class LockTests(unittest.TestCase): |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 11 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 12 | """Very basic test of import lock functions.""" |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 14 | def verify_lock_state(self, expected): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 15 | self.assertEqual(imp.lock_held(), expected, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 16 | "expected imp.lock_held() to be %r" % expected) |
| 17 | def testLock(self): |
| 18 | LOOPS = 50 |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 19 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 20 | # The import lock may already be held, e.g. if the test suite is run |
| 21 | # via "import test.autotest". |
| 22 | lock_held_at_start = imp.lock_held() |
| 23 | self.verify_lock_state(lock_held_at_start) |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 24 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 25 | for i in range(LOOPS): |
| 26 | imp.acquire_lock() |
| 27 | self.verify_lock_state(True) |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 28 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 29 | for i in range(LOOPS): |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 30 | imp.release_lock() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 31 | |
| 32 | # The original state should be restored now. |
| 33 | self.verify_lock_state(lock_held_at_start) |
| 34 | |
| 35 | if not lock_held_at_start: |
| 36 | try: |
| 37 | imp.release_lock() |
| 38 | except RuntimeError: |
| 39 | pass |
| 40 | else: |
| 41 | self.fail("release_lock() without lock should raise " |
| 42 | "RuntimeError") |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 44 | class ImportTests(unittest.TestCase): |
Alexander Belopolsky | e8f5832 | 2010-10-15 16:28:20 +0000 | [diff] [blame] | 45 | def setUp(self): |
| 46 | mod = importlib.import_module('test.encoded_modules') |
| 47 | self.test_strings = mod.test_strings |
| 48 | self.test_path = mod.__path__ |
| 49 | |
| 50 | def test_import_encoded_module(self): |
| 51 | for modname, encoding, teststr in self.test_strings: |
| 52 | mod = importlib.import_module('test.encoded_modules.' |
| 53 | 'module_' + modname) |
| 54 | self.assertEqual(teststr, mod.test) |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 55 | |
| 56 | def test_find_module_encoding(self): |
Alexander Belopolsky | e8f5832 | 2010-10-15 16:28:20 +0000 | [diff] [blame] | 57 | for mod, encoding, _ in self.test_strings: |
Brett Cannon | 749afa9 | 2010-10-29 23:47:23 +0000 | [diff] [blame] | 58 | with imp.find_module('module_' + mod, self.test_path)[0] as fd: |
| 59 | self.assertEqual(fd.encoding, encoding) |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 60 | |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 61 | path = [os.path.dirname(__file__)] |
Brett Cannon | dd9a569 | 2012-04-20 12:59:59 -0400 | [diff] [blame^] | 62 | with self.assertRaises(SyntaxError): |
| 63 | imp.find_module('badsyntax_pep3120', path) |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 64 | |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 65 | def test_issue1267(self): |
Alexander Belopolsky | e8f5832 | 2010-10-15 16:28:20 +0000 | [diff] [blame] | 66 | for mod, encoding, _ in self.test_strings: |
| 67 | fp, filename, info = imp.find_module('module_' + mod, |
| 68 | self.test_path) |
Brett Cannon | 749afa9 | 2010-10-29 23:47:23 +0000 | [diff] [blame] | 69 | with fp: |
| 70 | self.assertNotEqual(fp, None) |
| 71 | self.assertEqual(fp.encoding, encoding) |
| 72 | self.assertEqual(fp.tell(), 0) |
| 73 | self.assertEqual(fp.readline(), '# test %s encoding\n' |
| 74 | % encoding) |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 75 | |
| 76 | fp, filename, info = imp.find_module("tokenize") |
Brett Cannon | 749afa9 | 2010-10-29 23:47:23 +0000 | [diff] [blame] | 77 | with fp: |
| 78 | self.assertNotEqual(fp, None) |
| 79 | self.assertEqual(fp.encoding, "utf-8") |
| 80 | self.assertEqual(fp.tell(), 0) |
| 81 | self.assertEqual(fp.readline(), |
| 82 | '"""Tokenization help for Python programs.\n') |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 83 | |
Brett Cannon | 8a9583e | 2008-09-04 05:04:25 +0000 | [diff] [blame] | 84 | def test_issue3594(self): |
| 85 | temp_mod_name = 'test_imp_helper' |
| 86 | sys.path.insert(0, '.') |
| 87 | try: |
| 88 | with open(temp_mod_name + '.py', 'w') as file: |
| 89 | file.write("# coding: cp1252\nu = 'test.test_imp'\n") |
| 90 | file, filename, info = imp.find_module(temp_mod_name) |
| 91 | file.close() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 92 | self.assertEqual(file.encoding, 'cp1252') |
Brett Cannon | 8a9583e | 2008-09-04 05:04:25 +0000 | [diff] [blame] | 93 | finally: |
| 94 | del sys.path[0] |
| 95 | support.unlink(temp_mod_name + '.py') |
| 96 | support.unlink(temp_mod_name + '.pyc') |
| 97 | support.unlink(temp_mod_name + '.pyo') |
| 98 | |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 99 | def test_issue5604(self): |
| 100 | # Test cannot cover imp.load_compiled function. |
| 101 | # Martin von Loewis note what shared library cannot have non-ascii |
| 102 | # character because init_xxx function cannot be compiled |
| 103 | # and issue never happens for dynamic modules. |
| 104 | # But sources modified to follow generic way for processing pathes. |
| 105 | |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 106 | # the return encoding could be uppercase or None |
| 107 | fs_encoding = sys.getfilesystemencoding() |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 108 | |
| 109 | # covers utf-8 and Windows ANSI code pages |
| 110 | # one non-space symbol from every page |
| 111 | # (http://en.wikipedia.org/wiki/Code_page) |
| 112 | known_locales = { |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 113 | 'utf-8' : b'\xc3\xa4', |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 114 | 'cp1250' : b'\x8C', |
| 115 | 'cp1251' : b'\xc0', |
| 116 | 'cp1252' : b'\xc0', |
| 117 | 'cp1253' : b'\xc1', |
| 118 | 'cp1254' : b'\xc0', |
| 119 | 'cp1255' : b'\xe0', |
| 120 | 'cp1256' : b'\xe0', |
| 121 | 'cp1257' : b'\xc0', |
| 122 | 'cp1258' : b'\xc0', |
| 123 | } |
| 124 | |
Florent Xicluna | 21164ce | 2010-03-20 20:30:53 +0000 | [diff] [blame] | 125 | if sys.platform == 'darwin': |
| 126 | self.assertEqual(fs_encoding, 'utf-8') |
| 127 | # Mac OS X uses the Normal Form D decomposition |
| 128 | # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html |
| 129 | special_char = b'a\xcc\x88' |
| 130 | else: |
| 131 | special_char = known_locales.get(fs_encoding) |
| 132 | |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 133 | if not special_char: |
Ezio Melotti | 76e0d1a | 2010-03-05 15:08:19 +0000 | [diff] [blame] | 134 | self.skipTest("can't run this test with %s as filesystem encoding" |
| 135 | % fs_encoding) |
| 136 | decoded_char = special_char.decode(fs_encoding) |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 137 | temp_mod_name = 'test_imp_helper_' + decoded_char |
| 138 | test_package_name = 'test_imp_helper_package_' + decoded_char |
| 139 | init_file_name = os.path.join(test_package_name, '__init__.py') |
| 140 | try: |
Ezio Melotti | 41a6b04 | 2010-03-06 01:50:25 +0000 | [diff] [blame] | 141 | # if the curdir is not in sys.path the test fails when run with |
| 142 | # ./python ./Lib/test/regrtest.py test_imp |
| 143 | sys.path.insert(0, os.curdir) |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 144 | with open(temp_mod_name + '.py', 'w') as file: |
| 145 | file.write('a = 1\n') |
| 146 | file, filename, info = imp.find_module(temp_mod_name) |
Brett Cannon | 749afa9 | 2010-10-29 23:47:23 +0000 | [diff] [blame] | 147 | with file: |
| 148 | self.assertIsNotNone(file) |
| 149 | self.assertTrue(filename[:-3].endswith(temp_mod_name)) |
| 150 | self.assertEqual(info[0], '.py') |
| 151 | self.assertEqual(info[1], 'U') |
| 152 | self.assertEqual(info[2], imp.PY_SOURCE) |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 153 | |
Brett Cannon | 749afa9 | 2010-10-29 23:47:23 +0000 | [diff] [blame] | 154 | mod = imp.load_module(temp_mod_name, file, filename, info) |
| 155 | self.assertEqual(mod.a, 1) |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 156 | |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 157 | mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 158 | self.assertEqual(mod.a, 1) |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 159 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 160 | mod = imp.load_compiled( |
| 161 | temp_mod_name, imp.cache_from_source(temp_mod_name + '.py')) |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 162 | self.assertEqual(mod.a, 1) |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 163 | |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 164 | if not os.path.exists(test_package_name): |
| 165 | os.mkdir(test_package_name) |
| 166 | with open(init_file_name, 'w') as file: |
| 167 | file.write('b = 2\n') |
| 168 | package = imp.load_package(test_package_name, test_package_name) |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 169 | self.assertEqual(package.b, 2) |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 170 | finally: |
Ezio Melotti | 41a6b04 | 2010-03-06 01:50:25 +0000 | [diff] [blame] | 171 | del sys.path[0] |
Ezio Melotti | 435b531 | 2010-03-06 01:20:49 +0000 | [diff] [blame] | 172 | for ext in ('.py', '.pyc', '.pyo'): |
| 173 | support.unlink(temp_mod_name + ext) |
| 174 | support.unlink(init_file_name + ext) |
Ezio Melotti | 9a7d5ac | 2010-03-05 12:43:17 +0000 | [diff] [blame] | 175 | support.rmtree(test_package_name) |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 176 | |
Victor Stinner | c68b6aa | 2011-04-23 00:41:19 +0200 | [diff] [blame] | 177 | def test_issue9319(self): |
Antoine Pitrou | 1184690 | 2011-04-25 21:39:49 +0200 | [diff] [blame] | 178 | path = os.path.dirname(__file__) |
Victor Stinner | 7fdd0fe | 2011-04-23 01:24:11 +0200 | [diff] [blame] | 179 | self.assertRaises(SyntaxError, |
Antoine Pitrou | 1184690 | 2011-04-25 21:39:49 +0200 | [diff] [blame] | 180 | imp.find_module, "badsyntax_pep3120", [path]) |
Victor Stinner | c68b6aa | 2011-04-23 00:41:19 +0200 | [diff] [blame] | 181 | |
Guido van Rossum | 0ad59d4 | 2009-03-30 22:01:35 +0000 | [diff] [blame] | 182 | |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 183 | class ReloadTests(unittest.TestCase): |
| 184 | |
| 185 | """Very basic tests to make sure that imp.reload() operates just like |
| 186 | reload().""" |
| 187 | |
| 188 | def test_source(self): |
Florent Xicluna | 9713372 | 2010-03-20 20:31:34 +0000 | [diff] [blame] | 189 | # XXX (ncoghlan): It would be nice to use test.support.CleanImport |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 190 | # here, but that breaks because the os module registers some |
| 191 | # handlers in copy_reg on import. Since CleanImport doesn't |
| 192 | # revert that registration, the module is left in a broken |
| 193 | # state after reversion. Reinitialising the module contents |
| 194 | # and just reverting os.environ to its previous state is an OK |
| 195 | # workaround |
| 196 | with support.EnvironmentVarGuard(): |
| 197 | import os |
| 198 | imp.reload(os) |
| 199 | |
| 200 | def test_extension(self): |
| 201 | with support.CleanImport('time'): |
| 202 | import time |
| 203 | imp.reload(time) |
| 204 | |
| 205 | def test_builtin(self): |
| 206 | with support.CleanImport('marshal'): |
| 207 | import marshal |
| 208 | imp.reload(marshal) |
Christian Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 210 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 211 | class PEP3147Tests(unittest.TestCase): |
| 212 | """Tests of PEP 3147.""" |
| 213 | |
| 214 | tag = imp.get_tag() |
| 215 | |
| 216 | def test_cache_from_source(self): |
| 217 | # Given the path to a .py file, return the path to its PEP 3147 |
| 218 | # defined .pyc file (i.e. under __pycache__). |
| 219 | self.assertEqual( |
| 220 | imp.cache_from_source('/foo/bar/baz/qux.py', True), |
| 221 | '/foo/bar/baz/__pycache__/qux.{}.pyc'.format(self.tag)) |
Victor Stinner | ccbf475 | 2011-03-14 15:05:12 -0400 | [diff] [blame] | 222 | # Directory with a dot, filename without dot |
| 223 | self.assertEqual( |
| 224 | imp.cache_from_source('/foo.bar/file', True), |
| 225 | '/foo.bar/__pycache__/file{}.pyc'.format(self.tag)) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 226 | |
| 227 | def test_cache_from_source_optimized(self): |
| 228 | # Given the path to a .py file, return the path to its PEP 3147 |
| 229 | # defined .pyo file (i.e. under __pycache__). |
| 230 | self.assertEqual( |
| 231 | imp.cache_from_source('/foo/bar/baz/qux.py', False), |
| 232 | '/foo/bar/baz/__pycache__/qux.{}.pyo'.format(self.tag)) |
| 233 | |
| 234 | def test_cache_from_source_cwd(self): |
| 235 | self.assertEqual(imp.cache_from_source('foo.py', True), |
| 236 | os.sep.join(('__pycache__', |
| 237 | 'foo.{}.pyc'.format(self.tag)))) |
| 238 | |
| 239 | def test_cache_from_source_override(self): |
| 240 | # When debug_override is not None, it can be any true-ish or false-ish |
| 241 | # value. |
| 242 | self.assertEqual( |
| 243 | imp.cache_from_source('/foo/bar/baz.py', []), |
| 244 | '/foo/bar/__pycache__/baz.{}.pyo'.format(self.tag)) |
| 245 | self.assertEqual( |
| 246 | imp.cache_from_source('/foo/bar/baz.py', [17]), |
| 247 | '/foo/bar/__pycache__/baz.{}.pyc'.format(self.tag)) |
| 248 | # However if the bool-ishness can't be determined, the exception |
| 249 | # propagates. |
| 250 | class Bearish: |
| 251 | def __bool__(self): raise RuntimeError |
| 252 | self.assertRaises( |
| 253 | RuntimeError, |
| 254 | imp.cache_from_source, '/foo/bar/baz.py', Bearish()) |
| 255 | |
| 256 | @unittest.skipIf(os.altsep is None, |
| 257 | 'test meaningful only where os.altsep is defined') |
| 258 | def test_altsep_cache_from_source(self): |
| 259 | # Windows path and PEP 3147. |
| 260 | self.assertEqual( |
| 261 | imp.cache_from_source('\\foo\\bar\\baz\\qux.py', True), |
| 262 | '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag)) |
| 263 | |
| 264 | @unittest.skipIf(os.altsep is None, |
| 265 | 'test meaningful only where os.altsep is defined') |
| 266 | def test_altsep_and_sep_cache_from_source(self): |
| 267 | # Windows path and PEP 3147 where altsep is right of sep. |
| 268 | self.assertEqual( |
| 269 | imp.cache_from_source('\\foo\\bar/baz\\qux.py', True), |
| 270 | '\\foo\\bar/baz\\__pycache__\\qux.{}.pyc'.format(self.tag)) |
| 271 | |
| 272 | @unittest.skipIf(os.altsep is None, |
| 273 | 'test meaningful only where os.altsep is defined') |
| 274 | def test_sep_altsep_and_sep_cache_from_source(self): |
| 275 | # Windows path and PEP 3147 where sep is right of altsep. |
| 276 | self.assertEqual( |
| 277 | imp.cache_from_source('\\foo\\bar\\baz/qux.py', True), |
| 278 | '\\foo\\bar\\baz/__pycache__/qux.{}.pyc'.format(self.tag)) |
| 279 | |
| 280 | def test_source_from_cache(self): |
| 281 | # Given the path to a PEP 3147 defined .pyc file, return the path to |
| 282 | # its source. This tests the good path. |
| 283 | self.assertEqual(imp.source_from_cache( |
| 284 | '/foo/bar/baz/__pycache__/qux.{}.pyc'.format(self.tag)), |
| 285 | '/foo/bar/baz/qux.py') |
| 286 | |
| 287 | def test_source_from_cache_bad_path(self): |
| 288 | # When the path to a pyc file is not in PEP 3147 format, a ValueError |
| 289 | # is raised. |
| 290 | self.assertRaises( |
| 291 | ValueError, imp.source_from_cache, '/foo/bar/bazqux.pyc') |
| 292 | |
| 293 | def test_source_from_cache_no_slash(self): |
| 294 | # No slashes at all in path -> ValueError |
| 295 | self.assertRaises( |
| 296 | ValueError, imp.source_from_cache, 'foo.cpython-32.pyc') |
| 297 | |
| 298 | def test_source_from_cache_too_few_dots(self): |
| 299 | # Too few dots in final path component -> ValueError |
| 300 | self.assertRaises( |
| 301 | ValueError, imp.source_from_cache, '__pycache__/foo.pyc') |
| 302 | |
| 303 | def test_source_from_cache_too_many_dots(self): |
| 304 | # Too many dots in final path component -> ValueError |
| 305 | self.assertRaises( |
| 306 | ValueError, imp.source_from_cache, |
| 307 | '__pycache__/foo.cpython-32.foo.pyc') |
| 308 | |
| 309 | def test_source_from_cache_no__pycache__(self): |
| 310 | # Another problem with the path -> ValueError |
| 311 | self.assertRaises( |
| 312 | ValueError, imp.source_from_cache, |
| 313 | '/foo/bar/foo.cpython-32.foo.pyc') |
| 314 | |
| 315 | def test_package___file__(self): |
| 316 | # Test that a package's __file__ points to the right source directory. |
| 317 | os.mkdir('pep3147') |
| 318 | sys.path.insert(0, os.curdir) |
| 319 | def cleanup(): |
| 320 | if sys.path[0] == os.curdir: |
| 321 | del sys.path[0] |
| 322 | shutil.rmtree('pep3147') |
| 323 | self.addCleanup(cleanup) |
| 324 | # Touch the __init__.py file. |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 325 | support.create_empty_file('pep3147/__init__.py') |
Antoine Pitrou | 4f92a68 | 2012-02-26 18:09:50 +0100 | [diff] [blame] | 326 | importlib.invalidate_caches() |
Antoine Pitrou | abe72d7 | 2012-02-22 01:11:31 +0100 | [diff] [blame] | 327 | expected___file__ = os.sep.join(('.', 'pep3147', '__init__.py')) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 328 | m = __import__('pep3147') |
Antoine Pitrou | 9a4d7dd | 2012-02-27 22:01:25 +0100 | [diff] [blame] | 329 | self.assertEqual(m.__file__, expected___file__, (m.__file__, m.__path__, sys.path, sys.path_importer_cache)) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 330 | # Ensure we load the pyc file. |
Antoine Pitrou | 037615e | 2012-02-22 02:30:09 +0100 | [diff] [blame] | 331 | support.unload('pep3147') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 332 | m = __import__('pep3147') |
Antoine Pitrou | 037615e | 2012-02-22 02:30:09 +0100 | [diff] [blame] | 333 | support.unload('pep3147') |
Antoine Pitrou | 9a4d7dd | 2012-02-27 22:01:25 +0100 | [diff] [blame] | 334 | self.assertEqual(m.__file__, expected___file__, (m.__file__, m.__path__, sys.path, sys.path_importer_cache)) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 335 | |
| 336 | |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 337 | class NullImporterTests(unittest.TestCase): |
Victor Stinner | 09c449c | 2010-08-13 22:23:24 +0000 | [diff] [blame] | 338 | @unittest.skipIf(support.TESTFN_UNENCODABLE is None, |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 339 | "Need an undecodeable filename") |
| 340 | def test_unencodeable(self): |
Victor Stinner | 09c449c | 2010-08-13 22:23:24 +0000 | [diff] [blame] | 341 | name = support.TESTFN_UNENCODABLE |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 342 | os.mkdir(name) |
| 343 | try: |
| 344 | self.assertRaises(ImportError, imp.NullImporter, name) |
| 345 | finally: |
| 346 | os.rmdir(name) |
| 347 | |
| 348 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 349 | def test_main(): |
Hirokazu Yamamoto | 3614409 | 2008-09-09 07:33:27 +0000 | [diff] [blame] | 350 | tests = [ |
| 351 | ImportTests, |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 352 | PEP3147Tests, |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 353 | ReloadTests, |
Victor Stinner | 1a4d12d | 2010-08-13 13:07:29 +0000 | [diff] [blame] | 354 | NullImporterTests, |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 355 | ] |
Hirokazu Yamamoto | 3614409 | 2008-09-09 07:33:27 +0000 | [diff] [blame] | 356 | try: |
| 357 | import _thread |
| 358 | except ImportError: |
| 359 | pass |
| 360 | else: |
| 361 | tests.append(LockTests) |
| 362 | support.run_unittest(*tests) |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 363 | |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 364 | if __name__ == "__main__": |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 365 | test_main() |