Guido van Rossum | 394a47b | 2001-08-02 14:14:20 +0000 | [diff] [blame] | 1 | import os, sys, string, random, tempfile, unittest |
| 2 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test.support import run_unittest |
Guido van Rossum | 394a47b | 2001-08-02 14:14:20 +0000 | [diff] [blame] | 4 | |
| 5 | class TestImport(unittest.TestCase): |
| 6 | |
| 7 | def __init__(self, *args, **kw): |
| 8 | self.package_name = 'PACKAGE_' |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 9 | while self.package_name in sys.modules: |
Martin v. Löwis | 967f1e3 | 2007-08-14 09:23:10 +0000 | [diff] [blame] | 10 | self.package_name += random.choose(string.ascii_letters) |
Guido van Rossum | 394a47b | 2001-08-02 14:14:20 +0000 | [diff] [blame] | 11 | self.module_name = self.package_name + '.foo' |
| 12 | unittest.TestCase.__init__(self, *args, **kw) |
| 13 | |
| 14 | def remove_modules(self): |
| 15 | for module_name in (self.package_name, self.module_name): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 16 | if module_name in sys.modules: |
Guido van Rossum | 394a47b | 2001-08-02 14:14:20 +0000 | [diff] [blame] | 17 | del sys.modules[module_name] |
| 18 | |
| 19 | def setUp(self): |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 20 | self.test_dir = tempfile.mkdtemp() |
Guido van Rossum | 394a47b | 2001-08-02 14:14:20 +0000 | [diff] [blame] | 21 | sys.path.append(self.test_dir) |
| 22 | self.package_dir = os.path.join(self.test_dir, |
| 23 | self.package_name) |
| 24 | os.mkdir(self.package_dir) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 25 | open(os.path.join(self.package_dir, '__init__.py'), 'w') |
| 26 | self.module_path = os.path.join(self.package_dir, 'foo.py') |
Guido van Rossum | 394a47b | 2001-08-02 14:14:20 +0000 | [diff] [blame] | 27 | |
| 28 | def tearDown(self): |
| 29 | for file in os.listdir(self.package_dir): |
| 30 | os.remove(os.path.join(self.package_dir, file)) |
| 31 | os.rmdir(self.package_dir) |
| 32 | os.rmdir(self.test_dir) |
| 33 | self.assertNotEqual(sys.path.count(self.test_dir), 0) |
| 34 | sys.path.remove(self.test_dir) |
| 35 | self.remove_modules() |
| 36 | |
| 37 | def rewrite_file(self, contents): |
Tim Peters | d342c62 | 2001-08-02 17:23:11 +0000 | [diff] [blame] | 38 | for extension in "co": |
| 39 | compiled_path = self.module_path + extension |
| 40 | if os.path.exists(compiled_path): |
| 41 | os.remove(compiled_path) |
| 42 | f = open(self.module_path, 'w') |
| 43 | f.write(contents) |
| 44 | f.close() |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 394a47b | 2001-08-02 14:14:20 +0000 | [diff] [blame] | 46 | def test_package_import__semantics(self): |
| 47 | |
| 48 | # Generate a couple of broken modules to try importing. |
| 49 | |
| 50 | # ...try loading the module when there's a SyntaxError |
| 51 | self.rewrite_file('for') |
| 52 | try: __import__(self.module_name) |
| 53 | except SyntaxError: pass |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 54 | else: raise RuntimeError('Failed to induce SyntaxError') |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 55 | self.assert_(self.module_name not in sys.modules and |
Guido van Rossum | 394a47b | 2001-08-02 14:14:20 +0000 | [diff] [blame] | 56 | not hasattr(sys.modules[self.package_name], 'foo')) |
| 57 | |
| 58 | # ...make up a variable name that isn't bound in __builtins__ |
| 59 | var = 'a' |
| 60 | while var in dir(__builtins__): |
Martin v. Löwis | 967f1e3 | 2007-08-14 09:23:10 +0000 | [diff] [blame] | 61 | var += random.choose(string.ascii_letters) |
Guido van Rossum | 394a47b | 2001-08-02 14:14:20 +0000 | [diff] [blame] | 62 | |
| 63 | # ...make a module that just contains that |
| 64 | self.rewrite_file(var) |
| 65 | |
| 66 | try: __import__(self.module_name) |
| 67 | except NameError: pass |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 68 | else: raise RuntimeError('Failed to induce NameError.') |
Guido van Rossum | 394a47b | 2001-08-02 14:14:20 +0000 | [diff] [blame] | 69 | |
| 70 | # ...now change the module so that the NameError doesn't |
| 71 | # happen |
| 72 | self.rewrite_file('%s = 1' % var) |
Tim Peters | 99d001e | 2004-08-02 03:59:57 +0000 | [diff] [blame] | 73 | module = __import__(self.module_name).foo |
Guido van Rossum | 394a47b | 2001-08-02 14:14:20 +0000 | [diff] [blame] | 74 | self.assertEqual(getattr(module, var), 1) |
| 75 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 76 | |
| 77 | def test_main(): |
| 78 | run_unittest(TestImport) |
| 79 | |
| 80 | |
| 81 | if __name__ == "__main__": |
| 82 | test_main() |