1 | n/a | import os |
---|
2 | n/a | import sys |
---|
3 | n/a | import shutil |
---|
4 | n/a | import string |
---|
5 | n/a | import random |
---|
6 | n/a | import tempfile |
---|
7 | n/a | import unittest |
---|
8 | n/a | |
---|
9 | n/a | from importlib.util import cache_from_source |
---|
10 | n/a | from test.support import create_empty_file |
---|
11 | n/a | |
---|
12 | n/a | class TestImport(unittest.TestCase): |
---|
13 | n/a | |
---|
14 | n/a | def __init__(self, *args, **kw): |
---|
15 | n/a | self.package_name = 'PACKAGE_' |
---|
16 | n/a | while self.package_name in sys.modules: |
---|
17 | n/a | self.package_name += random.choose(string.ascii_letters) |
---|
18 | n/a | self.module_name = self.package_name + '.foo' |
---|
19 | n/a | unittest.TestCase.__init__(self, *args, **kw) |
---|
20 | n/a | |
---|
21 | n/a | def remove_modules(self): |
---|
22 | n/a | for module_name in (self.package_name, self.module_name): |
---|
23 | n/a | if module_name in sys.modules: |
---|
24 | n/a | del sys.modules[module_name] |
---|
25 | n/a | |
---|
26 | n/a | def setUp(self): |
---|
27 | n/a | self.test_dir = tempfile.mkdtemp() |
---|
28 | n/a | sys.path.append(self.test_dir) |
---|
29 | n/a | self.package_dir = os.path.join(self.test_dir, |
---|
30 | n/a | self.package_name) |
---|
31 | n/a | os.mkdir(self.package_dir) |
---|
32 | n/a | create_empty_file(os.path.join(self.package_dir, '__init__.py')) |
---|
33 | n/a | self.module_path = os.path.join(self.package_dir, 'foo.py') |
---|
34 | n/a | |
---|
35 | n/a | def tearDown(self): |
---|
36 | n/a | shutil.rmtree(self.test_dir) |
---|
37 | n/a | self.assertNotEqual(sys.path.count(self.test_dir), 0) |
---|
38 | n/a | sys.path.remove(self.test_dir) |
---|
39 | n/a | self.remove_modules() |
---|
40 | n/a | |
---|
41 | n/a | def rewrite_file(self, contents): |
---|
42 | n/a | compiled_path = cache_from_source(self.module_path) |
---|
43 | n/a | if os.path.exists(compiled_path): |
---|
44 | n/a | os.remove(compiled_path) |
---|
45 | n/a | with open(self.module_path, 'w') as f: |
---|
46 | n/a | f.write(contents) |
---|
47 | n/a | |
---|
48 | n/a | def test_package_import__semantics(self): |
---|
49 | n/a | |
---|
50 | n/a | # Generate a couple of broken modules to try importing. |
---|
51 | n/a | |
---|
52 | n/a | # ...try loading the module when there's a SyntaxError |
---|
53 | n/a | self.rewrite_file('for') |
---|
54 | n/a | try: __import__(self.module_name) |
---|
55 | n/a | except SyntaxError: pass |
---|
56 | n/a | else: raise RuntimeError('Failed to induce SyntaxError') # self.fail()? |
---|
57 | n/a | self.assertNotIn(self.module_name, sys.modules) |
---|
58 | n/a | self.assertFalse(hasattr(sys.modules[self.package_name], 'foo')) |
---|
59 | n/a | |
---|
60 | n/a | # ...make up a variable name that isn't bound in __builtins__ |
---|
61 | n/a | var = 'a' |
---|
62 | n/a | while var in dir(__builtins__): |
---|
63 | n/a | var += random.choose(string.ascii_letters) |
---|
64 | n/a | |
---|
65 | n/a | # ...make a module that just contains that |
---|
66 | n/a | self.rewrite_file(var) |
---|
67 | n/a | |
---|
68 | n/a | try: __import__(self.module_name) |
---|
69 | n/a | except NameError: pass |
---|
70 | n/a | else: raise RuntimeError('Failed to induce NameError.') |
---|
71 | n/a | |
---|
72 | n/a | # ...now change the module so that the NameError doesn't |
---|
73 | n/a | # happen |
---|
74 | n/a | self.rewrite_file('%s = 1' % var) |
---|
75 | n/a | module = __import__(self.module_name).foo |
---|
76 | n/a | self.assertEqual(getattr(module, var), 1) |
---|
77 | n/a | |
---|
78 | n/a | |
---|
79 | n/a | if __name__ == "__main__": |
---|
80 | n/a | unittest.main() |
---|