blob: a151626de7d2fdbac56e76b193f14b21d77f89b6 [file] [log] [blame]
Brett Cannonbcb26c52009-02-01 04:00:05 +00001from . import util
Brett Cannon2c318a12009-02-07 01:15:27 +00002import imp
3import importlib
4import sys
5import unittest
Brett Cannon23cbd8a2009-01-18 00:24:28 +00006
7
8class ImportModuleTests(unittest.TestCase):
9
10 """Test importlib.import_module."""
11
12 def test_module_import(self):
13 # Test importing a top-level module.
Brett Cannonbcb26c52009-02-01 04:00:05 +000014 with util.mock_modules('top_level') as mock:
15 with util.import_state(meta_path=[mock]):
Brett Cannon23cbd8a2009-01-18 00:24:28 +000016 module = importlib.import_module('top_level')
17 self.assertEqual(module.__name__, 'top_level')
18
19 def test_absolute_package_import(self):
20 # Test importing a module from a package with an absolute name.
21 pkg_name = 'pkg'
22 pkg_long_name = '{0}.__init__'.format(pkg_name)
23 name = '{0}.mod'.format(pkg_name)
Brett Cannonbcb26c52009-02-01 04:00:05 +000024 with util.mock_modules(pkg_long_name, name) as mock:
25 with util.import_state(meta_path=[mock]):
Brett Cannon23cbd8a2009-01-18 00:24:28 +000026 module = importlib.import_module(name)
27 self.assertEqual(module.__name__, name)
28
Brett Cannonb5f03c62009-03-04 01:02:54 +000029 def test_shallow_relative_package_import(self):
Brett Cannon2cf15852010-07-03 22:03:16 +000030 # Test importing a module from a package through a relative import.
Brett Cannon23cbd8a2009-01-18 00:24:28 +000031 pkg_name = 'pkg'
32 pkg_long_name = '{0}.__init__'.format(pkg_name)
33 module_name = 'mod'
34 absolute_name = '{0}.{1}'.format(pkg_name, module_name)
35 relative_name = '.{0}'.format(module_name)
Brett Cannonbcb26c52009-02-01 04:00:05 +000036 with util.mock_modules(pkg_long_name, absolute_name) as mock:
37 with util.import_state(meta_path=[mock]):
Brett Cannon2c318a12009-02-07 01:15:27 +000038 importlib.import_module(pkg_name)
Brett Cannon23cbd8a2009-01-18 00:24:28 +000039 module = importlib.import_module(relative_name, pkg_name)
40 self.assertEqual(module.__name__, absolute_name)
41
Brett Cannonb5f03c62009-03-04 01:02:54 +000042 def test_deep_relative_package_import(self):
43 modules = ['a.__init__', 'a.b.__init__', 'a.c']
44 with util.mock_modules(*modules) as mock:
45 with util.import_state(meta_path=[mock]):
46 importlib.import_module('a')
47 importlib.import_module('a.b')
48 module = importlib.import_module('..c', 'a.b')
49 self.assertEqual(module.__name__, 'a.c')
50
Brett Cannon23cbd8a2009-01-18 00:24:28 +000051 def test_absolute_import_with_package(self):
52 # Test importing a module from a package with an absolute name with
53 # the 'package' argument given.
54 pkg_name = 'pkg'
55 pkg_long_name = '{0}.__init__'.format(pkg_name)
56 name = '{0}.mod'.format(pkg_name)
Brett Cannonbcb26c52009-02-01 04:00:05 +000057 with util.mock_modules(pkg_long_name, name) as mock:
58 with util.import_state(meta_path=[mock]):
Brett Cannon2c318a12009-02-07 01:15:27 +000059 importlib.import_module(pkg_name)
Brett Cannon23cbd8a2009-01-18 00:24:28 +000060 module = importlib.import_module(name, pkg_name)
61 self.assertEqual(module.__name__, name)
62
63 def test_relative_import_wo_package(self):
64 # Relative imports cannot happen without the 'package' argument being
65 # set.
Brett Cannon2153dc02009-08-27 23:49:21 +000066 with self.assertRaises(TypeError):
67 importlib.import_module('.support')
Brett Cannon23cbd8a2009-01-18 00:24:28 +000068
69
Meador Inge416f12d2011-12-14 22:23:46 -060070 def test_loaded_once(self):
71 # Issue #13591: Modules should only be loaded once when
72 # initializing the parent package attempts to import the
73 # module currently being imported.
74 b_load_count = 0
75 def load_a():
76 importlib.import_module('a.b')
77 def load_b():
78 nonlocal b_load_count
79 b_load_count += 1
80 code = {'a': load_a, 'a.b': load_b}
81 modules = ['a.__init__', 'a.b']
82 with util.mock_modules(*modules, module_code=code) as mock:
83 with util.import_state(meta_path=[mock]):
84 importlib.import_module('a.b')
85 self.assertEqual(b_load_count, 1)
86
Brett Cannon23cbd8a2009-01-18 00:24:28 +000087def test_main():
88 from test.support import run_unittest
89 run_unittest(ImportModuleTests)
90
91
92if __name__ == '__main__':
93 test_main()