Mercurial > cpython-lru_cache-weakref
annotate Lib/importlib/test/test_api.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | d2504d30f259 |
| children | de125ee32897 |
| rev | line source |
|---|---|
|
51426
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51236
diff
changeset
|
1 from . import util |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51236
diff
changeset
|
2 import imp |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
3 import importlib |
|
51426
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51236
diff
changeset
|
4 import sys |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51236
diff
changeset
|
5 import unittest |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
6 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
7 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
8 class ImportModuleTests(unittest.TestCase): |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
9 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
10 """Test importlib.import_module.""" |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
11 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
12 def test_module_import(self): |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
13 # Test importing a top-level module. |
|
51236
d85ef26efd10
Rename importlib.test.support to importlib.test.util.
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
14 with util.mock_modules('top_level') as mock: |
|
d85ef26efd10
Rename importlib.test.support to importlib.test.util.
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
15 with util.import_state(meta_path=[mock]): |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
16 module = importlib.import_module('top_level') |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
17 self.assertEqual(module.__name__, 'top_level') |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
18 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
19 def test_absolute_package_import(self): |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
20 # Test importing a module from a package with an absolute name. |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
21 pkg_name = 'pkg' |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
22 pkg_long_name = '{0}.__init__'.format(pkg_name) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
23 name = '{0}.mod'.format(pkg_name) |
|
51236
d85ef26efd10
Rename importlib.test.support to importlib.test.util.
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
24 with util.mock_modules(pkg_long_name, name) as mock: |
|
d85ef26efd10
Rename importlib.test.support to importlib.test.util.
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
25 with util.import_state(meta_path=[mock]): |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
26 module = importlib.import_module(name) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
27 self.assertEqual(module.__name__, name) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
28 |
|
51993
ebe10810ebed
Add a test for importlib.import_module.
Brett Cannon <bcannon@gmail.com>
parents:
51426
diff
changeset
|
29 def test_shallow_relative_package_import(self): |
|
62758
6c3c543ede8c
Fix a spelling mistake in a comment.
Brett Cannon <bcannon@gmail.com>
parents:
55737
diff
changeset
|
30 # Test importing a module from a package through a relative import. |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
31 pkg_name = 'pkg' |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
32 pkg_long_name = '{0}.__init__'.format(pkg_name) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
33 module_name = 'mod' |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
34 absolute_name = '{0}.{1}'.format(pkg_name, module_name) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
35 relative_name = '.{0}'.format(module_name) |
|
51236
d85ef26efd10
Rename importlib.test.support to importlib.test.util.
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
36 with util.mock_modules(pkg_long_name, absolute_name) as mock: |
|
d85ef26efd10
Rename importlib.test.support to importlib.test.util.
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
37 with util.import_state(meta_path=[mock]): |
|
51426
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51236
diff
changeset
|
38 importlib.import_module(pkg_name) |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
39 module = importlib.import_module(relative_name, pkg_name) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
40 self.assertEqual(module.__name__, absolute_name) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
41 |
|
51993
ebe10810ebed
Add a test for importlib.import_module.
Brett Cannon <bcannon@gmail.com>
parents:
51426
diff
changeset
|
42 def test_deep_relative_package_import(self): |
|
ebe10810ebed
Add a test for importlib.import_module.
Brett Cannon <bcannon@gmail.com>
parents:
51426
diff
changeset
|
43 modules = ['a.__init__', 'a.b.__init__', 'a.c'] |
|
ebe10810ebed
Add a test for importlib.import_module.
Brett Cannon <bcannon@gmail.com>
parents:
51426
diff
changeset
|
44 with util.mock_modules(*modules) as mock: |
|
ebe10810ebed
Add a test for importlib.import_module.
Brett Cannon <bcannon@gmail.com>
parents:
51426
diff
changeset
|
45 with util.import_state(meta_path=[mock]): |
|
ebe10810ebed
Add a test for importlib.import_module.
Brett Cannon <bcannon@gmail.com>
parents:
51426
diff
changeset
|
46 importlib.import_module('a') |
|
ebe10810ebed
Add a test for importlib.import_module.
Brett Cannon <bcannon@gmail.com>
parents:
51426
diff
changeset
|
47 importlib.import_module('a.b') |
|
ebe10810ebed
Add a test for importlib.import_module.
Brett Cannon <bcannon@gmail.com>
parents:
51426
diff
changeset
|
48 module = importlib.import_module('..c', 'a.b') |
|
ebe10810ebed
Add a test for importlib.import_module.
Brett Cannon <bcannon@gmail.com>
parents:
51426
diff
changeset
|
49 self.assertEqual(module.__name__, 'a.c') |
|
ebe10810ebed
Add a test for importlib.import_module.
Brett Cannon <bcannon@gmail.com>
parents:
51426
diff
changeset
|
50 |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
51 def test_absolute_import_with_package(self): |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
52 # Test importing a module from a package with an absolute name with |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
53 # the 'package' argument given. |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
54 pkg_name = 'pkg' |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
55 pkg_long_name = '{0}.__init__'.format(pkg_name) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
56 name = '{0}.mod'.format(pkg_name) |
|
51236
d85ef26efd10
Rename importlib.test.support to importlib.test.util.
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
57 with util.mock_modules(pkg_long_name, name) as mock: |
|
d85ef26efd10
Rename importlib.test.support to importlib.test.util.
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
58 with util.import_state(meta_path=[mock]): |
|
51426
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51236
diff
changeset
|
59 importlib.import_module(pkg_name) |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
60 module = importlib.import_module(name, pkg_name) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
61 self.assertEqual(module.__name__, name) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
62 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
63 def test_relative_import_wo_package(self): |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
64 # Relative imports cannot happen without the 'package' argument being |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
65 # set. |
|
55737
b65ebed06eb0
Move over to using assertRaises as a context manager for importlib tests.
Brett Cannon <bcannon@gmail.com>
parents:
51993
diff
changeset
|
66 with self.assertRaises(TypeError): |
|
b65ebed06eb0
Move over to using assertRaises as a context manager for importlib tests.
Brett Cannon <bcannon@gmail.com>
parents:
51993
diff
changeset
|
67 importlib.import_module('.support') |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
68 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
69 |
|
73973
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
70 def test_loaded_once(self): |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
71 # Issue #13591: Modules should only be loaded once when |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
72 # initializing the parent package attempts to import the |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
73 # module currently being imported. |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
74 b_load_count = 0 |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
75 def load_a(): |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
76 importlib.import_module('a.b') |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
77 def load_b(): |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
78 nonlocal b_load_count |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
79 b_load_count += 1 |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
80 code = {'a': load_a, 'a.b': load_b} |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
81 modules = ['a.__init__', 'a.b'] |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
82 with util.mock_modules(*modules, module_code=code) as mock: |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
83 with util.import_state(meta_path=[mock]): |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
84 importlib.import_module('a.b') |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
85 self.assertEqual(b_load_count, 1) |
|
d2504d30f259
Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents:
62758
diff
changeset
|
86 |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
87 def test_main(): |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
88 from test.support import run_unittest |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
89 run_unittest(ImportModuleTests) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
90 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
91 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
92 if __name__ == '__main__': |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
93 test_main() |
